<?php

namespace Modules\HR\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Modules\HR\Models\Traits\HasLeave;
use Modules\Core\Traits\HasStatus;

// use Modules\HR\Database\Factories\LeavePolicyFactory;


class LeavePolicy extends Model
{
    use HasLeave,
        HasFactory,
        HasStatus;

    /**
     * The attributes that are mass assignable.
     */
    protected $fillable = ['leave_type_id', 'annual_limit', 'carry_forward', 'encashable', 'max_carry_forward_days', 'status'];

    protected $casts = [
        'carry_forward' => 'boolean',
        'encashable' => 'boolean',
    ];

    public function scopeSearch($query, ?string $search)
    {
        if (blank($search)) {
            return $query;
        }
        $search = '%' . trim($search) . '%';

        return $query->whereHas('leaveType', function ($leaveType) use ($search) {
            $leaveType->where('name', 'like', $search);
        });
    }
}
