<?php

namespace App\Models\Application;

use App\Models\Administration\Office;
use App\Models\Administration\Site;
use App\Models\Administration\User;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;

class Authorization extends Model
{
    use HasFactory;

    protected $table = 'sy_authorizations';
    public $timestamps = false;

    protected $fillable = [
        'id',
        'fk_site',
        'fk_office',
        'fk_loan_type',
        'fk_group',
        'fullname',
        'loandate',
        'loanamount',
        'weeklypayment',
        'renewpayment',
        'paperworkpayment',
        'otherpayment',
        'realamount',
        'fk_status',
        'visible',
        'available',
        'fk_usercreator',
        'created_at',
        'fk_userupdater',
        'udapted_at',
        'is_active',
    ];

    protected $casts = [
        'id' => 'integer',
        'fk_site' => 'integer',
        'fk_office' => 'integer',
        'fk_loan_type' => 'integer',
        'fk_group' => 'integer',
        'loandate' => 'date',
        'loanamount' => 'float',
        'weeklypayment' => 'float',
        'renewpayment' => 'float',
        'paperworkpayment' => 'float',
        'otherpayment' => 'float',
        'realamount' => 'float',
        'fk_status' => 'integer',
        'visible' => 'boolean',
        'available' => 'boolean',
        'fk_usercreator' => 'integer',
        'created_at' => 'timestamp',
        'fk_userupdater' => 'integer',
        'updated_at' => 'timestamp',
        'is_active' => 'boolean'
    ];

    protected $hidden = [
        'fk_site',
        'fk_office',
        'fk_loan_type',
        'fk_group',
        'fk_client',
        'fk_supervisor',
        'fk_promoter',
        'fk_collaborator',
        'fk_status',
        'fk_usercreator',
        'fk_userupdater',
        'is_active',
    ];

    protected static function booted()
    {
        static::updated(function ($loanauthroization) {
            Log::channel('loanauthroization')->info('LoanAuthorization Updated', [
                'id' => $loanauthroization->id,
                'fk_userupdater' => $loanauthroization->fk_userupdater,
                'updated_at' => Carbon::now(),
                'changes' => $loanauthroization->getChanges()
            ]);
        });
    }

    public function site()
    {
        return $this->belongsTo(Site::class, 'fk_site', 'id');
    }

    public function office()
    {
        return $this->belongsTo(Office::class, 'fk_office', 'id');
    }

    public function loan_type()
    {
        return $this->belongsTo(LoanType::class, 'fk_loan_type', 'id')->select('id', 'loantypename', 'weeklypayment', 'period', 'duration', 'interesrate', 'alloweddays');
    }

    public function group()
    {
        return $this->belongsTo(Group::class, 'fk_group', 'id')->select('id', 'fk_zone', 'groupname', 'fk_promoter');
    }

    public function status()
    {
        return $this->belongsTo(Status::class, 'fk_status', 'id')->select('id', 'fk_status_type', 'statusname', 'classname');
    }
    public function user_creator()
    {
        return $this->hasOne(User::class, 'id', 'fk_usercreator')->select('id', 'username');
    }

    public function user_updater()
    {
        return $this->hasOne(User::class, 'id', 'fk_userupdater')->select('id', 'username');
    }
}
