<?php

namespace App\Models;

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

class LoanTopUp extends Model
{
    protected $fillable = [
        'top_up_number','branch_id','previous_loan_id','new_loan_id','financial_account_id',
        'previous_outstanding','previous_principal','previous_interest','previous_fees','previous_penalties',
        'amount_settled_internally','new_principal','new_interest','new_fees','new_total_repayment',
        'cash_given_to_customer','top_up_date','reference','notes','executed_by','financial_account_transaction_id',
    ];

    protected function casts(): array
    {
        return [
            'previous_outstanding'=>'decimal:2','previous_principal'=>'decimal:2','previous_interest'=>'decimal:2',
            'previous_fees'=>'decimal:2','previous_penalties'=>'decimal:2','amount_settled_internally'=>'decimal:2',
            'new_principal'=>'decimal:2','new_interest'=>'decimal:2','new_fees'=>'decimal:2','new_total_repayment'=>'decimal:2',
            'cash_given_to_customer'=>'decimal:2','top_up_date'=>'date',
        ];
    }

    public function branch(): BelongsTo { return $this->belongsTo(Branch::class); }
    public function previousLoan(): BelongsTo { return $this->belongsTo(Loan::class, 'previous_loan_id'); }
    public function newLoan(): BelongsTo { return $this->belongsTo(Loan::class, 'new_loan_id'); }
    public function financialAccount(): BelongsTo { return $this->belongsTo(FinancialAccount::class); }
    public function executor(): BelongsTo { return $this->belongsTo(User::class, 'executed_by'); }
    public function accountTransaction(): BelongsTo { return $this->belongsTo(FinancialAccountTransaction::class, 'financial_account_transaction_id'); }
}
