@php
    $layoutLabel = config('care.display_layouts.'.$display->layout, $display->layout);
    $isOnline = $display->last_seen_at && $display->last_seen_at->gt(now()->subMinutes(2));
@endphp

<x-app-layout :title="$display->name">
    <div class="flex flex-wrap items-start justify-between gap-4">
        <div>
            <a href="{{ route('care.displays.index') }}" class="text-sm font-medium text-indigo-600 hover:text-indigo-800">← Displays</a>
            <div class="mt-2 flex flex-wrap items-center gap-3">
                <h1 class="text-xl font-semibold text-slate-900">{{ $display->name }}</h1>
                @if ($display->is_active)
                    <span class="inline-flex items-center gap-1.5 rounded-full bg-emerald-50 px-2.5 py-1 text-xs font-semibold text-emerald-700 ring-1 ring-emerald-200">
                        <span class="h-1.5 w-1.5 rounded-full bg-emerald-500"></span>
                        Active
                    </span>
                @else
                    <span class="inline-flex items-center rounded-full bg-slate-100 px-2.5 py-1 text-xs font-semibold text-slate-600 ring-1 ring-slate-200">Inactive</span>
                @endif
                @if ($isOnline)
                    <span class="inline-flex items-center gap-1.5 rounded-full bg-sky-50 px-2.5 py-1 text-xs font-semibold text-sky-700 ring-1 ring-sky-200">
                        <span class="h-1.5 w-1.5 rounded-full bg-sky-500"></span>
                        Display online
                    </span>
                @endif
            </div>
            <p class="mt-1 text-sm text-slate-500">
                {{ $display->branch?->name ?? 'No branch' }}
                · {{ $layoutLabel }} layout
                @if ($display->last_seen_at)
                    · Last seen {{ $display->last_seen_at->diffForHumans() }}
                @endif
            </p>
        </div>
        <div class="flex flex-wrap gap-2">
            @if ($canManage)
                <a href="{{ route('care.displays.edit', $display) }}" class="btn-secondary">Edit display</a>
            @endif
            <a href="{{ $publicUrl }}" target="_blank" rel="noopener" class="btn-primary">Open public display</a>
        </div>
    </div>

    <div class="mt-6 grid gap-4 lg:grid-cols-3">
        <section class="rounded-2xl border border-slate-200 bg-white p-5 lg:col-span-2">
            <h2 class="text-sm font-semibold text-slate-900">Assigned queues</h2>
            <p class="mt-1 text-sm text-slate-500">Tickets from these queues appear on this screen and trigger voice calls.</p>

            @if ($queues->isEmpty())
                <div class="mt-4 rounded-xl border border-dashed border-slate-200 bg-slate-50 px-4 py-8 text-center">
                    <p class="text-sm font-medium text-slate-700">No queues assigned</p>
                    <p class="mt-1 text-sm text-slate-500">All active queues for this branch will be shown. Edit to pick specific queues.</p>
                    @if ($canManage)
                        <a href="{{ route('care.displays.edit', $display) }}" class="btn-secondary mt-4 inline-flex">Assign queues</a>
                    @endif
                </div>
            @else
                <ul class="mt-4 grid gap-3 sm:grid-cols-2">
                    @foreach ($queues as $queue)
                        <li class="flex items-start gap-3 rounded-xl bg-slate-50 px-4 py-3">
                            <span class="mt-0.5 flex h-8 w-8 shrink-0 items-center justify-center rounded-lg bg-white text-xs font-bold text-indigo-700 ring-1 ring-indigo-100">
                                {{ $queue->prefix }}
                            </span>
                            <div class="min-w-0">
                                <p class="truncate font-medium text-slate-900">{{ $queue->name }}</p>
                                <p class="text-xs text-slate-500">
                                    {{ $queue->is_active ? 'Accepting tickets' : 'Inactive' }}
                                </p>
                            </div>
                        </li>
                    @endforeach
                </ul>
            @endif
        </section>

        <section class="space-y-4">
            <div class="rounded-2xl border border-slate-200 bg-white p-5">
                <h2 class="text-sm font-semibold text-slate-900">Display details</h2>
                <dl class="mt-4 space-y-3 text-sm">
                    <div class="flex justify-between gap-4">
                        <dt class="text-slate-500">Organization</dt>
                        <dd class="text-right font-medium text-slate-900">{{ $display->organization?->name ?? '—' }}</dd>
                    </div>
                    <div class="flex justify-between gap-4">
                        <dt class="text-slate-500">Branch</dt>
                        <dd class="text-right font-medium text-slate-900">{{ $display->branch?->name ?? '—' }}</dd>
                    </div>
                    <div class="flex justify-between gap-4">
                        <dt class="text-slate-500">Layout</dt>
                        <dd class="text-right font-medium text-slate-900">{{ $layoutLabel }}</dd>
                    </div>
                    <div class="flex justify-between gap-4">
                        <dt class="text-slate-500">Queues</dt>
                        <dd class="text-right font-medium text-slate-900">{{ $queues->count() }}</dd>
                    </div>
                </dl>
            </div>

            <div
                class="rounded-2xl border border-slate-200 bg-white p-5"
                x-data="{
                    url: @js($publicUrl),
                    copied: false,
                    async copy() {
                        try {
                            await navigator.clipboard.writeText(this.url);
                            this.copied = true;
                            setTimeout(() => this.copied = false, 2000);
                        } catch (e) {
                            window.prompt('Copy this display URL:', this.url);
                        }
                    }
                }"
            >
                <h2 class="text-sm font-semibold text-slate-900">Public display URL</h2>
                <p class="mt-1 text-sm text-slate-500">Open this link on a TV, tablet, or kiosk in your waiting area.</p>
                <div class="mt-4 rounded-xl bg-slate-50 p-3">
                    <p class="break-all font-mono text-xs text-slate-700" x-text="url"></p>
                </div>
                <div class="mt-3 flex flex-wrap gap-2">
                    <button type="button" class="btn-secondary" @click="copy()" x-text="copied ? 'Copied!' : 'Copy URL'"></button>
                    <a href="{{ $publicUrl }}" target="_blank" rel="noopener" class="btn-secondary">Preview</a>
                </div>
            </div>
        </section>
    </div>

    <section class="mt-4 overflow-hidden rounded-2xl bg-slate-900 shadow-sm">
        <div class="flex items-center justify-between border-b border-white/10 px-5 py-3">
            <p class="text-xs font-semibold uppercase tracking-widest text-slate-400">Live preview</p>
            <span class="inline-flex items-center gap-1.5 text-xs text-slate-400">
                <span class="qms-display__live-dot h-1.5 w-1.5 rounded-full bg-emerald-400"></span>
                Patient view
            </span>
        </div>
        <div class="relative aspect-video w-full bg-slate-950">
            <iframe
                src="{{ $publicUrl }}"
                title="Display preview"
                class="absolute inset-0 h-full w-full border-0"
                loading="lazy"
            ></iframe>
        </div>
        <p class="border-t border-white/10 px-5 py-3 text-xs text-slate-500">
            Voice announcements require a tap on the display screen once per browser session.
        </p>
    </section>
</x-app-layout>
