<?php

namespace App\Models;

use App\Models\User;
use App\Models\Commune;
use App\Models\Village;
use App\Models\District;
use App\Models\Province;
use App\Models\CollateralDetail;
use Illuminate\Support\Facades\Auth;
use Illuminate\Database\Eloquent\Model;
use Spatie\Activitylog\Contracts\Activity;
use Spatie\Activitylog\Traits\LogsActivity;
use Illuminate\Database\Eloquent\SoftDeletes;

class Client extends Model
{
    use SoftDeletes, LogsActivity;

    protected $fillable = [
        'name','code','branch_id', 'gender', 'type_document', 'id_card', 'date_of_birth', 'issued_by', 'place_of_birth',
        'create_card_date', 'expired_card_date','created_by', 'phone', 'photo', 'work_place', 'relationship','address',
        'status', 'house', 'position', 'province_id','district_id', 'commune_id', 'village_id', 'village_kh','institution',
        'name_en','income','expense','balance','remark','group_no','house_no','street_no','occupation','commune_kh','district_kh','province_kh'
    ];
    protected $logAttributes = [
        'name','code','branch_id', 'gender', 'type_document', 'id_card', 'date_of_birth', 'issued_by', 'place_of_birth',
        'create_card_date', 'expired_card_date','created_by', 'phone', 'photo', 'work_place', 'relationship','address',
        'status', 'house', 'position', 'province_id','district_id', 'commune_id', 'village_id', 'village_kh','institution',
        'name_en','income','expense','balance','remark','group_no','house_no','street_no','occupation','commune_kh','district_kh','province_kh'
    ];

    protected static $recordEvents = ['deleted', 'created', 'updated'];
    protected static $logName = 'Client Model';
    protected static $logOnlyDirty = true;

    public function getDescriptionForEvent(string $eventName): string
    {
        return "This user has been {$eventName}";
    }

    public function tapActivity(Activity $activity, string $eventName)
    {
        $activity->ip = request()->ip();
        $activity->branch_id = Auth::user()->branch_id;
        $activity->agent = request()->header('user-agent');
    }

    public function scopeSearch($query,$searchText)
    {
        if(isset($searchText)){
            $query->Name($searchText);
            $query->NameEn($searchText);
            $query->Code($searchText);
            $query->IDCard($searchText);
            $query->Phone($searchText);
        }
    }

    public function scopeName($query,$searchText)
    {
        return $query->where("name","LIKE","$searchText%");
    }

    public function scopeNameEn($query,$searchText)
    {
        return $query->orWhere("name_en","LIKE","$searchText%");
    }

    public function scopeCode($query,$searchText)
    {
        return $query->orWhere("code","LIKE","$searchText%");
    }

    public function scopeIDCard($query,$searchText)
    {
        return $query->orWhere("id_card","LIKE","$searchText%");
    }

    public function scopePhone($query,$searchText)
    {
        return $query->orWhere("phone","LIKE","$searchText%");
    }

    public function village()
    {
        return $this->belongsTo(Village::class, 'village_id');
    }

    public function commune()
    {
        return $this->belongsTo(Commune::class, 'commune_id');
    }

    public function district()
    {
        return $this->belongsTo(District::class, 'district_id');
    }

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

    public function clientHasLoan()
    {
        return $this->hasMany(Loan::class, 'client_id');
    }

    public function guarator_1()
    {
        return $this->hasMany(Guarantor::class, 'client_id')->where('type',1);
    }

    public function guarators()
    {
        return $this->hasMany(Guarantor::class, 'client_id')->whereIn('type',[2,3]);
    }
    public function collateralDetail()
    {
        return $this->hasMany(CollateralDetail::class, 'client_id');
    }
    public function createdBy()
    {
        // return $this->belongsTo(Group::class, 'created_by');
        return $this->belongsTo(User::class, 'created_by');
    }
}
