<?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','title','client_id','collateral_id','status','number','body_number','number_plate','latabat','collateral_owner','other_price',
        'model_type','color','engine_number','cylinder_size','degree','year','date_made','price_market','price_urgent','authority_price','photo','image',
        'head_land','size','issued_by','confirmed_by','location','status_type','north','south','east','west','description','created_by','pawn_type','strength',
        'link_map','weight'
    ];
    protected $logAttributes = [
        'id','title','client_id','collateral_id','status','number','body_number','number_plate','latabat','collateral_owner','other_price',
        'model_type','color','engine_number','cylinder_size','degree','year','date_made','price_market','price_urgent','authority_price','photo','image',
        'head_land','size','issued_by','confirmed_by','location','status_type','north','south','east','west','description','created_by','pawn_type','strength',
        'link_map','weight'
    ];

    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 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');
    }
    public function loan()
    {
        return $this->belongsTo(Loan::class, 'loan_id');
    }

}
