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

    protected $fillable = [
        'first_name',
        'last_name',
        'phone',
        'email',
        'count_credits',
        'number_credits',
        'number_bad_repayments',
        'total_credits',
        'total_repayments',
        'in_progress',
    ];

    public function credits() {
        return $this->hasMany(Credit::class);
    }

    public function repayments() {
        return $this->hasMany(Repayment::class);
    }

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

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