@php
    $isSwitched = session('active_client_id') && session('active_client_id') != auth('client')->id();
    $permissions = session('active_permissions', []);
    $P = \App\Enums\ClientPermission::class;

    $hasServiceAccess = function() use ($permissions, $P) {
        if (in_array($P::VIEW_SERVICES, $permissions)) return true;
        return collect($permissions)->contains(fn($p) => str_starts_with($p, 'view_service'));
    };

    $hasDomainAccess = function() use ($permissions, $P) {
        if (in_array($P::VIEW_DOMAINS, $permissions)) return true;
        return collect($permissions)->contains(fn($p) => str_starts_with($p, 'view_domain'));
    };
@endphp

<aside class="starter-sidebar">
    {{-- Main --}}
    <div class="starter-sidebar-section">
        <div class="starter-sidebar-title">{{ __('store.clientarea_nav_client_area') }}</div>
        <a href="{{ route('store.auth.dashboard') }}"
           class="starter-sidebar-link {{ request()->routeIs('store.auth.dashboard') ? 'active' : '' }}">
            <i class="fas fa-home"></i>
            <span>{{ __('store.clientarea_nav_dashboard') }}</span>
        </a>
    </div>

    {{-- Services --}}
    @if(!$isSwitched || $hasServiceAccess())
    <div class="starter-sidebar-section">
        <div class="starter-sidebar-title">{{ __('store.clientarea_nav_services') }}</div>
        <a href="{{ route('store.auth.services') }}"
           class="starter-sidebar-link {{ request()->routeIs('store.auth.services*') ? 'active' : '' }}">
            <i class="fas fa-server"></i>
            <span>{{ __('store.clientarea_nav_services') }}</span>
        </a>
        @if(!$isSwitched || $hasDomainAccess())
        <a href="{{ route('store.auth.domains') }}"
           class="starter-sidebar-link {{ request()->routeIs('store.auth.domains*') ? 'active' : '' }}">
            <i class="fas fa-globe"></i>
            <span>{{ __('store.clientarea_nav_domains') }}</span>
        </a>
        @endif
    </div>
    @endif

    {{-- Financial --}}
    <div class="starter-sidebar-section">
        <div class="starter-sidebar-title">{{ __('store.clientarea_nav_financial') }}</div>
        @if(!$isSwitched || in_array($P::VIEW_INVOICES, $permissions))
        <a href="{{ route('store.auth.invoices') }}"
           class="starter-sidebar-link {{ request()->routeIs('store.auth.invoices*') ? 'active' : '' }}">
            <i class="fas fa-file-invoice"></i>
            <span>{{ __('store.clientarea_nav_invoices') }}</span>
        </a>
        @endif
        @if(!$isSwitched || in_array($P::VIEW_INVOICES, $permissions))
        <a href="{{ route('store.auth.quotes') }}"
           class="starter-sidebar-link {{ request()->routeIs('store.auth.quotes*') ? 'active' : '' }}">
            <i class="fas fa-file-alt"></i>
            <span>{{ __('store.quotes_title') }}</span>
        </a>
        @endif
        @if(!$isSwitched && \App\Models\Setting::getValue('enable_credit', false))
        <a href="{{ route('store.auth.add-funds.index') }}"
           class="starter-sidebar-link {{ request()->routeIs('store.auth.add-funds*') ? 'active' : '' }}">
            <i class="fas fa-wallet"></i>
            <span>{{ __('store.add_funds_title') }}</span>
        </a>
        @endif
        @if(!$isSwitched)
        <a href="{{ route('store.auth.affiliate.dashboard') }}"
           class="starter-sidebar-link {{ request()->routeIs('store.auth.affiliate*') ? 'active' : '' }}">
            <i class="fas fa-handshake"></i>
            <span>{{ __('store.affiliate_title') }}</span>
        </a>
        @endif
    </div>

    {{-- Support --}}
    <div class="starter-sidebar-section">
        <div class="starter-sidebar-title">{{ __('store.clientarea_nav_support') }}</div>
        @if(!$isSwitched || in_array($P::VIEW_TICKETS, $permissions))
        <a href="{{ route('store.auth.tickets') }}"
           class="starter-sidebar-link {{ request()->routeIs('store.auth.tickets*') ? 'active' : '' }}">
            <i class="fas fa-ticket-alt"></i>
            <span>{{ __('store.clientarea_nav_tickets') }}</span>
        </a>
        @endif
        {{-- Knowledgebase link removed; pending unified posts module. --}}
        @if(!$isSwitched || $hasServiceAccess())
        <a href="{{ route('store.auth.downloads') }}"
           class="starter-sidebar-link {{ request()->routeIs('store.auth.downloads*') ? 'active' : '' }}">
            <i class="fas fa-download"></i>
            <span>{{ __('store.downloads_title') }}</span>
        </a>
        @endif
    </div>

    {{-- Account --}}
    @if(!$isSwitched)
    <div class="starter-sidebar-section">
        <div class="starter-sidebar-title">{{ __('store.clientarea_nav_profile') }}</div>
        <a href="{{ route('store.auth.profile.index') }}"
           class="starter-sidebar-link {{ request()->routeIs('store.auth.profile*') ? 'active' : '' }}">
            <i class="fas fa-user-edit"></i>
            <span>{{ __('store.clientarea_profile_navigation_edit_profile') }}</span>
        </a>
        <a href="{{ route('store.auth.profile.notifications') }}"
           class="starter-sidebar-link {{ request()->routeIs('store.auth.notifications*') ? 'active' : '' }}">
            <i class="fas fa-bell"></i>
            <span>{{ __('store.clientarea_profile_navigation_notifications') }}</span>
        </a>
    </div>
    @endif
</aside>
