<?php

namespace App\Modules\LoanOrder\Models;

use App\Models\User;
use App\Modules\Branch\Interfaces\BelongsToBranch;
use App\Modules\Branch\Models\Branch;
use App\Modules\LoanOrder\Repositories\LoanOrderRepository;
use App\Modules\OrderStatus\Interfaces\HasStatus;
use App\Modules\Province\Models\Province;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Carbon;

/**
 * @property int $id
 * @property string $unique_id
 * @property string|null $source
 * @property int|null $user_id
 * @property int $loan_type
 * @property string $region
 * @property int $branch_id
 * @property string $customer_name
 * @property string $customer_surname
 * @property string|null $customer_patronic_name
 * @property string $passport_address
 * @property string $real_address
 * @property string $passport_serie
 * @property string $passport_id
 * @property Carbon $passport_given_at
 * @property string $passport_given_by
 * @property string $born_place
 * @property Carbon $born_at
 * @property string|null $email
 * @property string $phone
 * @property string|null $phone_additional
 * @property string|null $phone_home
 * @property string|null $work_region
 * @property int|null $work_province_id
 * @property string|null $work_company
 * @property string|null $work_company_accountant_number
 * @property Carbon|null $work_started_at
 * @property string|null $work_salary
 * @property string|null $work_position
 * @property string $education
 * @property string $marriage_status
 * @property string $passport_one
 * @property string $passport_two
 * @property string $passport_three
 * @property string $passport_four
 * @property string|null $loan_amount
 * @property string|null $card_number
 * @property string|null $card_name
 * @property string|null $card_month
 * @property string|null $card_year
 * @property string $guarantor_name
 * @property string|null $guarantor_surname
 * @property string|null $guarantor_patronic_name
 * @property string|null $guarantor_passport_serie
 * @property string|null $guarantor_passport_id
 * @property string|null $guarantor_card_number
 * @property string|null $guarantor_card_name
 * @property string|null $guarantor_card_month
 * @property string|null $guarantor_card_year
 * @property string|null $guarantor_note
 * @property string|null $guarantor_2_name
 * @property string|null $guarantor_2_surname
 * @property string|null $guarantor_2_patronic_name
 * @property string|null $guarantor_2_passport_serie
 * @property string|null $guarantor_2_passport_id
 * @property string|null $guarantor_2_card_number
 * @property string|null $guarantor_2_card_name
 * @property string|null $guarantor_2_card_month
 * @property string|null $guarantor_2_card_year
 * @property string|null $guarantor_2_note
 * @property string|null $loan_card_number
 * @property string|null $loan_card_name
 * @property string|null $loan_card_month
 * @property string|null $loan_card_year
 * @property int $loan_order_required_doc_id
 * @property string|null $status
 * @property string|null $satisfiable
 * @property string|null $notes
 * @property Carbon|null $created_at
 * @property Carbon|null $updated_at
 * @property Carbon|null $deleted_at
 */
class LoanOrder extends Model implements BelongsToBranch, HasStatus
{
    use SoftDeletes;

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

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

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

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

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

    /**
     * Required docs
     *
     * @return BelongsTo<LoanOrderRequiredDocs, $this>
     */
    public function requiredDocs(): BelongsTo
    {
        return $this->belongsTo(LoanOrderRequiredDocs::class, 'loan_order_required_doc_id');
    }

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

        static::creating(LoanOrderRepository::creating());
        static::created(LoanOrderRepository::created());
        // static::updated(function (LoanOrder $model) {
        //     if ($model->notes && $model->wasChanged('notes')) {
        //         Alert::create([
        //             'user_id' => $model->user_id,
        //             'name' => 'Duýdyryş',
        //             'value' => $model->notes,
        //         ]);
        //     }
        // });
    }
}
