<x-app-layout title="Devices">
    <div class="space-y-6">
        <x-care.page-hero
            badge="Queue · Scanners · Agents"
            title="Devices"
            description="Register queue kiosks and waiting displays, plus barcode scanners and agent-connected clinical hardware."
            :stats="[
                ['value' => number_format($heroStats['total']), 'label' => 'Devices'],
                ['value' => number_format($heroStats['kiosks'] ?? 0), 'label' => 'Kiosks'],
                ['value' => number_format($heroStats['displays'] ?? 0), 'label' => 'Displays'],
                ['value' => number_format($heroStats['online']), 'label' => 'Online'],
            ]">
            @if ($canManage)
                <x-slot name="actions">
                    @if ($hasQueueDevices ?? false)
                        <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
                    <a href="{{ route('care.devices.create') }}" class="btn-primary">Add device</a>
                </x-slot>
            @endif
        </x-care.page-hero>

        <div class="overflow-hidden rounded-2xl border border-slate-200 bg-white">
            <table class="min-w-full text-sm">
                <thead class="bg-slate-50 text-left text-xs uppercase text-slate-500">
                    <tr>
                        <th class="px-4 py-3">Name</th>
                        <th class="px-4 py-3">Type</th>
                        <th class="px-4 py-3">Connection</th>
                        <th class="px-4 py-3">Status</th>
                        <th class="px-4 py-3">Branch</th>
                        <th class="px-4 py-3">Last seen</th>
                        <th class="px-4 py-3"></th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-50">
                    @forelse ($devices as $device)
                        <tr>
                            <td class="px-4 py-3 font-medium text-slate-900">{{ $device->name }}</td>
                            <td class="px-4 py-3 text-slate-600">{{ $deviceTypes[$device->type] ?? $device->type }}</td>
                            <td class="px-4 py-3 text-slate-600">{{ $connectionModes[$device->connection_mode] ?? $device->connection_mode }}</td>
                            <td class="px-4 py-3">
                                <span class="inline-flex rounded-full px-2 py-0.5 text-xs font-medium {{ $device->isOnline() ? 'bg-emerald-100 text-emerald-800' : 'bg-slate-100 text-slate-600' }}">
                                    {{ $device->isOnline() ? 'Online' : ucfirst($device->status) }}
                                </span>
                            </td>
                            <td class="px-4 py-3 text-slate-600">{{ $device->branch?->name ?? 'All branches' }}</td>
                            <td class="px-4 py-3 text-slate-500">{{ $device->last_seen_at?->diffForHumans() ?? 'Never' }}</td>
                            <td class="px-4 py-3 text-right">
                                @if ($canManage)
                                    <a href="{{ route('care.devices.edit', $device) }}" class="text-sky-600 hover:text-sky-700">Edit</a>
                                @endif
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="7" class="px-4 py-8 text-center text-slate-500">No devices registered yet.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
            @if ($devices->hasPages())
                <div class="border-t border-slate-100 px-5 py-3">{{ $devices->links() }}</div>
            @endif
        </div>
    </div>
</x-app-layout>
