<?php
declare(strict_types=1);

namespace App\Modules\HR\Models;

use App\Core\Model;

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

    protected static array $fillable = [
        'code', 'name_ar', 'name_en', 'category',
        'default_days_per_year', 'max_days_per_year', 'pay_percentage',
        'is_paid', 'requires_approval', 'requires_attachment',
        'min_days_per_request', 'max_days_per_request', 'min_consecutive_annual',
        'max_consecutive_days', 'max_per_occurrence', 'max_times_in_career',
        'min_service_months', 'advance_notice_days',
        'carry_over_allowed', 'max_carry_over_days',
        'carry_over_expiry_months', 'is_accumulative', 'accumulation_max_years',
        'gender_restriction', 'career_max_times', 'career_max_days',
        'deduct_from_salary', 'legal_reference', 'is_active', 'sort_order', 'rules_json',
    ];

    public static function allActive(): array
    {
        return static::query()
            ->where('is_active', '=', 1)
            ->orderBy('sort_order', 'ASC')
            ->get();
    }

    public static function findByCode(string $code): ?array
    {
        return static::query()->where('code', '=', $code)->first();
    }

    public static function getCategories(): array
    {
        return [
            'annual'  => 'سنوية',
            'sick'    => 'مرضية',
            'casual'  => 'عارضة',
            'special' => 'خاصة',
            'unpaid'  => 'بدون راتب',
        ];
    }

    public function getRules(): array
    {
        $json = $this->rules_json ?? '{}';
        return json_decode($json, true) ?: [];
    }
}
