{{-- Nuovo menu sidebar completo e organizzato logicamente --}}
<nav class="h-full flex flex-col bg-yellow-300 dark:bg-gray-800 border-r-4 border-indigo-500 dark:border-yellow-500 py-6 px-2 w-full shadow-xl z-50">
    @php
        $userRoles = auth()->check() ? auth()->user()->getRoleNames()->toArray() : [];
        $panelPrefix = '';
        if (in_array('super-admin', $userRoles)) {
            $panelPrefix = 'superadmin.';
        } elseif (in_array('admin', $userRoles) || in_array('amministratore', $userRoles)) {
            $panelPrefix = 'admin.';
        }
        
        // MENU ORGANIZZATO IN CATEGORIE LOGICHE
        $mainMenu = [
            // 1. DASHBOARD & OVERVIEW
            [
                'icon' => 'fa-solid fa-home',
                'label' => __('menu.dashboard'),
                'route' => $panelPrefix . 'dashboard',
                'roles' => ['admin', 'super-admin', 'amministratore', 'collaboratore', 'condomino'],
            ],
            
            // 2. ANAGRAFICA (Dati di base)
            [
                'icon' => 'fa-solid fa-address-book',
                'label' => __('menu.anagrafica'),
                'route' => '#',
                'roles' => ['admin', 'super-admin', 'amministratore', 'collaboratore'],
                'expandable' => true,
                'id' => 'anagrafica-menu',
                'submenu' => [
                    [
                        'icon' => 'fa-solid fa-building',
                        'label' => __('menu.stabili'),
                        'route' => $panelPrefix . 'stabili.index',
                        'roles' => ['admin', 'super-admin', 'amministratore', 'collaboratore'],
                    ],
                    [
                        'icon' => 'fa-solid fa-door-open',
                        'label' => __('menu.unita_immobiliari'),
                        'route' => $panelPrefix . 'unita-immobiliari.index',
                        'roles' => ['admin', 'super-admin', 'amministratore', 'collaboratore'],
                    ],
                    [
                        'icon' => 'fa-solid fa-users',
                        'label' => __('menu.soggetti'),
                        'route' => $panelPrefix . 'soggetti.index',
                        'roles' => ['admin', 'super-admin', 'amministratore', 'collaboratore'],
                    ],
                    [
                        'icon' => 'fa-solid fa-id-card',
                        'label' => __('menu.anagrafica_condominiale'),
                        'route' => $panelPrefix . 'anagrafica-condominiale.index',
                        'roles' => ['admin', 'super-admin', 'amministratore', 'collaboratore'],
                    ],
                    [
                        'icon' => 'fa-solid fa-key',
                        'label' => __('menu.diritti_reali'),
                        'route' => $panelPrefix . 'diritti-reali.index',
                        'roles' => ['admin', 'super-admin', 'amministratore', 'collaboratore'],
                    ],
                    [
                        'icon' => 'fa-solid fa-table',
                        'label' => __('menu.tabelle_millesimali'),
                        'route' => $panelPrefix . 'tabelle-millesimali.index',
                        'roles' => ['admin', 'super-admin', 'amministratore', 'collaboratore'],
                    ],
                    [
                        'icon' => 'fa-solid fa-address-book',
                        'label' => __('menu.rubrica'),
                        'route' => $panelPrefix . 'rubrica.index',
                        'roles' => ['admin', 'super-admin', 'amministratore', 'collaboratore'],
                    ],
                    [
                        'icon' => 'fa-solid fa-truck',
                        'label' => __('menu.fornitori'),
                        'route' => $panelPrefix . 'fornitori.index',
                        'roles' => ['admin', 'super-admin', 'amministratore', 'collaboratore'],
                    ],
                ]
            ],
            
            // 3. CONTRATTI & LOCAZIONI
            [
                'icon' => 'fa-solid fa-file-contract',
                'label' => __('menu.contratti_locazioni'),
                'route' => '#',
                'roles' => ['admin', 'super-admin', 'amministratore', 'collaboratore'],
                'expandable' => true,
                'id' => 'contratti-menu',
                'submenu' => [
                    [
                        'icon' => 'fa-solid fa-file-signature',
                        'label' => __('menu.contratti_locazione'),
                        'route' => $panelPrefix . 'contratti-locazione.index',
                        'roles' => ['admin', 'super-admin', 'amministratore', 'collaboratore'],
                    ],
                    // Placeholder per future funzionalità contratti
                    [
                        'icon' => 'fa-solid fa-calendar-dollar',
                        'label' => __('menu.canoni_scadenze'),
                        'route' => '#', // TODO: Implementare
                        'roles' => ['admin', 'super-admin', 'amministratore'],
                    ],
                ]
            ],
            
            // 4. CONTABILITÀ & FINANZE
            [
                'icon' => 'fa-solid fa-file-invoice-dollar',
                'label' => __('menu.contabilita'),
                'route' => '#',
                'roles' => ['admin', 'super-admin', 'amministratore'],
                'expandable' => true,
                'id' => 'contabilita-menu',
                'submenu' => [
                    [
                        'icon' => 'fa-solid fa-chart-line',
                        'label' => __('menu.movimenti'),
                        'route' => $panelPrefix . 'contabilita.index',
                        'roles' => ['admin', 'super-admin', 'amministratore'],
                    ],
                    [
                        'icon' => 'fa-solid fa-balance-scale',
                        'label' => __('menu.bilanci'),
                        'route' => $panelPrefix . 'bilanci.index',
                        'roles' => ['admin', 'super-admin', 'amministratore'],
                    ],
                    [
                        'icon' => 'fa-solid fa-university',
                        'label' => __('menu.conti_bancari'),
                        'route' => '#', // TODO: Implementare BancaController
                        'roles' => ['admin', 'super-admin', 'amministratore'],
                    ],
                    [
                        'icon' => 'fa-solid fa-file-import',
                        'label' => __('menu.import_export_xml'),
                        'route' => $panelPrefix . 'contabilita.import-xml',
                        'roles' => ['admin', 'super-admin', 'amministratore'],
                    ],
                ]
            ],
            
            // 5. SPESE & RIPARTIZIONI
            [
                'icon' => 'fa-solid fa-calculator',
                'label' => __('menu.gestione_spese'),
                'route' => '#',
                'roles' => ['admin', 'super-admin', 'amministratore'],
                'expandable' => true,
                'id' => 'spese-menu',
                'submenu' => [
                    [
                        'icon' => 'fa-solid fa-receipt',
                        'label' => __('menu.voci_spesa'),
                        'route' => $panelPrefix . 'voci-spesa.index',
                        'roles' => ['admin', 'super-admin', 'amministratore'],
                    ],
                    [
                        'icon' => 'fa-solid fa-chart-pie',
                        'label' => __('menu.ripartizione_spese'),
                        'route' => $panelPrefix . 'ripartizioni-spesa.index',
                        'roles' => ['admin', 'super-admin', 'amministratore'],
                    ],
                    [
                        'icon' => 'fa-solid fa-credit-card',
                        'label' => __('menu.piano_rateizzazione'),
                        'route' => $panelPrefix . 'piani-rateizzazione.index',
                        'roles' => ['admin', 'super-admin', 'amministratore'],
                    ],
                    [
                        'icon' => 'fa-solid fa-calendar-check',
                        'label' => __('menu.rate'),
                        'route' => $panelPrefix . 'rate.index',
                        'roles' => ['admin', 'super-admin', 'amministratore'],
                    ],
                ]
            ],
            
            // 6. ASSEMBLEE & DELIBERE
            [
                'icon' => 'fa-solid fa-users-cog',
                'label' => __('menu.assemblee'),
                'route' => '#',
                'roles' => ['admin', 'super-admin', 'amministratore'],
                'expandable' => true,
                'id' => 'assemblee-menu',
                'submenu' => [
                    [
                        'icon' => 'fa-solid fa-calendar-alt',
                        'label' => __('menu.calendario_assemblee'),
                        'route' => $panelPrefix . 'assemblee.index',
                        'roles' => ['admin', 'super-admin', 'amministratore'],
                    ],
                    // Placeholder per future funzionalità assemblee
                    [
                        'icon' => 'fa-solid fa-file-alt',
                        'label' => __('menu.convocazioni'),
                        'route' => '#', // TODO: Implementare
                        'roles' => ['admin', 'super-admin', 'amministratore'],
                    ],
                ]
            ],
            
            // 7. PREVENTIVI & PIANIFICAZIONE
            [
                'icon' => 'fa-solid fa-clipboard-list',
                'label' => __('menu.preventivi_planning'),
                'route' => '#',
                'roles' => ['admin', 'super-admin', 'amministratore'],
                'expandable' => true,
                'id' => 'preventivi-menu',
                'submenu' => [
                    [
                        'icon' => 'fa-solid fa-file-invoice',
                        'label' => __('menu.preventivi'),
                        'route' => $panelPrefix . 'preventivi.index',
                        'roles' => ['admin', 'super-admin', 'amministratore'],
                    ],
                    // Placeholder per future funzionalità preventivi
                ]
            ],
            
            // 8. DOCUMENTI & ARCHIVIO
            [
                'icon' => 'fa-solid fa-folder-open',
                'label' => __('menu.documenti'),
                'route' => '#',
                'roles' => ['admin', 'super-admin', 'amministratore', 'collaboratore'],
                'expandable' => true,
                'id' => 'documenti-menu',
                'submenu' => [
                    [
                        'icon' => 'fa-solid fa-paperclip',
                        'label' => __('menu.allegati'),
                        'route' => $panelPrefix . 'allegati.index',
                        'roles' => ['admin', 'super-admin', 'amministratore', 'collaboratore'],
                    ],
                    [
                        'icon' => 'fa-solid fa-file-alt',
                        'label' => __('menu.documenti_ufficiali'),
                        'route' => $panelPrefix . 'documenti.index',
                        'roles' => ['admin', 'super-admin', 'amministratore', 'collaboratore'],
                    ],
                    [
                        'icon' => 'fa-solid fa-folder',
                        'label' => __('menu.file_manager'),
                        'route' => $panelPrefix . 'file-manager.index',
                        'roles' => ['admin', 'super-admin', 'amministratore'],
                    ],
                ]
            ],
            
            // 9. GESTIONI AMMINISTRATIVE
            [
                'icon' => 'fa-solid fa-cogs',
                'label' => __('menu.gestioni'),
                'route' => $panelPrefix . 'gestioni.index',
                'roles' => ['admin', 'super-admin', 'amministratore'],
            ],
            
            // 10. SUPPORTO & ASSISTENZA
            [
                'icon' => 'fa-solid fa-life-ring',
                'label' => __('menu.supporto'),
                'route' => '#',
                'roles' => ['admin', 'super-admin', 'amministratore', 'collaboratore', 'condomino'],
                'expandable' => true,
                'id' => 'supporto-menu',
                'submenu' => [
                    [
                        'icon' => 'fa-solid fa-ticket-alt',
                        'label' => __('menu.tickets'),
                        'route' => $panelPrefix . 'tickets.index',
                        'roles' => ['admin', 'super-admin', 'amministratore', 'collaboratore', 'condomino'],
                    ],
                ]
            ],
            
            // 11. SISTEMA & CONFIGURAZIONI
            [
                'icon' => 'fa-solid fa-cog',
                'label' => __('menu.sistema'),
                'route' => '#',
                'roles' => ['admin', 'super-admin', 'amministratore'],
                'expandable' => true,
                'id' => 'sistema-menu',
                'submenu' => [
                    [
                        'icon' => 'fa-solid fa-users-cog',
                        'label' => __('menu.utenti'),
                        'route' => '#', // TODO: Implementare UserController
                        'roles' => ['admin', 'super-admin'],
                    ],
                    [
                        'icon' => 'fa-solid fa-user-shield',
                        'label' => __('menu.ruoli'),
                        'route' => '#', // TODO: Implementare RoleController avanzato
                        'roles' => ['admin', 'super-admin'],
                    ],
                    [
                        'icon' => 'fa-solid fa-key',
                        'label' => __('menu.api_tokens'),
                        'route' => $panelPrefix . 'api-tokens.index',
                        'roles' => ['admin', 'super-admin'],
                    ],
                    [
                        'icon' => 'fa-solid fa-sliders-h',
                        'label' => __('menu.impostazioni'),
                        'route' => $panelPrefix . 'impostazioni',
                        'roles' => ['admin', 'super-admin', 'amministratore'],
                    ],
                ]
            ],
        ];
    @endphp
    
    {{-- Header fisso con info stabile --}}
    <div class="px-2 pt-3 pb-2 bg-yellow-200 dark:bg-gray-700 border-b-2 border-indigo-500 dark:border-yellow-500 sticky top-0 z-20">
        <div class="flex flex-col items-center">
            <span class="text-xs text-gray-700 dark:text-gray-300 mb-1">{{ $annoAttivo }}/{{ $gestione }}</span>
            <div class="flex items-center gap-2 bg-yellow-200 dark:bg-gray-800 rounded px-2 py-1 shadow" style="min-width: 160px;">
                <div class="flex flex-col justify-center items-center mr-1">
                    <button id="prev-stabile" class="w-6 h-6 flex items-center justify-center rounded bg-indigo-500 text-white hover:bg-indigo-700 text-xs mb-1" title="Stabile precedente">
                        <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-chevron-up" width="18" height="18" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><polyline points="6 15 12 9 18 15" /></svg>
                    </button>
                    <button id="next-stabile" class="w-6 h-6 flex items-center justify-center rounded bg-indigo-500 text-white hover:bg-indigo-700 text-xs" title="Stabile successivo">
                        <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-chevron-down" width="18" height="18" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><polyline points="6 9 12 15 18 9" /></svg>
                    </button>
                </div>
                <button id="current-stabile" class="flex-1 text-base font-semibold text-gray-900 dark:text-gray-100 bg-transparent hover:bg-yellow-300 dark:hover:bg-gray-700 focus:outline-none focus:ring-2 focus:ring-indigo-500 transition text-center" title="Cambia stabile">
                    {{ $stabileAttivo }}
                </button>
            </div>
            <button id="toggle-darkmode" class="mt-2 px-2 py-1 rounded bg-gray-800 dark:bg-yellow-600 text-yellow-300 dark:text-gray-900 hover:bg-gray-900 dark:hover:bg-yellow-700 text-xs flex items-center gap-1" title="Attiva/disattiva modalità scura">
                <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-moon w-4 h-4 dark:hidden" width="16" height="16" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M12 3h.393a7.5 7.5 0 0 0 7.92 12.446a9 9 0 1 1 -8.313 -12.454z" /></svg>
                <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-sun w-4 h-4 hidden dark:block" width="16" height="16" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><circle cx="12" cy="12" r="4" /><path d="m3 12h1m8 -9v1m8 8h1m-9 8v1m-6.4 -15.4l.7 .7m12.1 -.7l-.7 .7m0 11.4l.7 .7m-12.1 -.7l-.7 .7" /></svg>
                <span class="hidden md:inline">{{ __('Dark Mode') }}</span>
            </button>
        </div>
    </div>

    {{-- Menu principale scrollabile --}}
    <div class="flex-1 overflow-y-auto py-4">
        <ul class="space-y-1">
            @foreach($mainMenu as $menuItem)
                @if(auth()->check() && array_intersect($userRoles, $menuItem['roles']))
                    <li>
                        @if(isset($menuItem['expandable']) && $menuItem['expandable'])
                            {{-- Menu espandibile --}}
                            <div x-data="{ open: false }" class="relative">
                                <button @click="open = !open" 
                                        class="w-full flex items-center justify-between px-3 py-2 text-sm font-medium text-gray-700 dark:text-gray-200 rounded-lg hover:bg-yellow-400 dark:hover:bg-gray-700 group transition-all duration-200">
                                    <div class="flex items-center">
                                        <i class="{{ $menuItem['icon'] }} w-5 h-5 mr-3 text-indigo-600 dark:text-yellow-400"></i>
                                        <span>{{ $menuItem['label'] }}</span>
                                    </div>
                                    <svg class="w-4 h-4 transition-transform duration-200" :class="{'rotate-180': open}" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"></path>
                                    </svg>
                                </button>
                                
                                {{-- Sottomenu --}}
                                <div x-show="open" x-transition class="mt-1 ml-6 space-y-1">
                                    @foreach($menuItem['submenu'] as $subItem)
                                        @if(auth()->check() && array_intersect($userRoles, $subItem['roles']))
                                            <a href="{{ $subItem['route'] != '#' ? route($subItem['route']) : '#' }}" 
                                               class="flex items-center px-3 py-2 text-sm text-gray-600 dark:text-gray-300 rounded-lg hover:bg-yellow-300 dark:hover:bg-gray-600 transition-colors duration-200 {{ $subItem['route'] == '#' ? 'cursor-not-allowed opacity-50' : '' }}">
                                                <i class="{{ $subItem['icon'] }} w-4 h-4 mr-2 text-gray-500 dark:text-gray-400"></i>
                                                <span>{{ $subItem['label'] }}</span>
                                                @if($subItem['route'] == '#')
                                                    <span class="ml-auto text-xs bg-orange-200 dark:bg-orange-600 text-orange-800 dark:text-orange-200 px-1 rounded">TODO</span>
                                                @endif
                                            </a>
                                        @endif
                                    @endforeach
                                </div>
                            </div>
                        @else
                            {{-- Menu normale --}}
                            <a href="{{ $menuItem['route'] != '#' ? route($menuItem['route']) : '#' }}" 
                               class="flex items-center px-3 py-2 text-sm font-medium text-gray-700 dark:text-gray-200 rounded-lg hover:bg-yellow-400 dark:hover:bg-gray-700 group transition-all duration-200 {{ $menuItem['route'] == '#' ? 'cursor-not-allowed opacity-50' : '' }}">
                                <i class="{{ $menuItem['icon'] }} w-5 h-5 mr-3 text-indigo-600 dark:text-yellow-400"></i>
                                <span>{{ $menuItem['label'] }}</span>
                                @if($menuItem['route'] == '#')
                                    <span class="ml-auto text-xs bg-orange-200 dark:bg-orange-600 text-orange-800 dark:text-orange-200 px-1 rounded">TODO</span>
                                @endif
                            </a>
                        @endif
                    </li>
                @endif
            @endforeach
        </ul>
    </div>

    {{-- Footer con info utente --}}
    <div class="border-t border-gray-300 dark:border-gray-600 pt-4 mt-4">
        <div class="px-3">
            <div class="flex items-center">
                <div class="flex-shrink-0">
                    <div class="w-8 h-8 bg-indigo-500 rounded-full flex items-center justify-center">
                        <span class="text-white text-sm font-semibold">{{ substr(auth()->user()->name ?? 'U', 0, 1) }}</span>
                    </div>
                </div>
                <div class="ml-3 min-w-0 flex-1">
                    <p class="text-sm font-medium text-gray-700 dark:text-gray-200 truncate">
                        {{ auth()->user()->name ?? 'Utente' }}
                    </p>
                    <p class="text-xs text-gray-500 dark:text-gray-400 truncate">
                        {{ implode(', ', $userRoles) }}
                    </p>
                </div>
            </div>
        </div>
    </div>
</nav>

{{-- Alpine.js per interattività menu --}}
@push('scripts')
<script>
    // Menu interattività aggiuntiva se necessaria
    document.addEventListener('DOMContentLoaded', function() {
        // Gestione menu collassabile se necessario
    });
</script>
@endpush
