<?php

namespace Modules\HR\Models;

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

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

class SalaryComponent extends Model
{
    use HasStatus, HasFactory;

    /**
     * The attributes that are mass assignable.
     */
    protected $fillable = [
        'name',
        'code',
        'type',
        'calculation_type',
        'calculation_value',
        'formula',
        'calculation_basis',
        'is_taxable',
        'is_basic',
        'is_cpf',
        'affects_attendance',
        'affects_overtime',
        'include_in_gross',
        'include_in_net',
        'is_pro_rated',
        'sort_order',
        'expense_account_id',
        'liability_account_id',
        'status',
    ];

    protected $casts = [
        'is_taxable' => 'boolean',
        'is_basic' => 'boolean',
        'is_cpf' => 'boolean',
        'affects_attendance' => 'boolean',
        'affects_overtime' => 'boolean',
        'include_in_gross' => 'boolean',
        'include_in_net' => 'boolean',
        'is_pro_rated' => 'boolean',
    ];

    public function scopeSearch($query, ?string $search)
    {
        if (blank($search)) {
            return $query;
        }
        $search = '%' . trim($search) . '%';
        return $query->where('name', 'like', $search)->orWhere('code', 'like', $search);
    }
}
