<?php

namespace App\Models;

use App\Traits\ModelCounterTrait;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Repayment extends Model
{
    use HasFactory, SoftDeletes, ModelCounterTrait;

    protected $fillable = [
        'lender_id',
        'credit_id',
        'code',
        'amount',
        'date',
        'attachment',
        'observation',
        'is_valid',
        'validation_date',
    ];

    // Relationship with Lender
    public function lender()
    {
        return $this->belongsTo(Lender::class);
    }

    // Relationship with Repayment
    public function credit()
    {
        return $this->belongsTo(Credit::class);
    }

    public static function booted(): void {
        static::creating(function(Repayment $repayment) {
            $repayment->code = $repayment->codeGenerator([
                "prefix" => "REP",
                "model" => "Repayment",
                "length" => 5,
            ]);
        });
    }
}
