@php
    $schedule = json_decode($section->getSetting('schedule_data', '[]'), true) ?? [];
    $days = ['السبت', 'الأحد', 'الاثنين', 'الثلاثاء', 'الأربعاء', 'الخميس', 'الجمعة'];
    $groupedByDay = collect($schedule)->groupBy('day');
    $activeDays = $days;
    $todayIdx = (int) now()->dayOfWeek;
    $arabicDayMap = [6 => 'السبت', 0 => 'الأحد', 1 => 'الاثنين', 2 => 'الثلاثاء', 3 => 'الأربعاء', 4 => 'الخميس', 5 => 'الجمعة'];
    $todayArabic = $arabicDayMap[$todayIdx] ?? 'السبت';
@endphp

<div class="site-section site-section--alt">
    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
        <div class="text-center mb-12 reveal">
            <h2 class="section-title mx-auto">{{ $section->title ?? 'مواعيد التدريب' }}</h2>
            @if($section->subtitle)
                <p class="mt-4 text-gray-500 text-lg">{{ $section->subtitle }}</p>
            @endif
        </div>

        @if(count($schedule))
            <div class="reveal" x-data="{ activeDay: '{{ $todayArabic }}' }">
                {{-- Day Tabs --}}
                <div class="flex overflow-x-auto gap-2 pb-4 mb-6 scrollbar-hide">
                    <button type="button" @click="activeDay = ''" class="shrink-0 px-4 py-2.5 rounded-xl text-sm font-medium transition-all"
                            :class="activeDay === '' ? 'bg-[var(--site-accent)] text-white shadow-lg shadow-[var(--site-accent)]/20' : 'bg-white text-gray-600 hover:bg-gray-100 border border-gray-200'">
                        الكل
                    </button>
                    @foreach($days as $day)
                        @if($groupedByDay->has($day))
                            <button type="button" @click="activeDay = '{{ $day }}'" class="shrink-0 px-4 py-2.5 rounded-xl text-sm font-medium transition-all"
                                    :class="activeDay === '{{ $day }}' ? 'bg-[var(--site-accent)] text-white shadow-lg shadow-[var(--site-accent)]/20' : 'bg-white text-gray-600 hover:bg-gray-100 border border-gray-200'">
                                {{ $day }}
                                @if($day === $todayArabic)
                                    <span class="inline-block w-1.5 h-1.5 rounded-full bg-green-400 ms-1 animate-pulse"></span>
                                @endif
                            </button>
                        @endif
                    @endforeach
                </div>

                {{-- Schedule Cards --}}
                <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
                    @foreach($schedule as $index => $row)
                        <div x-show="activeDay === '' || activeDay === '{{ $row['day'] ?? '' }}'"
                             x-transition:enter="transition ease-out duration-200"
                             x-transition:enter-start="opacity-0 scale-95"
                             x-transition:enter-end="opacity-100 scale-100"
                             class="bg-white rounded-xl border border-gray-100 p-4 shadow-sm hover:shadow-md transition-shadow group">
                            <div class="flex items-start gap-3">
                                {{-- Time Badge --}}
                                <div class="shrink-0 w-16 text-center">
                                    <div class="bg-[var(--site-accent)]/5 rounded-lg p-2 group-hover:bg-[var(--site-accent)]/10 transition-colors">
                                        <svg class="w-5 h-5 mx-auto text-[var(--site-accent)] mb-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"/>
                                        </svg>
                                        <span class="text-[11px] font-bold text-[var(--site-accent)]" dir="ltr">{{ $row['time'] ?? '' }}</span>
                                    </div>
                                </div>
                                {{-- Info --}}
                                <div class="flex-1 min-w-0">
                                    <h4 class="text-gray-900 font-bold text-sm">{{ $row['activity'] ?? '' }}</h4>
                                    <div class="flex items-center gap-3 mt-2">
                                        <span class="inline-flex items-center gap-1 text-xs text-gray-500">
                                            <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z"/></svg>
                                            {{ $row['day'] ?? '' }}
                                        </span>
                                        @if(!empty($row['branch']))
                                            <span class="inline-flex items-center gap-1 text-xs text-gray-500">
                                                <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17.657 16.657L13.414 20.9a1.998 1.998 0 01-2.827 0l-4.244-4.243a8 8 0 1111.314 0z"/></svg>
                                                {{ $row['branch'] }}
                                            </span>
                                        @endif
                                    </div>
                                </div>
                            </div>
                        </div>
                    @endforeach
                </div>

                {{-- Today highlight --}}
                <div class="mt-6 text-center" x-show="activeDay === '{{ $todayArabic }}'">
                    <p class="text-xs text-gray-400 flex items-center justify-center gap-1">
                        <span class="w-2 h-2 rounded-full bg-green-400 animate-pulse"></span>
                        اليوم — {{ $todayArabic }}
                    </p>
                </div>
            </div>
        @else
            <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
                <div class="p-4 space-y-3">
                    @for($i = 0; $i < 4; $i++)
                        <div class="flex items-center gap-4">
                            <div class="skeleton-block h-4 w-16"></div>
                            <div class="skeleton-block h-4 w-24"></div>
                            <div class="skeleton-block h-4 w-20"></div>
                            <div class="skeleton-block h-4 w-16"></div>
                        </div>
                    @endfor
                </div>
            </div>
            <div class="empty-state mt-4">
                <p class="text-sm">لم تُضف مواعيد بعد</p>
            </div>
        @endif
    </div>
</div>
