<?php

namespace Modules\Employee\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Modules\Core\Traits\HasStatus;
use Modules\Employee\Models\Traits\HasEmployee;
use Modules\HR\Models\Traits\HasHolidayGroup;

// use Modules\Employee\Database\Factories\EmployeeHolidayGroupFactory;

class EmployeeHolidayGroup extends Model
{
    use HasStatus, HasEmployee, HasHolidayGroup, HasFactory;

    /**
     * The attributes that are mass assignable.
     */
    protected $fillable = [
        'employee_id',
        'holiday_group_id',
        'status',
        'assigned_from',
        'assigned_to',
        'status',
    ];

    public function scopeFilterByEmployee($query, $employeeId)
    {
        if ($employeeId) {
            $query->where('employee_id', $employeeId);
        }
    }

    public function scopeSearch($query, $term){
        if(blank($term)){
            return $query;
        }
        $term = "%".$term."%";
        $query->where(function ($q) use ($term){
            $q->whereHas('employee', function ($q) use ($term){
                $q->where('name', 'like', $term);
            })->orWhereHas('holidayGroup', function ($q) use ($term){
                $q->where('name', 'like', $term);
            });
        });
        return $query;
    }


}
