<?php

namespace App\Models\Order\Loan;

use App\Models\Branch\Branch;
use App\Models\System\Location\Province;
use App\Models\User;
use App\Repos\Order\Loan\LoanOrderRepo;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
use Laravel\Nova\Actions\Actionable;

class LoanOrder extends Model
{
    use Actionable;
    use HasFactory;
    use SoftDeletes;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'unique_id',
        'loan_type',
        'region',
        'branch_id',
        'customer_name',
        'customer_surname',
        'customer_patronic_name',
        'passport_address',
        'real_address',
        'passport_serie',
        'passport_id',
        'passport_given_at',
        'passport_given_by',
        'born_place',
        'born_at',
        'email',
        'phone',
        'phone_additional',
        'phone_home',
        'work_region',
        'work_province_id',
        'work_company',
        'work_company_accountant_number',
        'work_started_at',
        'work_salary',
        'work_position',
        'education',
        'marriage_status',
        'passport_one',
        'passport_two',
        'passport_three',
        'passport_four',
        'user_id',
        'status',
        'status_reason',
        'notes',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array
     */
    protected $casts = [
        'born_at' => 'date',
        'passport_given_at' => 'date',
        'work_started_at' => 'date',
    ];

    /**
     * Loan type
     */
    public function loanType(): BelongsTo
    {
        return $this->belongsTo(LoanType::class, 'loan_type');
    }

    /**
     * Branch
     */
    public function branch(): BelongsTo
    {
        return $this->belongsTo(Branch::class);
    }

    /**
     * Work province
     */
    public function workProvince(): BelongsTo
    {
        return $this->belongsTo(Province::class, 'province_id');
    }

    /**
     * User (who created order)
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class, 'user_id');
    }

    /**
     * "boot" method for model
     */
    protected static function boot()
    {
        parent::boot();

        static::creating(LoanOrderRepo::creating());
        static::created(LoanOrderRepo::created());
    }
}
