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

    protected $fillable = [
        'first_name',
        'last_name',
        'phone',
        'email',
        'count_loans',
        'number_loans',
        'number_bad_restitutions',
        'total_loans',
        'total_restitutions',
        'in_progress',
    ];

    public function loans() {
        return $this->hasMany(Loan::class);
    }

    public function restitutions() {
        return $this->hasMany(Restitution::class);
    }

    public function name() {
        return $this->first_name . ' ' . $this->last_name;
    }

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