@php
    use App\Support\EventsSourceLink;

    $eventsUrls = EventsSourceLink::urls($room);
    $isLinked = EventsSourceLink::isLinked($room);
    $eventTitle = EventsSourceLink::eventTitle($room);
    $eventsConfigured = EventsSourceLink::isApiConfigured();
    $createEventUrl = EventsSourceLink::createEventUrl($room);
    $kind = $room->isConference() ? 'conference' : 'webinar';
@endphp

<div class="mt-6 rounded-2xl border {{ $isLinked ? 'border-indigo-200 bg-indigo-50/40' : 'border-amber-200 bg-amber-50/50' }} p-6">
    <div class="flex flex-wrap items-start justify-between gap-3">
        <div>
            <h2 class="text-sm font-medium text-slate-900">Ladill Events</h2>
            @if ($isLinked && $eventTitle)
                <p class="mt-1 text-sm text-slate-600">Linked to <span class="font-medium">{{ $eventTitle }}</span></p>
            @elseif ($isLinked)
                <p class="mt-1 text-sm text-slate-600">Linked to event #{{ EventsSourceLink::eventId($room) }}</p>
            @else
                <p class="mt-1 text-sm text-slate-600">
                    Registration, invitations, speakers, and QR codes for this {{ $kind }} are managed in Ladill Events.
                    Link a virtual or hybrid event to enable attendee registration.
                </p>
            @endif
        </div>
        @if ($isLinked)
            <form method="POST" action="{{ route('meet.events.link.destroy', $room) }}" onsubmit="return confirm('Remove the Events link? Registration settings stay in Events but this room will no longer show as linked.');">
                @csrf
                @method('DELETE')
                <button type="submit" class="text-xs font-medium text-slate-500 hover:text-slate-700">Unlink</button>
            </form>
        @endif
    </div>

    @if (! $isLinked && ! $eventsConfigured)
        <div class="mt-4 rounded-lg border border-amber-300/80 bg-amber-100/60 px-4 py-3 text-sm text-amber-950">
            <p class="font-medium">Events linking is not configured on this Meet instance.</p>
            <p class="mt-1 text-xs leading-relaxed text-amber-900/90">
                Set <span class="font-mono">MEET_API_KEY_EVENTS</span> in Meet and the matching
                <span class="font-mono">EVENTS_API_KEY_MEET</span> in Events so Meet can list and link your events.
                You can still create an event in Events using the button below.
            </p>
        </div>
    @endif

    @if ($isLinked)
        <div class="mt-4 grid gap-2 sm:grid-cols-2">
            @if ($eventsUrls['registration'])
                <div class="rounded-lg border border-white/80 bg-white p-3">
                    <p class="text-xs font-semibold uppercase tracking-wide text-slate-500">Registration page</p>
                    <div class="mt-2 flex gap-2">
                        <input type="text" readonly value="{{ $eventsUrls['registration'] }}" class="min-w-0 flex-1 rounded border-slate-200 bg-slate-50 text-xs font-mono">
                        <x-copy-button :text="$eventsUrls['registration']" class="btn-secondary btn-secondary-sm shrink-0" />
                    </div>
                </div>
            @endif
            @if ($eventsUrls['admin'])
                <a href="{{ $eventsUrls['admin'] }}" target="_blank" rel="noopener" class="rounded-lg border border-white/80 bg-white p-3 text-sm font-medium text-indigo-700 hover:bg-indigo-50">
                    Attendees & check-in →
                </a>
            @endif
            @if ($eventsUrls['edit'])
                <a href="{{ $eventsUrls['edit'] }}" target="_blank" rel="noopener" class="rounded-lg border border-white/80 bg-white p-3 text-sm font-medium text-indigo-700 hover:bg-indigo-50">
                    Event settings (tickets, branding) →
                </a>
            @endif
            @if ($eventsUrls['comms'])
                <a href="{{ $eventsUrls['comms'] }}" target="_blank" rel="noopener" class="rounded-lg border border-white/80 bg-white p-3 text-sm font-medium text-indigo-700 hover:bg-indigo-50">
                    Bulk invitations & comms →
                </a>
            @endif
            @if ($eventsUrls['qr'])
                <a href="{{ $eventsUrls['qr'] }}" target="_blank" rel="noopener" class="rounded-lg border border-white/80 bg-white p-3 text-sm font-medium text-indigo-700 hover:bg-indigo-50">
                    Event QR code →
                </a>
            @endif
            @if ($eventsUrls['badges'])
                <a href="{{ $eventsUrls['badges'] }}" target="_blank" rel="noopener" class="rounded-lg border border-white/80 bg-white p-3 text-sm font-medium text-indigo-700 hover:bg-indigo-50">
                    Badges & printing →
                </a>
            @endif
        </div>
        <p class="mt-3 text-xs text-slate-500">Programme speakers and panelists are configured on the event programme in Events and sync to this session when linked.</p>
    @else
        <div class="mt-4 flex flex-wrap gap-3">
            @if ($eventsConfigured)
                <a href="{{ route('meet.events.link', $room) }}" class="btn-primary btn-primary-sm">Link to Ladill Events</a>
            @else
                <span class="btn-primary btn-primary-sm cursor-not-allowed opacity-50" title="Configure MEET_API_KEY_EVENTS to enable linking">Link to Ladill Events</span>
            @endif
            <a href="{{ $createEventUrl }}" target="_blank" rel="noopener" class="btn-secondary btn-secondary-sm">Create event in Events</a>
        </div>
    @endif
</div>
