<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Auth;
use Spatie\Activitylog\Contracts\Activity;
use Spatie\Activitylog\Traits\LogsActivity;

class CollateralDetail extends Model
{
    use SoftDeletes, LogsActivity;

    protected $fillable = [
        'id','client_id','num','collateral_name','ownership_doc_type','status','number','body_number','number_plate','latabat','collateral_owner',
        'model_type','color','engine_number','cylinder_size','degree','year','date_made','price_market','price_urgent',
        'head_land','size','issued_by','confirmed_by','location','status_type','north','south','east','west','description','created_by','pawn_type','strength',
        'customs_office','transportation','vehicle_technical_certificate_expire','letter_of_car','copy_of_idcard','image','photo','price_collateral'
    ];
    protected $logAttributes = [
        'id','client_id','num','collateral_name','ownership_doc_type','status','number','body_number','number_plate','latabat','collateral_owner',
        'model_type','color','engine_number','cylinder_size','degree','year','date_made','price_market','price_urgent',
        'head_land','size','issued_by','confirmed_by','location','status_type','north','south','east','west','description','created_by','pawn_type','strength',
        'customs_office','transportation','vehicle_technical_certificate_expire','letter_of_car','copy_of_idcard','image','photo','price_collateral'
    ];

    protected static $recordEvents = ['deleted', 'created', 'updated'];
    protected static $logName = 'Collateral Detail 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 client()
    {
        return $this->belongsTo(Client::class, 'client_id');
    }
    public function usingCollateral()
    {
        return $this->hasMany(LoanCollateral::class, 'collateral_id');
    }

    // public function loan()
    // {
    //     return $this->belongsTo(Loan::class, 'loan_id');
    // }
    public function CreadteBy()
    {
        return $this->belongsTo(User::class, 'created_by');
    }

    // public function collateral()
    // {
    //     return $this->belongsTo(Collateral::class, 'collateral_id');
    // }
    // public function category()
    // {
    //     return $this->belongsTo(Category::class,'cat_id');
    // }
}
