{{-- Account Navigation Sidebar --}}
@php
    // If a module has registered a nav override whose condition is true, use it.
    $navOverride = app('module.account.nav')
        ->first(fn($item) => !isset($item['condition']) || call_user_func($item['condition']));
@endphp
@if($navOverride)
    @include($navOverride['view'])
@else
@php
    $hiddenNavSlugs = app('module.nav.hide')
        ->filter(fn($item) => !isset($item['condition']) || call_user_func($item['condition']))
        ->pluck('slug')
        ->toArray();
    $navVisible = fn(string $slug) => !in_array($slug, $hiddenNavSlugs);
@endphp
<nav class="w-full lg:w-64 flex-shrink-0">
    @if(auth()->user()->hasRole('administrator'))
        <a class="flex items-center gap-3 px-4 py-3 text-sm font-medium text-text-muted hover:text-primary hover:bg-white dark:hover:bg-surface-dark rounded-lg transition-all"
           href="{{ route('ch-admin.ch_admin_dashboard') }}">
            <span class="material-symbols-outlined text-[22px]">admin_panel_settings</span>
            Admin Panel
        </a>
    @endif
    @if($navVisible('downloads'))
    <a class="flex items-center gap-3 px-4 py-3 text-sm font-medium {{ request()->routeIs('frontend.account.downloads*') ? 'text-primary bg-white dark:bg-surface-dark shadow-sm border border-gray-100 dark:border-gray-800 rounded-lg transition-all font-semibold' : 'text-text-muted hover:text-primary hover:bg-white dark:hover:bg-surface-dark rounded-lg transition-all' }}"
       href="{{ route('frontend.account.downloads') }}">
        <span class="material-symbols-outlined text-[22px]">download</span>
        {{ __('account.My Downloads') }}
    </a>
    @endif
    @if($navVisible('orders'))
    <a class="flex items-center gap-3 px-4 py-3 text-sm font-medium {{ request()->routeIs('ch_user_orders') ? 'text-primary bg-white dark:bg-surface-dark shadow-sm border border-gray-100 dark:border-gray-800 rounded-lg transition-all font-semibold' : 'text-text-muted hover:text-primary hover:bg-white dark:hover:bg-surface-dark rounded-lg transition-all' }}"
        href="{{ route('ch_user_orders') }}">
        <span class="material-symbols-outlined text-[22px]">shopping_cart</span>
        {{ __('account.My Orders') }}
    </a>
    @endif
    @if($navVisible('subscriptions'))
    <a class="flex items-center gap-3 px-4 py-3 text-sm font-medium {{ in_array(Route::currentRouteName(), ['frontend.subscriptions.index', 'frontend.subscriptions.show', 'frontend.subscriptions.orders.show']) ? 'font-semibold text-primary bg-white dark:bg-surface-dark shadow-sm border border-gray-100 dark:border-gray-800' : 'text-text-muted hover:text-primary hover:bg-white dark:hover:bg-surface-dark' }} rounded-lg transition-all"
       href="{{ route('frontend.subscriptions.index') }}">
        <span class="material-symbols-outlined text-[22px]">card_membership</span>
        {{ __('account.My Subscriptions') }}
    </a>
    @endif
    @if($navVisible('licenses'))
    <a class="flex items-center gap-3 px-4 py-3 text-sm font-medium {{ request()->routeIs('frontend.account.licenses*') ? 'text-primary bg-white dark:bg-surface-dark shadow-sm border border-gray-100 dark:border-gray-800 rounded-lg transition-all font-semibold' : 'text-text-muted hover:text-primary hover:bg-white dark:hover:bg-surface-dark rounded-lg transition-all' }}"
        href="{{ route('frontend.account.licenses') }}">
        <span class="material-symbols-outlined text-[22px]">key</span>
        {{ __('account.Licenses') }}
    </a>
    @endif
    @if($navVisible('reviews'))
    <a class="flex items-center gap-3 px-4 py-3 text-sm font-medium {{ request()->routeIs('frontend.account.reviews') ? 'text-primary bg-white dark:bg-surface-dark shadow-sm border border-gray-100 dark:border-gray-800 rounded-lg transition-all font-semibold' : 'text-text-muted hover:text-primary hover:bg-white dark:hover:bg-surface-dark rounded-lg transition-all' }}"
        href="{{ route('frontend.account.reviews') }}">
        <span class="material-symbols-outlined text-[22px]">reviews</span>
        {{ __('account.My Reviews') }}
    </a>
    @endif
    @if($navVisible('support'))
    <a class="flex items-center gap-3 px-4 py-3 text-sm font-medium {{ in_array(Route::currentRouteName(), ['helpdesk.tickets.index', 'helpdesk.tickets.create', 'helpdesk.tickets.show']) ? 'font-semibold text-primary bg-white dark:bg-surface-dark shadow-sm border border-gray-100 dark:border-gray-800' : 'text-text-muted hover:text-primary hover:bg-white dark:hover:bg-surface-dark' }} rounded-lg transition-all"
       href="{{ route('helpdesk.tickets.index') }}">
        <span class="material-symbols-outlined text-[22px]">contact_support</span>
        Support Tickets
    </a>
    @endif
    @if($navVisible('account'))
    <a class="flex items-center gap-3 px-4 py-3 text-sm font-medium {{ Route::currentRouteName() == 'frontend.user.account' ? 'font-semibold text-primary bg-white dark:bg-surface-dark shadow-sm border border-gray-100 dark:border-gray-800' : 'text-text-muted hover:text-primary hover:bg-white dark:hover:bg-surface-dark' }} rounded-lg transition-all"
        href="{{ route('frontend.user.account') }}">
        <span class="material-symbols-outlined text-[22px]">settings</span>
        {{ __('account.My Account') }}
    </a>
    @endif

    {{-- Module-injected account nav items --}}
    @php
        $moduleAccountItems = app('module.account.menu')->sortBy('order');
    @endphp
    @if($moduleAccountItems->isNotEmpty())
        <div class="border-t border-gray-100 dark:border-gray-800 my-1"></div>
        @foreach($moduleAccountItems as $item)
            @if(!isset($item['condition']) || call_user_func($item['condition']))
                <a class="flex items-center gap-3 px-4 py-3 text-sm font-medium {{ request()->routeIs($item['routes']) ? 'font-semibold text-primary bg-white dark:bg-surface-dark shadow-sm border border-gray-100 dark:border-gray-800' : 'text-text-muted hover:text-primary hover:bg-white dark:hover:bg-surface-dark' }} rounded-lg transition-all"
                   href="{{ route($item['route']) }}">
                    <span class="material-symbols-outlined text-[22px]">{{ $item['icon'] }}</span>
                    {{ $item['label'] }}
                </a>
            @endif
        @endforeach
    @endif

    <form id="logout-form-account" action="{{ route('logout') }}" method="POST" style="display: none;">
        @csrf
    </form>
    <a class="flex items-center gap-3 px-4 py-3 text-sm font-medium text-text-muted hover:text-red-600 hover:bg-white dark:hover:bg-surface-dark rounded-lg transition-all"
       href="{{ route('logout') }}" onclick="event.preventDefault(); document.getElementById('logout-form-account').submit();">
        <span class="material-symbols-outlined text-[22px]">logout</span>
        {{ __('account.Logout') }}
    </a>
</nav>
@endif
