<?php

namespace App\Models;

use App\Models\Commune;
use App\Models\Village;
use App\Models\District;
use App\Models\Province;
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','accommodation',
        'name_en','income','expense','balance','remark','group_no','house_no','street_no','occupation',
        'link_map_house','link_map_business','image',
        'google_map_lat', 'google_map_lng', 'google_map_url'
    ];

    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','accommodation',
        'name_en','income','expense','balance','remark','group_no','house_no','street_no','occupation',
        'link_map_house','link_map_business','image',
        'google_map_lat', 'google_map_lng', 'google_map_url'
    ];


    // 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 collaterals()
    {
        return $this->hasMany(Collateral::class, 'client_id');
    }

    public function guarators()
    {
        return $this->hasMany(Guarantor::class, 'client_id')->whereIn('type',[2,3]);
    }

    public function createdBy()
    {
        return $this->belongsTo(Group::class, 'created_by');
    }

    public function branchs()
    {
        return $this->belongsTo(Branch::class, 'branch_id');
    }

    public function collateral()
    {
        return $this->belongsTo(Collateral::class,'client_id');
    }

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