<?php

namespace App\Models;

use App\Models\Concerns\BelongsToCompany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class PayrollPtSlab extends Model
{
    use BelongsToCompany;

    protected $table = 'payroll_pt_slabs';

    protected $fillable = [
        'company_id',
        'state',
        'min_gross',
        'max_gross',
        'amount',
        'effective_from',
    ];

    protected function casts(): array
    {
        return [
            'min_gross' => 'decimal:2',
            'max_gross' => 'decimal:2',
            'amount' => 'decimal:2',
            'effective_from' => 'date',
        ];
    }

    public function company(): BelongsTo
    {
        return $this->belongsTo(Company::class);
    }

    public function scopeForState($query, string $state)
    {
        return $query->where('state', $state);
    }

    public function scopeEffectiveAt($query, $date)
    {
        return $query->where(function ($q) use ($date) {
            $q->whereNull('effective_from')->orWhere('effective_from', '<=', $date);
        });
    }
}
