@php
    $user = auth()->user();
    $bookingsOpen = request()->routeIs('bookings.*');
    $adminOpen = request()->routeIs('admin.*');
@endphp

<aside id="vemSidebar" class="vem-sidebar" aria-label="Main navigation">
    <div class="vem-sidebar-brand">
        <div class="vem-sidebar-brand-icon">
            <i class="bi bi-building" aria-hidden="true"></i>
        </div>
        <div class="vem-sidebar-brand-text">
            <strong>{{ config('app.name', 'Venue Events Manager') }}</strong>
            <small>Events &amp; Reservations</small>
        </div>
    </div>

    <nav class="vem-sidebar-nav" aria-label="Sidebar menu">
        @if ($user?->hasPermission('dashboard.view'))
            <a href="{{ route('dashboard') }}"
               class="vem-nav-link {{ request()->routeIs('dashboard') ? 'active' : '' }}"
               @if (request()->routeIs('dashboard')) aria-current="page" @endif>
                <i class="bi bi-speedometer2" aria-hidden="true"></i>
                <span>Dashboard</span>
            </a>
        @endif

        @if ($user?->hasPermission('customers.view'))
            <a href="{{ Route::has('customers.index') ? route('customers.index') : '#' }}"
               class="vem-nav-link {{ request()->routeIs('customers.*') ? 'active' : '' }}">
                <i class="bi bi-people" aria-hidden="true"></i>
                <span>Customers</span>
            </a>
        @endif

        @if ($user?->hasPermission('spaces.view'))
            <a href="{{ Route::has('spaces.index') ? route('spaces.index') : '#' }}"
               class="vem-nav-link {{ request()->routeIs('spaces.*') ? 'active' : '' }}">
                <i class="bi bi-door-open" aria-hidden="true"></i>
                <span>Spaces</span>
            </a>
        @endif

        @if ($user?->hasPermission('spaces.view'))
            <a href="{{ Route::has('availability.index') ? route('availability.index') : '#' }}"
               class="vem-nav-link {{ request()->routeIs('availability.*') ? 'active' : '' }}">
                <i class="bi bi-calendar3" aria-hidden="true"></i>
                <span>Availability Calendar</span>
            </a>
        @endif

        @if ($user?->hasPermission('products.view'))
            <a href="{{ Route::has('products.index') ? route('products.index') : '#' }}"
               class="vem-nav-link {{ request()->routeIs('products.*') ? 'active' : '' }}">
                <i class="bi bi-box-seam" aria-hidden="true"></i>
                <span>Products &amp; Services</span>
            </a>
        @endif

        @if ($user?->hasPermission('packages.view'))
            <a href="{{ Route::has('packages.index') ? route('packages.index') : '#' }}"
               class="vem-nav-link {{ request()->routeIs('packages.*') ? 'active' : '' }}">
                <i class="bi bi-collection" aria-hidden="true"></i>
                <span>Packages</span>
            </a>
        @endif

        @if ($user?->hasPermission('bookings.view'))
            <button class="vem-nav-link vem-nav-toggle {{ $bookingsOpen ? 'active' : '' }}"
                    type="button"
                    data-bs-toggle="collapse"
                    data-bs-target="#navBookings"
                    aria-expanded="{{ $bookingsOpen ? 'true' : 'false' }}"
                    aria-controls="navBookings">
                <i class="bi bi-calendar-event" aria-hidden="true"></i>
                <span>Bookings &amp; Reservations</span>
                <i class="bi bi-chevron-down" aria-hidden="true"></i>
            </button>
            <div class="collapse vem-nav-children {{ $bookingsOpen ? 'show' : '' }}" id="navBookings" data-nav-collapse="bookings">
                @php
                    $bookingLinks = [
                        ['label' => 'All', 'route' => 'bookings.index', 'params' => []],
                        ['label' => 'Upcoming', 'route' => 'bookings.index', 'params' => ['view' => 'upcoming']],
                        ['label' => 'Today', 'route' => 'bookings.index', 'params' => ['view' => 'today']],
                        ['label' => 'Past', 'route' => 'bookings.index', 'params' => ['view' => 'past']],
                        ['label' => 'Provisional', 'route' => 'bookings.index', 'params' => ['status' => 'provisional']],
                        ['label' => 'Confirmed', 'route' => 'bookings.index', 'params' => ['status' => 'confirmed']],
                        ['label' => 'Cancelled', 'route' => 'bookings.index', 'params' => ['status' => 'cancelled']],
                    ];
                @endphp
                @foreach ($bookingLinks as $link)
                    @php
                        $href = Route::has($link['route'])
                            ? route($link['route'], $link['params'])
                            : '#';
                        $isActive = request()->routeIs('bookings.*')
                            && request('view', request('filter', '')) === ($link['params']['view'] ?? $link['params']['filter'] ?? '')
                            && request('status', '') === ($link['params']['status'] ?? '');
                    @endphp
                    <a href="{{ $href }}" class="vem-nav-link {{ $isActive ? 'active' : '' }}">
                        {{ $link['label'] }}
                    </a>
                @endforeach
            </div>
        @endif

        @if ($user?->hasPermission('quotations.view'))
            <a href="{{ Route::has('quotations.index') ? route('quotations.index') : '#' }}"
               class="vem-nav-link {{ request()->routeIs('quotations.*') ? 'active' : '' }}">
                <i class="bi bi-file-earmark-text" aria-hidden="true"></i>
                <span>Quotations</span>
            </a>
        @endif

        @if ($user?->hasPermission('invoices.view'))
            <a href="{{ Route::has('invoices.index') ? route('invoices.index') : '#' }}"
               class="vem-nav-link {{ request()->routeIs('invoices.*') ? 'active' : '' }}">
                <i class="bi bi-receipt" aria-hidden="true"></i>
                <span>Invoices</span>
            </a>
        @endif

        @if ($user?->hasPermission('payments.view'))
            <a href="{{ Route::has('payments.index') ? route('payments.index') : '#' }}"
               class="vem-nav-link {{ request()->routeIs('payments.*') ? 'active' : '' }}">
                <i class="bi bi-credit-card" aria-hidden="true"></i>
                <span>Payments</span>
            </a>
        @endif

        @if ($user?->hasPermission('reports.view'))
            <a href="{{ Route::has('reports.index') ? route('reports.index') : '#' }}"
               class="vem-nav-link {{ request()->routeIs('reports.*') ? 'active' : '' }}">
                <i class="bi bi-bar-chart-line" aria-hidden="true"></i>
                <span>Reports</span>
            </a>
        @endif

        @php
            $canAdmin = $user?->hasPermission('users.manage')
                || $user?->hasPermission('roles.manage')
                || $user?->hasPermission('settings.manage')
                || $user?->hasPermission('audit.view');
        @endphp

        @if ($canAdmin)
            <div class="vem-nav-section">Administration</div>

            <button class="vem-nav-link vem-nav-toggle {{ $adminOpen ? 'active' : '' }}"
                    type="button"
                    data-bs-toggle="collapse"
                    data-bs-target="#navAdmin"
                    aria-expanded="{{ $adminOpen ? 'true' : 'false' }}"
                    aria-controls="navAdmin">
                <i class="bi bi-gear" aria-hidden="true"></i>
                <span>Administration</span>
                <i class="bi bi-chevron-down" aria-hidden="true"></i>
            </button>
            <div class="collapse vem-nav-children {{ $adminOpen ? 'show' : '' }}" id="navAdmin" data-nav-collapse="admin">
                @if ($user?->hasPermission('users.manage'))
                    <a href="{{ route('admin.users.index') }}"
                       class="vem-nav-link {{ request()->routeIs('admin.users.*') ? 'active' : '' }}">
                        Users
                    </a>
                @endif
                @if ($user?->hasPermission('roles.manage'))
                    <a href="{{ route('admin.roles.index') }}"
                       class="vem-nav-link {{ request()->routeIs('admin.roles.*') ? 'active' : '' }}">
                        Roles
                    </a>
                @endif
                @if ($user?->hasPermission('settings.manage'))
                    <a href="{{ route('admin.settings.edit') }}"
                       class="vem-nav-link {{ request()->routeIs('admin.settings.*') ? 'active' : '' }}">
                        Settings
                    </a>
                @endif
                @if ($user?->hasPermission('audit.view'))
                    <a href="{{ route('admin.audit-logs.index') }}"
                       class="vem-nav-link {{ request()->routeIs('admin.audit-logs.*') ? 'active' : '' }}">
                        Audit Logs
                    </a>
                @endif
            </div>
        @endif
    </nav>
</aside>

<div id="vemSidebarBackdrop" class="vem-sidebar-backdrop d-none" onclick="vemCloseSidebar()" aria-hidden="true"></div>
