<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Payment extends Model
{
    protected $table = 'payments';

    protected $fillable = [
        'registration_id',
        'payment_mode',
        'total_amount',
        'paid_amount',
        'balance_amount',
        'payment_date',
        'status',
    ];

    protected $casts = [
        'total_amount' => 'decimal:2',
        'paid_amount' => 'decimal:2',
        'balance_amount' => 'decimal:2',
        'payment_date' => 'datetime',
    ];

    /*
    |--------------------------------------------------------------------------
    | Relationships
    |--------------------------------------------------------------------------
    */

    // 🔗 Belongs to Registration
    public function registration()
    {
        return $this->belongsTo(LockerRegistration::class, 'registration_id');
    }

    /*
    |--------------------------------------------------------------------------
    | Scopes (Useful)
    |--------------------------------------------------------------------------
    */

    public function scopePaid($query)
    {
        return $query->where('status', 'paid');
    }

    public function scopePending($query)
    {
        return $query->where('status', 'pending');
    }

    public function scopePartial($query)
    {
        return $query->where('status', 'partial');
    }

    /*
    |--------------------------------------------------------------------------
    | Helper Methods
    |--------------------------------------------------------------------------
    */

    // Check if fully paid
    public function isPaid()
    {
        return $this->status === 'paid';
    }

    // Check if pending
    public function isPending()
    {
        return $this->status === 'pending';
    }

    // Check if partial payment
    public function isPartial()
    {
        return $this->status === 'partial';
    }

    // Calculate balance automatically
    public function calculateBalance()
    {
        return $this->total_amount - $this->paid_amount;
    }
}