<?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 Category extends Model
{
    use SoftDeletes, LogsActivity;

    protected $fillable = [
        'id','title','purpose'
    ];
    protected $logAttributes = [
        'id','title','purpose'
    ];

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

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

    public function scopeSearch($query,$searchText)
    {
        if(isset($searchText)){
            $query->Title($searchText);
        }
    }

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

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

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

}
