<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Relations\HasMany;

class SalaryComponent extends BaseModel
{
    protected $fillable = [
        'name',
        'type',
        'calculation_type',
        'value',
        'status',
    ];

    public function payrollStructureComponents(): HasMany
    {
        return $this->hasMany(PayrollStructureComponent::class);
    }

    public function isUsedInAnyStructure(): bool
    {
        return $this->payrollStructureComponents()->exists();
    }

    public function isUsedInApprovedPayroll(): bool
    {
        return $this->payrollStructureComponents()
            ->whereHas('payrollStructure.salaryAssignments.employee.payrollItems.payroll', function ($q) {
                $q->where('status', 'approved');
            })
            ->exists();
    }
}
