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

    protected $fillable = [
        'id','title','year','date'
    ];
    protected $logAttributes = [
        'id','title','year','date'
    ];
    
    protected static $recordEvents = ['deleted', 'created', 'updated'];
    protected static $logName = 'Holiday 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);
            $query->Year($searchText);
        }
    }

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

    public function scopeYear($query,$searchText)
    {
        return $query->orWhere("year","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');
    }
}