<?php
declare(strict_types=1);

namespace App\Modules\FacilityGrids\Models;

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

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

    protected static array $fillable = [
        'grid_id', 'plan_month', 'schedule_name', 'zone_selection_type', 'zone_selection_json',
        'day_of_week', 'start_time', 'end_time', 'activity_type',
        'coach_id', 'coach_name', 'academy_id', 'academy_name',
        'age_group', 'gender', 'max_occupants', 'color',
        'effective_from', 'effective_to', 'is_active', 'notes',
    ];

    public function getZonePositions(): array
    {
        $decoded = json_decode($this->zone_selection_json ?? '[]', true);
        return is_array($decoded) ? $decoded : [];
    }

    public static function getActiveForGrid(int $gridId, string $date): array
    {
        $db = App::getInstance()->db();
        $dayOfWeek = (int) date('w', strtotime($date));

        return $db->select(
            "SELECT * FROM facility_zone_schedules
             WHERE grid_id = ? AND day_of_week = ? AND is_active = 1
             AND effective_from <= ? AND (effective_to IS NULL OR effective_to >= ?)
             ORDER BY start_time ASC",
            [$gridId, $dayOfWeek, $date, $date]
        );
    }

    public static function getForGridByMonth(int $gridId, string $month): array
    {
        $db = App::getInstance()->db();
        return $db->select(
            "SELECT fzs.*, c.full_name_ar AS coach_full_name, a.name_ar AS acad_name
             FROM facility_zone_schedules fzs
             LEFT JOIN coaches c ON c.id = fzs.coach_id
             LEFT JOIN academies a ON a.id = fzs.academy_id
             WHERE fzs.grid_id = ? AND fzs.plan_month = ? AND fzs.is_active = 1
             ORDER BY fzs.day_of_week ASC, fzs.start_time ASC",
            [$gridId, $month]
        );
    }

    public static function getForGridDateAndHour(int $gridId, string $date, string $startTime, string $endTime): array
    {
        $db = App::getInstance()->db();
        $dayOfWeek = (int) date('w', strtotime($date));

        return $db->select(
            "SELECT * FROM facility_zone_schedules
             WHERE grid_id = ? AND day_of_week = ? AND is_active = 1
             AND start_time = ? AND end_time = ?
             AND effective_from <= ? AND (effective_to IS NULL OR effective_to >= ?)
             ORDER BY zone_selection_type ASC",
            [$gridId, $dayOfWeek, $startTime, $endTime, $date, $date]
        );
    }

    public static function getMaxOccupants(string $selectionType): int
    {
        return match ($selectionType) {
            'row'    => 8,
            'column' => 6,
            default  => 5,
        };
    }

    public static function getAllForGrid(int $gridId): array
    {
        $db = App::getInstance()->db();
        return $db->select(
            "SELECT fzs.*, c.full_name_ar AS coach_full_name, a.name_ar AS acad_name
             FROM facility_zone_schedules fzs
             LEFT JOIN coaches c ON c.id = fzs.coach_id
             LEFT JOIN academies a ON a.id = fzs.academy_id
             WHERE fzs.grid_id = ? AND fzs.is_active = 1
             ORDER BY fzs.day_of_week ASC, fzs.start_time ASC",
            [$gridId]
        );
    }

    public static function getDayNames(): array
    {
        return [0 => 'الأحد', 1 => 'الإثنين', 2 => 'الثلاثاء', 3 => 'الأربعاء', 4 => 'الخميس', 5 => 'الجمعة', 6 => 'السبت'];
    }

    public static function getActivityTypes(): array
    {
        return [
            'swimming_lessons' => 'دروس سباحة',
            'academy_session'  => 'حصة أكاديمية',
            'free_swim'        => 'سباحة حرة',
            'lap_swimming'     => 'سباحة (لفات)',
            'competition'      => 'مسابقة / تدريب',
            'private_lesson'   => 'درس خصوصي',
            'training'         => 'تدريب',
            'maintenance'      => 'صيانة',
        ];
    }
}
