<?php

namespace Modules\HR\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Modules\Accounts\Models\Traits\HasFiscalYear;
use Modules\Core\Traits\HasStatus;
use Illuminate\Database\Eloquent\Builder;
// use Modules\HR\Database\Factories\TaxSlabFactory;

class TaxSlab extends Model
{
    use HasFiscalYear, HasStatus, HasFactory;


    /**
     * The attributes that are mass assignable.
     */
    protected $fillable = [
        'name',
        'fiscal_year_id',
        'min_income',
        'max_income',
        'tax_type',
        'tax_value',
        'gender',
        'is_freedom_fighter',
        'is_disabled',
        'status'
    ];

    protected $casts = [
        'is_disabled' => 'boolean',
        'is_freedom_fighter' => 'boolean',
    ];

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

        return $query->where(function ($q) use ($search) {
            $q->whereHas('fiscalYear', function ($fiscalYearQuery) use ($search) {
                $fiscalYearQuery->where('name', 'LIKE', $search);
            })
            ->orWhere('name', 'LIKE', $search);
        });
    }
    
}
