@php
    $customTypes    = \AleshaCMS\Models\CMS\ContentType::where('is_active', true)->get();
    $activePlugins  = \AleshaCMS\Models\CMS\Plugin::where('is_active', true)->pluck('slug')->flip()->toArray();

    $contentItems = [
        ['route' => 'admin.posts.index', 'label' => 'Posts',  'icon' => 'document-text', 'params' => ['post']],
        ['route' => 'admin.posts.index', 'label' => 'Pages',  'icon' => 'document',      'params' => ['page']],
    ];

    foreach ($customTypes as $ct) {
        $contentItems[] = [
            'route'  => 'admin.posts.index',
            'label'  => $ct->name,
            'icon'   => 'document-text',
            'params' => [$ct->slug],
        ];
    }

    $contentItems[] = ['route' => 'admin.media.index', 'label' => 'Media', 'icon' => 'photo'];
    $contentItems[] = ['route' => 'admin.taxonomies.index', 'label' => 'Categories', 'icon' => 'folder', 'params' => ['category'], 'taxon' => true];
    $contentItems[] = ['route' => 'admin.taxonomies.index', 'label' => 'Tags',       'icon' => 'tag',    'params' => ['tag'],      'taxon' => true];

    $navGroups = [
        [
            'label' => null,
            'items' => [
                ['route' => 'admin.dashboard', 'label' => 'Dashboard', 'icon' => 'home'],
                ['route' => 'admin.stats',     'label' => 'Statistics', 'icon' => 'chart-bar'],
            ],
        ],
        [
            'label' => 'Content',
            'items' => $contentItems,
        ],
        [
            'label' => 'Structure',
            'items' => [
                ['route' => 'admin.menus.index',         'label' => 'Menus',         'icon' => 'bars-3'],
                ...( isset($activePlugins['Sliders']) ? [['route' => 'admin.sliders.index', 'label' => 'Sliders', 'icon' => 'photo']] : [] ),
                ['route' => 'admin.content-types.index', 'label' => 'Content Types', 'icon' => 'squares-2x2'],
            ],
        ],
        [
            'label' => 'Appearance',
            'items' => [
                ['route' => 'admin.themes.index',  'label' => 'Themes',  'icon' => 'swatch'],
                ['route' => 'admin.plugins.index', 'label' => 'Plugins', 'icon' => 'puzzle-piece'],
            ],
        ],
        [
            'label' => 'System',
            'items' => [
                ['route' => 'admin.users.index',    'label' => 'Users',    'icon' => 'users'],
                ['route' => 'admin.settings.index', 'label' => 'Settings', 'icon' => 'cog-6-tooth'],
                ['route' => 'admin.logs.index',     'label' => 'Logs',     'icon' => 'clipboard-document-list'],
            ],
        ],
    ];
@endphp

<nav class="space-y-5">
    @foreach($navGroups as $group)
    <div>
        @if($group['label'])
        <p class="px-3 mb-1 text-xs font-semibold uppercase tracking-widest text-gray-500 whitespace-nowrap overflow-hidden transition-all"
           :class="sidebarCollapsed ? 'lg:opacity-0 lg:h-0 lg:mb-0 lg:overflow-hidden' : ''">{{ $group['label'] }}</p>
        @else
        {{-- spacer when collapsed so groups don't bleed together --}}
        <div :class="sidebarCollapsed ? 'lg:hidden' : 'hidden'"></div>
        @endif
        <ul class="space-y-0.5">
            @foreach($group['items'] as $item)
            @php
                $routeName = $item['route'];
                $params    = $item['params'] ?? [];
                $isTaxon   = $item['taxon'] ?? false;

                if ($isTaxon) {
                    $isActive = request()->routeIs('admin.taxonomies.*') && request()->segment(3) === $params[0];
                } elseif (!empty($params)) {
                    $isActive = request()->routeIs('admin.posts.*') && request()->segment(3) === $params[0];
                } else {
                    $parts = explode('.', $routeName);
                    if (count($parts) <= 2) {
                        $isActive = request()->routeIs($routeName);
                    } else {
                        $prefix   = implode('.', array_slice($parts, 0, -1));
                        $isActive = request()->routeIs($prefix . '.*');
                    }
                }
            @endphp
            <li>
                <a href="{{ route($routeName, $params) }}"
                   :class="sidebarCollapsed ? 'lg:justify-center lg:px-0' : 'gap-3 px-3'"
                   class="flex items-center py-2 rounded-lg text-sm font-medium transition-colors w-full
                          {{ $isActive
                              ? 'bg-indigo-600 text-white'
                              : 'text-gray-400 hover:bg-gray-800 hover:text-white' }}"
                   @mouseenter="showNavTooltip('{{ $item['label'] }}', $event)"
                   @mouseleave="hideNavTooltip()">
                    <x-admin-icon :name="$item['icon']" class="w-5 h-5 flex-shrink-0" />
                    <span class="whitespace-nowrap overflow-hidden transition-all"
                          :class="sidebarCollapsed ? 'lg:hidden' : ''">{{ $item['label'] }}</span>
                </a>
            </li>
            @endforeach
        </ul>
    </div>
    @endforeach
</nav>

@hook('admin.sidebar')
