<?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 Restitution extends Model
{
    use HasFactory, softDeletes, ModelCounterTrait;

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

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

    // Relationship with Restitution
    public function loan()
    {
        return $this->belongsTo(Loan::class);
    }

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