<?php

namespace App\Models;

use App\Models\Concerns\BelongsToTenant;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class LoanPaymentAllocation extends Model
{
    use BelongsToTenant;

    protected $fillable = [
        'tenant_id',
        'loan_payment_id',
        'loan_repayment_schedule_id',
        'principal_amount',
        'interest_amount',
        'penalty_amount',
        'total_amount',
    ];

    protected function casts(): array
    {
        return [
            'principal_amount' => 'decimal:2',
            'interest_amount' => 'decimal:2',
            'penalty_amount' => 'decimal:2',
            'total_amount' => 'decimal:2',
        ];
    }

    public function payment(): BelongsTo
    {
        return $this->belongsTo(LoanPayment::class, 'loan_payment_id');
    }

    public function schedule(): BelongsTo
    {
        return $this->belongsTo(LoanRepaymentSchedule::class, 'loan_repayment_schedule_id')
            ->withoutGlobalScope('current_schedule');
    }
}
