<?php
declare(strict_types=1);

namespace App\Modules\HR\Models;

use App\Core\Model;

class HrWorkSchedule extends Model
{
    protected static string $table = 'hr_work_schedules';
    protected static string $primaryKey = 'id';
    protected static bool $timestamps = true;
    protected static bool $softDelete = true;
    protected static bool $autoTrackAuthor = true;

    protected static array $fillable = [
        'code', 'name_ar', 'name_en', 'schedule_type',
        'working_days_per_week', 'hours_per_day', 'hours_per_week',
        'ramadan_hours_per_day', 'is_night_shift', 'start_time', 'end_time',
        'break_duration_minutes', 'late_tolerance_minutes', 'early_leave_tolerance',
        'days_config_json', 'is_active',
    ];

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

    public static function getScheduleTypes(): array
    {
        return [
            'fixed'    => 'ثابت',
            'rotating' => 'دوري',
            'flexible' => 'مرن',
        ];
    }

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

    public function isWorkingDay(string $dayName): bool
    {
        $config = $this->getDaysConfig();
        $dayLower = strtolower($dayName);
        return ($config[$dayLower]['is_working'] ?? false) === true;
    }
}
