<?php

namespace App\Modules\HR\Payroll\Models;

use App\Modules\Admin\Tenancy\Traits\BelongsToTenant;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class SalaryStructure extends Model
{
    use BelongsToTenant;

    protected $table = 'hr_salary_structures';

    protected $fillable = ['tenant_id', 'name', 'currency', 'is_active'];

    protected function casts(): array
    {
        return ['is_active' => 'boolean'];
    }

    public function components(): HasMany
    {
        return $this->hasMany(SalaryStructureComponent::class, 'structure_id');
    }

    public function fixedBaseAmount(): float
    {
        return (float) $this->components
            ->where('component_type', 'earning')
            ->where('calculation', 'fixed')
            ->sum('amount');
    }
}
