@php
    // Global "update available" cue for the Version nav item — a cache read only (no network).
    $updateAvailable = app(\App\Services\ReleaseChecker::class)->updateAvailable();
    // Honeypot fake-login attempts in the last 24h → Threats nav badge. The sidebar renders on
    // every page, so cache the count for 60s to avoid a per-render COUNT query.
    $threatCount = \Illuminate\Support\Facades\Cache::remember(
        'threats:login_attempts_24h',
        60,
        fn () => \App\Models\AuditEvent::where('action', 'security.honeypot_login')
            ->where('created_at', '>', now()->subDay())
            ->count(),
    );
    // Active (firing) alert incidents → the Alarme nav badge. Cached like the threats count.
    $alertCount = \Illuminate\Support\Facades\Cache::remember(
        'alerts:firing_count',
        60,
        fn () => \App\Models\AlertIncident::where('state', 'firing')->count(),
    );
@endphp
{{-- Fixed on desktop, off-canvas drawer on mobile/tablet (toggled by `nav` in the layout). --}}
<aside class="fixed inset-y-0 left-0 z-40 flex w-[272px] -translate-x-full flex-col border-r border-line bg-surface transition-transform duration-200 lg:translate-x-0"
       :class="{ '!translate-x-0': nav }"
       x-bind:inert="! nav && ! desktop"
       aria-label="{{ __('shell.nav_aria') }}">
    {{-- Brand --}}
    <div class="flex h-14 shrink-0 items-center gap-2.5 border-b border-line px-4">
        <span class="grid h-8 w-8 place-items-center rounded-md border border-accent/25 bg-accent/10 text-accent shadow-[0_0_18px_-2px_var(--color-accent)]">
            <x-icon name="server" class="h-4 w-4" />
        </span>
        <span class="font-display text-lg font-semibold tracking-wide text-ink">Clus<span class="text-accent">e</span>v</span>
        <button type="button" @click="nav = false"
                class="ml-auto grid min-h-11 min-w-11 place-items-center rounded-md text-ink-3 hover:bg-raised hover:text-ink lg:hidden"
                aria-label="{{ __('shell.menu_close') }}">
            <x-icon name="x" class="h-5 w-5" />
        </button>
    </div>

    {{-- Server switcher --}}
    <div class="shrink-0 border-b border-line p-3">
        <livewire:server-switcher />
    </div>

    {{-- Nav --}}
    <nav class="flex-1 space-y-1 overflow-y-auto p-3">
        <p class="px-3 pb-1 pt-2 font-mono text-[10px] uppercase tracking-widest text-ink-4">{{ __('shell.group_fleet') }}</p>
        <x-nav-item icon="dashboard" href="/" :active="request()->is('/')" data-tour="dashboard">{{ __('shell.nav_dashboard') }}</x-nav-item>
        <x-nav-item icon="server" href="/servers" :active="request()->is('servers*')" data-tour="servers">{{ __('shell.nav_servers') }}</x-nav-item>
        <x-nav-item icon="cpu" href="/services" :active="request()->is('services*')">{{ __('shell.nav_services') }}</x-nav-item>
        <x-nav-item icon="box" href="/docker" :active="request()->is('docker*')">{{ __('shell.nav_docker') }}</x-nav-item>
        @can('operate')
            <x-nav-item icon="command" href="/commands" :active="request()->is('commands*')">{{ __('shell.nav_commands') }}</x-nav-item>
        @endcan
        <x-nav-item icon="folder" href="/files" :active="request()->is('files*')">{{ __('shell.nav_files') }}</x-nav-item>
        <x-nav-item icon="audit" href="/audit" :active="request()->is('audit*')">{{ __('shell.nav_audit') }}</x-nav-item>
        @can('manage-network')
            <x-nav-item icon="lock" href="/wireguard" :active="request()->is('wireguard*')">{{ __('shell.nav_wireguard') }}</x-nav-item>
        @endcan
        <x-nav-item icon="terminal" href="/terminal" :active="request()->is('terminal*')" data-tour="terminal">{{ __('shell.nav_terminal') }}</x-nav-item>

        <p class="px-3 pb-1 pt-3 font-mono text-[10px] uppercase tracking-widest text-ink-4">{{ __('shell.group_account') }}</p>
        <x-nav-item icon="settings" href="/settings" :active="request()->is('settings*')" data-tour="settings">{{ __('shell.nav_settings') }}</x-nav-item>
        <x-nav-item icon="shield" href="/system" :active="request()->is('system*')">{{ __('shell.nav_system') }}</x-nav-item>
        @can('operate')
            <x-nav-item icon="shield-check" href="/posture" :active="request()->is('posture*')">{{ __('shell.nav_posture') }}</x-nav-item>
            <x-nav-item icon="rotate" href="/patch" :active="request()->is('patch*')">{{ __('shell.nav_patch') }}</x-nav-item>
            <x-nav-item icon="lock" href="/certs" :active="request()->is('certs*')">{{ __('shell.nav_certs') }}</x-nav-item>
            <x-nav-item icon="activity" href="/health" :active="request()->is('health*')">{{ __('shell.nav_health') }}</x-nav-item>
        @endcan
        @can('manage-panel')
            <x-nav-item icon="alert" href="/alerts" :active="request()->is('alerts*')" :badge="$alertCount > 0 ? $alertCount : null" :badge-title="$alertCount > 0 ? __('alerts.incidents_title') : null">{{ __('shell.nav_alerts') }}</x-nav-item>
            <x-nav-item icon="shield-alert" href="/threats" :active="request()->is('threats*')" :badge="$threatCount > 0 ? $threatCount : null" :badge-title="$threatCount > 0 ? __('shell.threats_badge', ['count' => $threatCount]) : null">{{ __('shell.nav_threats') }}</x-nav-item>
        @endcan
        <x-nav-item icon="tag" href="/versions" :active="request()->is('versions*')" :badge="$updateAvailable ? '1' : null" :badge-title="$updateAvailable ? __('shell.update_available') : null">{{ __('shell.nav_versions') }}</x-nav-item>
        @if (config('clusev.release_controls'))
            <x-nav-item icon="tag" href="/release" :active="request()->is('release*')">
                {{ __('release.nav') }}
            </x-nav-item>
        @endif
        {{-- Help opens the online docs (docs.clusev.com) in a new tab — no in-panel duplication. --}}
        <x-nav-item icon="help-circle" :href="config('clusev.docs_url')" target="_blank" rel="noopener">{{ __('shell.nav_help') }}</x-nav-item>
    </nav>

    {{-- User --}}
    @php($u = auth()->user())
    <div class="shrink-0 border-t border-line p-3">
        <div class="flex items-center gap-1">
            <a href="{{ route('settings') }}" wire:navigate class="flex min-w-0 flex-1 items-center gap-2.5 rounded-md px-2 py-1.5 transition-colors hover:bg-raised">
                <span class="grid h-8 w-8 shrink-0 place-items-center rounded-full bg-raised font-mono text-xs text-ink-2">{{ strtoupper(substr($u?->name ?? 'U', 0, 2)) }}</span>
                <span class="flex min-w-0 flex-1 items-center gap-1.5">
                    <span class="min-w-0 truncate text-sm text-ink">{{ $u?->name ?? '—' }}</span>
                    <x-two-factor-badge :enabled="$u?->hasTwoFactorEnabled() ?? false" />
                </span>
            </a>
            <form method="POST" action="{{ route('logout') }}" class="shrink-0">
                @csrf
                <button type="submit" class="grid min-h-11 min-w-11 place-items-center rounded-md text-ink-3 hover:bg-raised hover:text-ink" aria-label="{{ __('shell.logout') }}">
                    <x-icon name="logout" class="h-[18px] w-[18px]" />
                </button>
            </form>
        </div>
    </div>
</aside>
