<?php

namespace App\Models\Application;

use App\Models\Administration\Office;
use App\Models\Administration\Site;
use App\Models\Administration\User;
use App\Models\Logs\ClientLog;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;
use PhpParser\Builder\Class_;

class Client extends Model
{
    use HasFactory;

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

    protected $fillable = [
        'id',
        'fk_site',
        'fk_office',
        'firstname',
        'lastname',
        'secondlastname',
        'curp',
        'fk_city',
        'colony',
        'address',
        'phonenumber',
        'fk_client_status',
        'fk_endorsement_status',
        'has_employee',
        'visible',
        'available',
        'fk_usercreator',
        'created_at',
        'fk_userupdater',
        'updated_at',
        'is_active'
    ];

    protected $casts = [
        'id' => 'integer',
        'fk_site' => 'integer',
        'fk_office' => 'integer',
        'fk_city' => 'integer',
        'fk_client_status' => 'integer',
        'fk_endorsement_status' => 'integer',
        'has_employee' => 'boolean',
        '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_city',
        'fk_client_status',
        'fk_endorsement_status',
        'fk_usercreator',
        'fk_userupdater',
        'is_active',
    ];

    public static function booted()
    {
        static::updated(function ($client) {
            Log::channel('client')->info('Client Updated', [
                'id' => $client->id,
                'fk_userupdater' => $client->fk_userupdater,
                'updated_at' => Carbon::now(),
                'changes' => $client->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 fullname()
    {
        return $this->firstname . ' ' . $this->lastname . ' ' . $this->secondlastname;
    }

    public function city()
    {
        return $this->belongsTo(City::class, 'fk_city', 'id')->select('id', 'cityname');
    }

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

    public function client_status()
    {
        return $this->hasOne(Status::class, 'id', 'fk_client_status')->select('id', 'statusname', 'classname');
    }

    public function endorsement_status()
    {
        return $this->hasOne(Status::class, 'id', 'fk_endorsement_status')->select('id', 'statusname', 'classname');
    }

    public function statuses()
    {
        return $this->hasManyThrough(
            Status::class,
            ClientStatus::class,
            'fk_client',
            'id',
            'id',
            'fk_status'
        )->select('sy_statuses.id', 'statusname', 'classname');
    }

    public function client_history()
    {
        return $this->hasMany(ClientLog::class,  'fk_client', 'id')->orderBy('updated_at', 'DESC');
    }

    public function loan_client_history()
    {
        return $this->hasMany(Loan::class, 'fk_client', 'id')->where('is_active', true)->orderBy('loandate', 'DESC');
    }

    public function loan_endorsement_history()
    {
        return $this->hasMany(Loan::class, LoanEndorsement::class, 'fk_endorsement', 'id', 'id', 'fk_loan');
    }

    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');
    }
}
