<x-app-layout title="{{ $room->title }}">
    <div class="mx-auto max-w-2xl">
        <div>
            <p class="text-xs font-semibold uppercase tracking-wide text-violet-600">Conference</p>
            <h1 class="text-2xl font-semibold text-slate-900">{{ $room->title }}</h1>
            <p class="mt-1 text-sm text-slate-500">{{ config('meet.room_statuses')[$room->status] ?? $room->status }}</p>
            @if(! empty($eventsEventUrl))
                <a href="{{ $eventsEventUrl }}" target="_blank" rel="noopener" class="mt-2 inline-flex text-sm font-medium text-violet-600 hover:text-violet-800">
                    View in Ladill Events →
                </a>
            @endif
        </div>

        @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

        <div class="mt-6 rounded-2xl border border-slate-200 bg-white p-6">
            <h2 class="text-sm font-medium text-slate-700">Join link</h2>
            <div class="mt-2 flex gap-2">
                <input type="text" readonly value="{{ $room->joinUrl() }}" class="flex-1 rounded-lg border-slate-300 bg-slate-50 text-sm font-mono">
                <x-copy-button :text="$room->joinUrl()" class="btn-secondary btn-secondary-sm shrink-0" />
            </div>

            <div class="btn-group mt-4">
                @if ($room->status === 'live' && $room->activeSession())
                    <a href="{{ route('meet.join', $room) }}" class="btn-primary btn-primary-sm">Join conference</a>
                @elseif ($room->status === 'scheduled')
                    <form method="POST" action="{{ route('meet.conferences.start', $room) }}" class="inline">
                        @csrf
                        <button type="submit" class="btn-primary btn-primary-sm">Start conference</button>
                    </form>
                @elseif ($room->canRestart())
                    <form method="POST" action="{{ route('meet.conferences.start', $room) }}" class="inline">
                        @csrf
                        <button type="submit" class="btn-primary btn-primary-sm">{{ $room->restartLabel() }}</button>
                    </form>
                @endif
                <a href="{{ route('meet.rooms.ical', $room) }}" class="btn-secondary btn-secondary-sm">Download iCal</a>
                <a href="{{ route('meet.rooms.qr', $room) }}" target="_blank" class="btn-secondary btn-secondary-sm">QR code</a>
                @if ($room->sessions->isNotEmpty())
                    <a href="{{ route('meet.rooms.attendance', $room) }}" class="btn-secondary btn-secondary-sm">Attendance CSV</a>
                @endif
                <a href="{{ route('meet.invitations.index', $room) }}" class="btn-secondary btn-secondary-sm">Manage invitations</a>
            </div>

            @if ($room->passcode)
                <p class="mt-3 text-sm text-slate-600">Passcode: <span class="font-mono font-medium">{{ $room->passcode }}</span></p>
            @endif

            @if ($room->scheduled_at)
                <p class="mt-3 text-sm text-slate-600">
                    Scheduled: {{ $room->scheduled_at->timezone($room->safeTimezone())->format('M j, Y g:i A T') }}
                </p>
            @endif

            @if ($room->setting('green_room'))
                <p class="mt-3 text-sm text-violet-700">Green room is on — when you start, you and assigned speakers rehearse before going live to the audience.</p>
            @endif
        </div>

        <div class="mt-6 rounded-2xl border border-slate-200 bg-white p-6">
            <h2 class="text-sm font-medium text-slate-700">Speakers</h2>
            <p class="mt-1 text-xs text-slate-500">Team members on stage and external speakers invited by email. You are always the host.</p>

            @if ($speakers->isNotEmpty() || $room->invitations->where('role', 'panelist')->isNotEmpty())
                <div class="mt-4 space-y-3">
                    @if ($speakers->isNotEmpty())
                        <div>
                            <h3 class="text-xs font-semibold uppercase tracking-wide text-slate-500">Assigned speakers</h3>
                            <ul class="mt-2 space-y-1 text-sm text-slate-600">
                                @foreach ($speakers as $speaker)
                                    @php $user = $speakerUsers->get($speaker->user_ref); @endphp
                                    <li>{{ $user?->name ?? $speaker->user_ref }}</li>
                                @endforeach
                            </ul>
                        </div>
                    @endif

                    @if ($room->invitations->where('role', 'panelist')->isNotEmpty())
                        <div>
                            <h3 class="text-xs font-semibold uppercase tracking-wide text-slate-500">Invited speakers</h3>
                            <ul class="mt-2 space-y-1 text-sm text-slate-600">
                                @foreach ($room->invitations->where('role', 'panelist') as $invite)
                                    <li>
                                        {{ $invite->display_name ?? $invite->email }}
                                        <span class="text-slate-400">· {{ ucfirst($invite->status) }}</span>
                                    </li>
                                @endforeach
                            </ul>
                        </div>
                    @endif
                </div>
            @else
                <p class="mt-3 text-sm text-slate-500">No speakers assigned yet.</p>
            @endif

            @if ($canManageSpeakers)
                <form method="POST" action="{{ route('meet.conferences.speakers.update', $room) }}" class="mt-5 space-y-4 border-t border-slate-100 pt-5">
                    @csrf

                    <div>
                        <label class="block text-sm font-medium text-slate-700">Invite speakers by email</label>
                        <input type="text" name="invite_emails" value="{{ old('invite_emails') }}" placeholder="speaker@example.com, another@example.com"
                               class="mt-1 w-full rounded-lg border-slate-300 text-sm">
                        <p class="mt-1 text-xs text-slate-500">Comma-separated. Invitations are sent as speaker (panelist) roles.</p>
                    </div>

                    @if ($members->isNotEmpty())
                        <div>
                            <label class="block text-sm font-medium text-slate-700">Assigned speakers</label>
                            <div class="mt-2 max-h-40 space-y-2 overflow-y-auto rounded-lg border border-slate-200 p-3">
                                @foreach ($members as $member)
                                    @php $user = $memberUsers->get($member->user_ref); @endphp
                                    <label class="flex items-center gap-2 text-sm">
                                        <input type="checkbox" name="presenter_refs[]" value="{{ $member->user_ref }}"
                                               @checked(in_array($member->user_ref, old('presenter_refs', $presenterRefs))) class="rounded border-slate-300">
                                        <span>{{ $user?->name ?? $member->user_ref }}</span>
                                    </label>
                                @endforeach
                            </div>
                        </div>
                    @endif

                    <label class="flex items-center gap-2 text-sm text-slate-700">
                        <input type="checkbox" name="panel_discussions" value="1"
                               @checked(old('panel_discussions', $room->setting('panel_discussions'))) class="rounded border-slate-300">
                        Enable panel discussions (multi-speaker stage layout)
                    </label>

                    <button type="submit" class="btn-primary btn-primary-sm">Save speakers</button>
                </form>
            @endif
        </div>

        @include('meet.partials.programme-agenda', ['room' => $room])

        @if ($room->sessions->isNotEmpty())
            <div class="mt-6 rounded-2xl border border-slate-200 bg-white p-6">
                <h2 class="text-sm font-medium text-slate-700">Sessions</h2>
                <ul class="mt-3 divide-y divide-slate-100 text-sm">
                    @foreach ($room->sessions as $session)
                        <li class="flex items-center justify-between py-2">
                            <span>{{ $session->started_at?->format('M j, Y g:i A') ?? 'Pending' }}</span>
                            <span class="text-slate-500">{{ $session->peak_participants }} participants · {{ config('meet.session_statuses')[$session->status] ?? $session->status }}</span>
                        </li>
                    @endforeach
                </ul>
            </div>
        @endif
    </div>
</x-app-layout>
