{{--
  Single patient-flow card for queue-board stages.

  Expected: $appointment, $stage (waiting|called|in_care|done),
  $canConsult, $queueIntegration, $startRouteName, $openInCareUrl (callable)

  Open (view workspace / form) is available whenever a destination URL exists —
  not gated by canConsult. Start / Call again remain consult-manage only.
--}}
@php
    $stage = $stage ?? 'waiting';
    $ticketStatus = $appointment->queue_ticket_status ?? null;
    $isCalled = in_array($ticketStatus, ['called', 'serving'], true);
    $openUrl = isset($openInCareUrl) ? $openInCareUrl($appointment) : null;
    $canConsult = $canConsult ?? false;
@endphp
<article
    @if ($openUrl)
        role="link"
        tabindex="0"
        data-href="{{ $openUrl }}"
        onclick="if (! event.target.closest('a, button, form, input, select, textarea, label')) { window.location.href = this.dataset.href }"
        onkeydown="if ((event.key === 'Enter' || event.key === ' ') && ! event.target.closest('a, button, form, input, select, textarea, label')) { event.preventDefault(); window.location.href = this.dataset.href }"
    @endif
    @class([
        'group relative flex gap-3 rounded-xl border px-3 py-3 transition',
        'cursor-pointer hover:shadow-sm' => (bool) $openUrl,
        'border-indigo-200 bg-indigo-50/70 ring-1 ring-indigo-100' => $stage === 'called' || ($stage === 'waiting' && $isCalled),
        'border-emerald-200 bg-emerald-50/60' => $stage === 'in_care',
        'border-slate-100 bg-slate-50/80' => $stage === 'waiting' && ! $isCalled,
        'border-slate-100 bg-white opacity-90' => $stage === 'done',
    ])
>
    <div class="min-w-0 flex-1 space-y-1.5">
        @if (! empty($queueIntegration['enabled']) && $appointment->queue_ticket_number)
            @include('care.partials.queue-ticket', [
                'ticketNumber' => $appointment->queue_ticket_number,
                'ticketStatus' => $ticketStatus,
                'destination' => $appointment->queue_destination,
                'staffDisplayName' => $appointment->queue_staff_display_name,
                'showAssignment' => $stage !== 'in_care' && $stage !== 'done',
                'prominent' => true,
            ])
        @elseif (! empty($queueIntegration['enabled']) && ($appointment->queue_routing_status ?? '') === 'unresolved')
            <span class="inline-flex rounded-md bg-amber-100 px-2 py-0.5 text-[10px] font-semibold uppercase tracking-wide text-amber-800">Unassigned</span>
        @elseif ($appointment->queue_position)
            <span class="inline-flex h-8 min-w-[2rem] items-center justify-center rounded-lg bg-slate-200/80 px-2 font-mono text-sm font-bold text-slate-700">{{ $appointment->queue_position }}</span>
        @endif

        <div class="min-w-0">
            <p class="truncate text-sm font-semibold text-slate-900">{{ $appointment->patient?->fullName() ?? 'Patient' }}</p>
            <p class="mt-0.5 truncate text-xs text-slate-500">
                @if ($stage === 'in_care' || $stage === 'done')
                    {{ $appointment->practitioner?->name ?? 'Unassigned' }}
                    @if ($appointment->patient?->patient_number)
                        <span class="text-slate-300"> · </span>{{ $appointment->patient->patient_number }}
                    @endif
                @else
                    {{ $appointment->patient?->patient_number ?? '—' }}
                    @if ($appointment->reason)
                        <span class="text-slate-300"> · </span>{{ $appointment->reason }}
                    @endif
                @endif
            </p>
        </div>
    </div>

    <div class="relative z-10 flex shrink-0 flex-col items-end justify-center gap-1.5" @click.stop>
        @if ($stage === 'waiting' || $stage === 'called')
            @if (! empty($queueIntegration['enabled']) && $appointment->queue_ticket_uuid && $isCalled && $canConsult)
                <form method="POST" action="{{ route('care.queue.recall', $appointment) }}">
                    @csrf
                    <button type="submit" class="rounded-lg border border-indigo-200 bg-white px-2.5 py-1 text-[11px] font-medium text-indigo-700 hover:bg-indigo-50">Call again</button>
                </form>
            @endif
            @if ($canConsult)
                <form method="POST" action="{{ route($startRouteName, $appointment) }}">
                    @csrf
                    <button type="submit" @class([
                        'rounded-lg px-2.5 py-1.5 text-xs font-semibold text-white',
                        'bg-indigo-600 hover:bg-indigo-700' => $isCalled,
                        'bg-slate-800 hover:bg-slate-900' => ! $isCalled,
                    ])>
                        {{ ! empty($queueIntegration['enabled']) && $ticketStatus === 'called' ? 'Serve & start' : 'Start' }}
                    </button>
                </form>
            @endif
            @if ($openUrl)
                <a href="{{ $openUrl }}" @class([
                    'rounded-lg px-2.5 py-1.5 text-xs font-semibold',
                    'bg-indigo-600 text-white hover:bg-indigo-700' => ! $canConsult,
                    'border border-slate-200 bg-white text-slate-700 hover:bg-slate-50' => $canConsult,
                ])>Open</a>
            @endif
        @elseif ($stage === 'in_care')
            @if (! empty($queueIntegration['enabled']) && $appointment->queue_ticket_uuid && $isCalled && $canConsult)
                <form method="POST" action="{{ route('care.queue.recall', $appointment) }}">
                    @csrf
                    <button type="submit" class="rounded-lg border border-indigo-200 bg-white px-2.5 py-1 text-[11px] font-medium text-indigo-700 hover:bg-indigo-50">Call again</button>
                </form>
            @endif
            @if ($openUrl)
                <a href="{{ $openUrl }}" class="rounded-lg bg-emerald-600 px-2.5 py-1.5 text-xs font-semibold text-white hover:bg-emerald-700">Open</a>
            @endif
        @elseif ($stage === 'done' && $openUrl)
            <a href="{{ $openUrl }}" class="rounded-lg border border-slate-200 bg-white px-2.5 py-1.5 text-xs font-semibold text-slate-700 hover:bg-slate-50">Open</a>
        @endif
    </div>
</article>
