@php use App\Support\EventsSourceLink; @endphp
<x-app-layout :title="'Link to Ladill Events · '.$room->title">
    <div class="mx-auto max-w-xl">
        <a href="{{ $room->isWebinar() ? route('meet.webinars.show', $room) : route('meet.conferences.show', $room) }}" class="text-sm font-medium text-indigo-600 hover:text-indigo-800">← Back</a>
        <h1 class="mt-4 text-xl font-semibold text-slate-900">Link to Ladill Events</h1>
        <p class="mt-1 text-sm text-slate-500">
            Choose a virtual or hybrid event. Registration, bulk invitations, speakers, and QR codes will be managed in Events.
        </p>

        @if ($errors->any())
            <div class="mt-4 rounded-lg border border-red-200 bg-red-50 px-4 py-3 text-sm text-red-700">
                {{ $errors->first() }}
            </div>
        @endif

        @if ($events === [])
            <div class="mt-6 rounded-2xl border border-slate-200 bg-white p-6 text-sm text-slate-600">
                <p>No virtual or hybrid events found in your Ladill Events account.</p>
                <a href="{{ EventsSourceLink::createEventUrl($room) }}" target="_blank" rel="noopener" class="mt-4 inline-flex btn-primary btn-primary-sm">Create event in Events</a>
            </div>
        @else
            <form method="POST" action="{{ route('meet.events.link.store', $room) }}" class="mt-6 space-y-5">
                @csrf

                <div class="rounded-2xl border border-slate-200 bg-white p-6">
                    <label class="block text-sm font-medium text-slate-700">Event</label>
                    <select name="event_id" required class="mt-2 w-full rounded-lg border-slate-300 text-sm">
                        <option value="">Select an event…</option>
                        @foreach ($events as $event)
                            <option value="{{ $event['id'] }}" @selected(old('event_id') == $event['id'])>
                                {{ $event['title'] }} ({{ ucfirst($event['format']) }})
                            </option>
                        @endforeach
                    </select>
                </div>

                <div class="rounded-2xl border border-slate-200 bg-white p-6">
                    <label class="block text-sm font-medium text-slate-700">Virtual session slot (optional)</label>
                    <p class="mt-1 text-xs text-slate-500">Pick an empty session matching this {{ $room->isConference() ? 'conference' : 'webinar' }}, or leave blank to attach to the first available {{ $expectedType === 'town_hall' ? 'conference' : 'webinar' }} slot.</p>
                    <select name="virtual_session_id" class="mt-2 w-full rounded-lg border-slate-300 text-sm">
                        <option value="">Auto-assign slot</option>
                        @foreach ($events as $event)
                            @foreach ($event['virtual_sessions'] ?? [] as $session)
                                @php
                                    $typeMatch = ($session['meet_room_type'] ?? '') === $expectedType;
                                    $available = empty($session['meet_room_uuid']);
                                @endphp
                                @if ($typeMatch)
                                    <option value="{{ $session['id'] }}" data-event="{{ $event['id'] }}" @selected(old('virtual_session_id') === $session['id'])>
                                        [{{ $event['title'] }}] {{ $session['title'] ?: 'Untitled session' }}{{ $available ? '' : ' (linked)' }}
                                    </option>
                                @endif
                            @endforeach
                        @endforeach
                    </select>
                </div>

                <button type="submit" class="btn-primary btn-primary-lg w-full">Link event</button>
            </form>
        @endif
    </div>
</x-app-layout>
