@php
    $specialtyLabel = $specialtyLabel ?? null;
    $specialtyDepartmentMap = $specialtyDepartmentMap ?? [];
@endphp
<x-app-layout title="Queue">
    <div class="space-y-6">
        <x-care.page-hero
            :badge="$specialtyLabel
                ? $specialtyLabel.' · Patient flow'
                : 'Ladill Care · Patient flow'"
            :title="$specialtyLabel ? $specialtyLabel.' queue' : 'Patient flow'"
            :description="$specialtyLabel
                ? 'Move '.$specialtyLabel.' patients from waiting through called, in care, and done.'
                : 'Move patients from waiting through called, in care, and done — one board for your branch.'"
            :stats="[
                ['value' => number_format($heroStats['waiting']), 'label' => 'Waiting'],
                ['value' => number_format($heroStats['in_consultation']), 'label' => $specialtyLabel ? 'In care' : 'In consultation'],
                ['value' => number_format($heroStats['today']), 'label' => 'Today'],
            ]">
            @if ($canManageQueue)
                <x-slot name="actions">
                    <a href="{{ route('care.appointments.walk-in.create') }}" class="btn-primary">Walk-in</a>
                    @if (app(\App\Services\Care\CarePermissions::class)->can(
                        auth()->user() ? app(\App\Services\Care\OrganizationResolver::class)->memberFor(auth()->user()) : null,
                        'devices.view'
                    ))
                        <a href="{{ route('care.devices.create', ['type' => 'kiosk']) }}" class="btn-secondary">Add kiosk</a>
                        <a href="{{ route('care.devices.create', ['type' => 'display']) }}" class="btn-secondary">Add display</a>
                    @endif
                    @if (app(\App\Services\Care\CarePermissions::class)->can(
                        auth()->user() ? app(\App\Services\Care\OrganizationResolver::class)->memberFor(auth()->user()) : null,
                        'displays.view'
                    ))
                        <a href="{{ route('care.displays.index') }}" class="btn-secondary">Displays</a>
                    @endif
                </x-slot>
            @endif
        </x-care.page-hero>

        @include('care.partials.queue-board', [
            'queue' => $queue,
            'inConsultation' => $inConsultation,
            'done' => $done ?? collect(),
            'queueIntegration' => $queueIntegration ?? null,
            'branchId' => $branchId,
            'canConsult' => $canConsult,
            'lockToPractitioner' => $lockToPractitioner ?? false,
            'canSwitchBranch' => $canSwitchBranch ?? false,
            'branches' => $branches ?? collect(),
            'practitioners' => $practitioners ?? collect(),
            'practitionerId' => $practitionerId ?? null,
            'queueCallNextRoute' => 'care.queue.call-next',
            'queueCallNextParams' => array_filter(['practitioner_id' => $practitionerId]),
            'startRouteName' => 'care.queue.start',
            'inCareLabel' => $specialtyLabel ? 'In care' : 'In consultation',
            'openInCareUrl' => function ($appointment) use ($specialtyDepartmentMap) {
                $deptId = (int) ($appointment->department_id ?? 0);
                $module = $deptId > 0 ? ($specialtyDepartmentMap[$deptId] ?? null) : null;
                if ($module && $appointment->visit) {
                    return route('care.specialty.workspace', [
                        'module' => $module,
                        'visit' => $appointment->visit,
                    ]);
                }

                return $appointment->consultation
                    ? route('care.consultations.show', $appointment->consultation)
                    : null;
            },
        ])
    </div>
</x-app-layout>
