<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class FinancingSchedule extends Model
{
    protected $table = 'financing_schedules';

    protected $fillable = [
        'financing_application_id',
        'schedule_number',
        'amount',
        'due_date',
        'paid_date',
        'status',
        'payment_proof_url',
        'notes',
    ];

    protected $casts = [
        'amount' => 'decimal:2',
        'due_date' => 'date',
        'paid_date' => 'date',
    ];

    public const STATUS_PENDING = 'pending';
    public const STATUS_PAID = 'paid';
    public const STATUS_LATE = 'late';

    public function application(): BelongsTo
    {
        return $this->belongsTo(FinancingApplication::class, 'financing_application_id');
    }

    public function isPaid(): bool
    {
        return $this->status === self::STATUS_PAID;
    }

    public function isLate(): bool
    {
        return $this->status === self::STATUS_LATE || ($this->due_date->isPast() && $this->status !== self::STATUS_PAID);
    }
}