@extends('website.layout')

@section('content')
<div class="site-section">
    <div class="max-w-5xl mx-auto px-4 sm:px-6 lg:px-8 py-12">
        {{-- Back --}}
        <a href="{{ route('website.events.index', $academy->slug) }}"
           class="inline-flex items-center gap-1 text-sm text-gray-500 hover:text-[var(--site-accent)] mb-6 transition-colors">
            <svg class="w-4 h-4 rotate-180" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"/>
            </svg>
            {{ __('العودة للإيفنتات') }}
        </a>

        {{-- Cover Image (full-bleed, edge-to-edge) --}}
        @if($event->cover)
            <div class="relative mb-8 -mx-4 sm:-mx-6 lg:-mx-8">
                <img src="{{ $event->cover->url }}"
                     alt="{{ $event->title }}"
                     class="w-full h-auto object-contain">
            </div>
        @endif

        {{-- Header --}}
        <div class="mb-8">
            @php
                $statusColors = [
                    'published' => 'bg-green-100 text-green-800',
                    'registration_closed' => 'bg-yellow-100 text-yellow-800',
                    'in_progress' => 'bg-blue-100 text-blue-800',
                    'completed' => 'bg-gray-100 text-gray-800',
                ];
                $statusLabels = [
                    'published' => 'التسجيل مفتوح',
                    'registration_closed' => 'التسجيل مغلق',
                    'in_progress' => 'جارٍ الآن',
                    'completed' => 'انتهى',
                ];
                $typeLabels = [
                    'tournament' => 'بطولة',
                    'camp' => 'معسكر',
                    'competition' => 'مسابقة',
                    'open_day' => 'يوم مفتوح',
                    'workshop' => 'ورشة عمل',
                    'friendly' => 'ودية',
                    'exhibition' => 'معرض',
                    'other' => 'أخرى',
                ];
            @endphp

            <div class="flex flex-wrap items-center gap-3 mb-4">
                <span class="inline-block px-3 py-1 text-xs font-bold rounded-full {{ $statusColors[$event->status->value ?? $event->status] ?? 'bg-gray-100 text-gray-800' }}">
                    {{ $statusLabels[$event->status->value ?? $event->status] ?? $event->status }}
                </span>
                <span class="inline-block px-3 py-1 text-xs font-medium rounded-full bg-[var(--site-accent)]/10 text-[var(--site-accent)]">
                    {{ $typeLabels[$event->type->value ?? $event->type] ?? $event->type }}
                </span>
            </div>

            <h1 class="text-3xl sm:text-4xl font-bold text-gray-900" style="font-family: var(--site-heading-font);">
                {{ $event->title }}
            </h1>
        </div>

        {{-- Info Cards --}}
        <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4 mb-10">
            {{-- Date --}}
            <div class="bg-white border border-gray-100 rounded-xl p-4 shadow-sm">
                <div class="flex items-center gap-3">
                    <div class="w-10 h-10 rounded-lg bg-blue-50 flex items-center justify-center">
                        <svg class="w-5 h-5 text-blue-600" 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>
                    </div>
                    <div>
                        <p class="text-xs text-gray-500">{{ __('التاريخ') }}</p>
                        <p class="text-sm font-semibold text-gray-900" dir="ltr">{{ $event->starts_at->translatedFormat('d M Y') }}</p>
                    </div>
                </div>
            </div>

            {{-- Time --}}
            <div class="bg-white border border-gray-100 rounded-xl p-4 shadow-sm">
                <div class="flex items-center gap-3">
                    <div class="w-10 h-10 rounded-lg bg-purple-50 flex items-center justify-center">
                        <svg class="w-5 h-5 text-purple-600" 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>
                    </div>
                    <div>
                        <p class="text-xs text-gray-500">{{ __('الوقت') }}</p>
                        <p class="text-sm font-semibold text-gray-900" dir="ltr">{{ $event->starts_at->format('h:i A') }}</p>
                    </div>
                </div>
            </div>

            {{-- Location --}}
            <div class="bg-white border border-gray-100 rounded-xl p-4 shadow-sm">
                <div class="flex items-center gap-3">
                    <div class="w-10 h-10 rounded-lg bg-green-50 flex items-center justify-center">
                        <svg class="w-5 h-5 text-green-600" 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"/>
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 11a3 3 0 11-6 0 3 3 0 016 0z"/>
                        </svg>
                    </div>
                    <div>
                        <p class="text-xs text-gray-500">{{ __('المكان') }}</p>
                        <p class="text-sm font-semibold text-gray-900 line-clamp-1">{{ $event->location_name ?? $event->facility?->name_ar ?? '-' }}</p>
                    </div>
                </div>
            </div>

            {{-- Capacity --}}
            <div class="bg-white border border-gray-100 rounded-xl p-4 shadow-sm">
                <div class="flex items-center gap-3">
                    <div class="w-10 h-10 rounded-lg bg-orange-50 flex items-center justify-center">
                        <svg class="w-5 h-5 text-orange-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0z"/>
                        </svg>
                    </div>
                    <div>
                        <p class="text-xs text-gray-500">{{ __('المقاعد') }}</p>
                        <p class="text-sm font-semibold text-gray-900">
                            @if($event->max_capacity)
                                {{ $event->registrations_count }}/{{ $event->max_capacity }}
                            @else
                                {{ __('مفتوح') }}
                            @endif
                        </p>
                    </div>
                </div>
            </div>
        </div>

        <div class="grid grid-cols-1 lg:grid-cols-3 gap-8">
            {{-- Main Content --}}
            <div class="lg:col-span-2 space-y-8">
                {{-- Description --}}
                @if($event->description)
                    <div class="prose prose-lg max-w-none text-gray-700 leading-relaxed">
                        {!! nl2br(e($event->description)) !!}
                    </div>
                @endif

                {{-- Gallery --}}
                @if($event->gallery && $event->gallery->count())
                    <div>
                        <h3 class="text-lg font-bold text-gray-900 mb-4">{{ __('معرض الصور') }}</h3>
                        <div class="grid grid-cols-2 sm:grid-cols-3 gap-3">
                            @foreach($event->gallery as $media)
                                <div class="rounded-xl overflow-hidden bg-gray-100">
                                    <img src="{{ $media->url }}" alt="" class="w-full h-auto object-contain hover:scale-105 transition-transform duration-300">
                                </div>
                            @endforeach
                        </div>
                    </div>
                @endif

                {{-- Map Locations --}}
                @if(!empty($event->map_locations))
                    <div>
                        <h3 class="text-lg font-bold text-gray-900 mb-4">{{ __('الموقع على الخريطة') }}</h3>
                        <div class="space-y-4">
                            @foreach($event->map_locations as $loc)
                                @if(!empty($loc['lat']) && !empty($loc['lng']))
                                    <div class="rounded-xl overflow-hidden border border-gray-200">
                                        @if(!empty($loc['title']))
                                            <div class="bg-gray-50 px-4 py-2 border-b border-gray-200">
                                                <p class="text-sm font-medium text-gray-700">{{ $loc['title'] }}</p>
                                                @if(!empty($loc['address']))
                                                    <p class="text-xs text-gray-500">{{ $loc['address'] }}</p>
                                                @endif
                                            </div>
                                        @endif
                                        <iframe
                                            src="https://maps.google.com/maps?q={{ $loc['lat'] }},{{ $loc['lng'] }}&z=15&output=embed"
                                            width="100%" height="250" style="border:0;" allowfullscreen loading="lazy"
                                            referrerpolicy="no-referrer-when-downgrade" class="w-full"></iframe>
                                    </div>
                                @endif
                            @endforeach
                        </div>
                    </div>
                @endif
            </div>

            {{-- Sidebar: Registration Form --}}
            <div class="lg:col-span-1">
                <div class="sticky top-6">
                    <div class="bg-white border border-gray-200 rounded-2xl p-6 shadow-sm">
                        <h3 class="text-lg font-bold text-gray-900 mb-4">{{ __('التسجيل في الإيفنت') }}</h3>

                        @if($event->isRegistrationOpen())
                            @if($event->max_capacity)
                                <div class="mb-4">
                                    @php $remaining = $event->spotsRemaining(); @endphp
                                    <div class="flex justify-between text-sm mb-1">
                                        <span class="text-gray-500">{{ __('المقاعد المتبقية') }}</span>
                                        <span class="font-semibold {{ $remaining <= 5 ? 'text-red-600' : 'text-green-600' }}">{{ $remaining }}</span>
                                    </div>
                                    <div class="w-full bg-gray-200 rounded-full h-2">
                                        <div class="h-2 rounded-full {{ $remaining <= 5 ? 'bg-red-500' : 'bg-green-500' }}" style="width: {{ min(100, ($event->registrations_count / $event->max_capacity) * 100) }}%"></div>
                                    </div>
                                </div>
                            @endif

                            @livewire('public.event-registration-form', ['event' => $event])
                        @elseif(!$event->hasCapacity())
                            <div class="text-center py-6">
                                <div class="w-12 h-12 mx-auto bg-red-100 rounded-full flex items-center justify-center mb-3">
                                    <svg class="w-6 h-6 text-red-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M18.364 18.364A9 9 0 005.636 5.636m12.728 12.728A9 9 0 015.636 5.636m12.728 12.728L5.636 5.636"/>
                                    </svg>
                                </div>
                                <p class="text-red-700 font-semibold">{{ __('تم اكتمال العدد') }}</p>
                            </div>
                        @else
                            <div class="text-center py-6">
                                <div class="w-12 h-12 mx-auto bg-yellow-100 rounded-full flex items-center justify-center mb-3">
                                    <svg class="w-6 h-6 text-yellow-600" 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>
                                </div>
                                <p class="text-yellow-700 font-semibold">{{ __('التسجيل مغلق حالياً') }}</p>
                                @if($event->registration_opens_at && $event->registration_opens_at->isFuture())
                                    <p class="text-sm text-gray-500 mt-2">
                                        {{ __('يفتح التسجيل:') }}
                                        <span dir="ltr">{{ $event->registration_opens_at->translatedFormat('d M Y - h:i A') }}</span>
                                    </p>
                                @endif
                            </div>
                        @endif
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
