<?php
declare(strict_types=1);

namespace App\Modules\SportsActivity\Models;

use App\Core\Model;

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

    protected static array $fillable = [
        'facility_id', 'name_ar', 'name_en', 'bracket_type',
        'start_time', 'end_time', 'days_of_week_json', 'is_active',
    ];

    public static function getBracketTypeOptions(): array
    {
        return [
            'peak'     => 'ذروة',
            'off_peak' => 'عادي',
            'weekend'  => 'عطلة أسبوعية',
            'holiday'  => 'إجازة',
        ];
    }

    public function getDaysOfWeek(): array
    {
        $json = $this->days_of_week_json;
        if (is_string($json)) {
            return json_decode($json, true) ?: [];
        }
        return $json ?: [];
    }

    public static function getByFacility(int $facilityId): array
    {
        return static::query()
            ->where('facility_id', '=', $facilityId)
            ->where('is_active', '=', 1)
            ->orderBy('start_time', 'ASC')
            ->get();
    }
}
