<?php
declare(strict_types=1);

namespace App\Modules\HR\Models;

use App\Core\Model;
use App\Core\App;

class HrEmployeeSalaryDetail extends Model
{
    protected static string $table = 'hr_employee_salary_details';
    protected static string $primaryKey = 'id';
    protected static bool $timestamps = true;
    protected static bool $softDelete = false;
    protected static bool $autoTrackAuthor = true;

    protected static array $fillable = [
        'employee_profile_id', 'component_id', 'amount', 'percentage',
        'effective_from', 'effective_to', 'is_active',
    ];

    public static function getForEmployee(int $profileId, ?string $asOfDate = null): array
    {
        $db = App::getInstance()->db();
        $date = $asOfDate ?? date('Y-m-d');

        return $db->select(
            "SELECT esd.*, sc.component_code, sc.name_ar, sc.name_en, sc.type,
                    sc.calculation_type, sc.percentage_of, sc.percentage_value,
                    sc.is_taxable, sc.is_insurable, sc.is_basic, sc.is_variable,
                    sc.is_system_calculated, sc.affects_overtime
             FROM hr_employee_salary_details esd
             JOIN hr_salary_components sc ON sc.id = esd.component_id
             WHERE esd.employee_profile_id = ?
               AND esd.effective_from <= ?
               AND (esd.effective_to IS NULL OR esd.effective_to >= ?)
             ORDER BY sc.sort_order ASC",
            [$profileId, $date, $date]
        );
    }
}
