{{-- Menu laterale dinamico basato su permessi utente --}}
<div class="sidebar bg-dark text-white" id="sidebar">
    <div class="sidebar-header p-3 border-bottom border-secondary">
        <div class="d-flex align-items-center">
            <div class="text-center me-2" style="width: 40px; height: 40px; background: linear-gradient(135deg, #2563eb, #0284c7); border-radius: 8px; display: flex; align-items: center; justify-content: center;">
                <i class="fas fa-building text-white fa-lg"></i>
            </div>
            <div>
                <h5 class="mb-0 text-white">NetGesCon</h5>
                <small class="text-muted">Gestione Condominiale</small>
            </div>
        </div>
    </div>

    <div class="sidebar-content">
        {{-- Informazioni utente con switch ruoli --}}
        <div class="user-info p-3 border-bottom border-secondary">
            <div class="d-flex align-items-center">
                <div class="avatar me-2">
                    <i class="fas fa-user-circle fa-2x text-info"></i>
                </div>
                <div class="flex-grow-1">
                    <div class="fw-medium">{{ auth()->user()->name ?? 'Utente' }}</div>
                    
                    {{-- Switch ruoli se l'utente ha ruoli multipli --}}
                    @if(isset($userRoles) && count($userRoles) > 1)
                        <div class="dropdown mt-1">
                            <button class="btn btn-sm btn-outline-light dropdown-toggle w-100" type="button" data-bs-toggle="dropdown">
                                <small>{{ ucfirst(str_replace('_', ' ', $activeRole ?? $userRole ?? 'user')) }}</small>
                            </button>
                            <ul class="dropdown-menu w-100">
                                @foreach($userRoles as $role)
                                    <li>
                                        <a class="dropdown-item {{ $role === ($activeRole ?? $userRole) ? 'active' : '' }}" 
                                           href="{{ route('switch.role', $role) }}">
                                            @switch($role)
                                                @case('super_admin')
                                                    <i class="fas fa-crown text-danger me-2"></i>Super Admin
                                                    @break
                                                @case('amministratore')
                                                    <i class="fas fa-user-tie text-warning me-2"></i>Amministratore
                                                    @break
                                                @case('collaboratore')
                                                    <i class="fas fa-users text-primary me-2"></i>Collaboratore
                                                    @break
                                                @case('condomino')
                                                    <i class="fas fa-home text-success me-2"></i>Condomino
                                                    @break
                                                @case('proprietario')
                                                    <i class="fas fa-key text-info me-2"></i>Proprietario
                                                    @break
                                                @case('inquilino')
                                                    <i class="fas fa-user-clock text-secondary me-2"></i>Inquilino
                                                    @break
                                                @case('manutentore')
                                                    <i class="fas fa-tools text-brown me-2"></i>Manutentore
                                                    @break
                                                @default
                                                    <i class="fas fa-user me-2"></i>{{ ucfirst(str_replace('_', ' ', $role)) }}
                                            @endswitch
                                        </a>
                                    </li>
                                @endforeach
                            </ul>
                        </div>
                    @else
                        <small class="text-muted">{{ ucfirst(str_replace('_', ' ', $userRole ?? 'user')) }}</small>
                    @endif
                </div>
                
                {{-- Badge ruolo principale --}}
                @if(($userRole ?? 'user') === 'super_admin')
                    <span class="badge bg-danger">SUPER</span>
                @elseif(($userRole ?? 'user') === 'amministratore')
                    <span class="badge bg-warning">ADMIN</span>
                @elseif(($userRole ?? 'user') === 'condomino')
                    <span class="badge bg-success">HOME</span>
                @endif
            </div>
        </div>

        {{-- Menu Navigation --}}
        <nav class="sidebar-nav">
            <ul class="nav flex-column">
                {{-- Dashboard - Sempre visibile se autenticato --}}
                @if($userPermissions['dashboard'] ?? false)
                <li class="nav-item">
                    <a href="{{ route('dashboard') }}" class="nav-link {{ request()->routeIs('dashboard') ? 'active' : '' }}">
                        <i class="fas fa-tachometer-alt me-2"></i>
                        <span>Dashboard</span>
                    </a>
                </li>
                @endif

                {{-- Gestione Stabili --}}
                @if($userPermissions['stabili'] ?? false)
                <li class="nav-item">
                    <a href="#stabiliSubmenu" class="nav-link collapse-toggle dashboard-nav-link" 
                       data-bs-toggle="collapse" role="button" aria-expanded="false" data-section="stabili">
                        <i class="fas fa-building me-2"></i>
                        <span>Gestione Stabili</span>
                        <i class="fas fa-chevron-down ms-auto collapse-icon"></i>
                    </a>
                    <div class="collapse" id="stabiliSubmenu">
                        <ul class="nav flex-column ms-3">
                            <li class="nav-item">
                                <a href="#" class="nav-link dashboard-nav-link" data-section="stabili" data-action="list">
                                    <i class="fas fa-list me-2"></i>Lista Stabili
                                </a>
                            </li>
                            @if($userPermissions['stabili'] !== 'limited')
                            <li class="nav-item">
                                <a href="#" class="nav-link dashboard-nav-link" data-section="stabili" data-action="create-stabile">
                                    <i class="fas fa-plus me-2"></i>Nuovo Stabile
                                </a>
                            </li>
                            @endif
                        </ul>
                    </div>
                </li>
                @endif

                {{-- Gestione Condomini --}}
                @if($userPermissions['condomini'] ?? false)
                <li class="nav-item">
                    <a href="#condominiSubmenu" class="nav-link collapse-toggle dashboard-nav-link" 
                       data-bs-toggle="collapse" role="button" aria-expanded="false" data-section="condomini">
                        <i class="fas fa-users me-2"></i>
                        <span>Gestione Condomini</span>
                        <i class="fas fa-chevron-down ms-auto collapse-icon"></i>
                    </a>
                    <div class="collapse" id="condominiSubmenu">
                        <ul class="nav flex-column ms-3">
                            <li class="nav-item">
                                <a href="#" class="nav-link dashboard-nav-link" data-section="condomini" data-action="list">
                                    <i class="fas fa-list me-2"></i>Lista Condomini
                                </a>
                            </li>
                            @if($userPermissions['condomini'] !== 'limited')
                            <li class="nav-item">
                                <a href="#" class="nav-link dashboard-nav-link" data-section="condomini" data-action="create">
                                    <i class="fas fa-plus me-2"></i>Nuovo Condomino
                                </a>
                            </li>
                            @endif
                        </ul>
                    </div>
                </li>
                @endif

                {{-- Gestione Tickets --}}
                @if($userPermissions['tickets'] ?? false)
                <li class="nav-item">
                    <a href="#ticketsSubmenu" class="nav-link collapse-toggle dashboard-nav-link" 
                       data-bs-toggle="collapse" role="button" aria-expanded="false" data-section="tickets">
                        <i class="fas fa-ticket-simple me-2"></i>
                        <span>Gestione Tickets</span>
                        <i class="fas fa-chevron-down ms-auto collapse-icon"></i>
                    </a>
                    <div class="collapse" id="ticketsSubmenu">
                        <ul class="nav flex-column ms-3">
                            <li class="nav-item">
                                <a href="#" class="nav-link dashboard-nav-link" data-section="tickets" data-action="list">
                                    <i class="fas fa-list me-2"></i>Lista Tickets
                                </a>
                            </li>
                            @if($userPermissions['tickets'] !== 'limited')
                            <li class="nav-item">
                                <a href="#" class="nav-link dashboard-nav-link" data-section="tickets" data-action="create">
                                    <i class="fas fa-plus me-2"></i>Nuovo Ticket
                                </a>
                            </li>
                            @endif
                        </ul>
                    </div>
                </li>
                @endif

                {{-- Gestione Unità --}}
                @if($userPermissions['unita'] ?? false)
                <li class="nav-item">
                    <a href="#unitaSubmenu" class="nav-link collapse-toggle" data-bs-toggle="collapse" role="button" aria-expanded="false">
                        <i class="fas fa-home me-2"></i>
                        <span>Unità Immobiliari</span>
                        <i class="fas fa-chevron-down ms-auto collapse-icon"></i>
                    </a>
                    <div class="collapse" id="unitaSubmenu">
                        <ul class="nav flex-column ms-3">
                            <li class="nav-item">
                                <a href="{{ route('admin.unita.index') }}" class="nav-link">
                                    <i class="fas fa-list me-2"></i>Lista Unità
                                </a>
                            </li>
                            @if($userPermissions['unita'] !== 'limited')
                            <li class="nav-item">
                                <a href="{{ route('admin.unita.create') }}" class="nav-link">
                                    <i class="fas fa-plus me-2"></i>Nuova Unità
                                </a>
                            </li>
                            @endif
                        </ul>
                    </div>
                </li>
                @endif

                {{-- Gestione Soggetti --}}
                @if($userPermissions['soggetti'] ?? false)
                <li class="nav-item">
                    <a href="#soggettiSubmenu" class="nav-link collapse-toggle" data-bs-toggle="collapse" role="button" aria-expanded="false">
                        <i class="fas fa-users me-2"></i>
                        <span>Gestione Soggetti</span>
                        <i class="fas fa-chevron-down ms-auto collapse-icon"></i>
                    </a>
                    <div class="collapse" id="soggettiSubmenu">
                        <ul class="nav flex-column ms-3">
                            <li class="nav-item">
                                <a href="{{ route('admin.soggetti.index') }}" class="nav-link {{ request()->routeIs('admin.soggetti.*') ? 'active' : '' }}">
                                    <i class="fas fa-address-book me-2"></i>Anagrafica
                                </a>
                            </li>
                            <li class="nav-item">
                                <a href="{{ route('admin.fornitori.index') }}" class="nav-link {{ request()->routeIs('admin.fornitori.*') ? 'active' : '' }}">
                                    <i class="fas fa-truck me-2"></i>Fornitori
                                </a>
                            </li>
                        </ul>
                    </div>
                </li>
                @endif

                {{-- Contabilità --}}
                @if($userPermissions['contabilita'] ?? false)
                <li class="nav-item">
                    <a href="#contabilitaSubmenu" class="nav-link collapse-toggle dashboard-nav-link" 
                       data-bs-toggle="collapse" role="button" aria-expanded="false" data-section="contabilita">
                        <i class="fas fa-calculator me-2"></i>
                        <span>Contabilità</span>
                        <i class="fas fa-chevron-down ms-auto collapse-icon"></i>
                    </a>
                    <div class="collapse" id="contabilitaSubmenu">
                        <ul class="nav flex-column ms-3">
                            <li class="nav-item">
                                <a href="#" class="nav-link dashboard-nav-link" data-section="contabilita" data-action="movimenti">
                                    <i class="fas fa-exchange-alt me-2"></i>Movimenti
                                </a>
                            </li>
                            <li class="nav-item">
                                <a href="#" class="nav-link dashboard-nav-link" data-section="contabilita" data-action="bilanci">
                                    <i class="fas fa-chart-bar me-2"></i>Bilanci
                                </a>
                            </li>
                        </ul>
                    </div>
                </li>
                @endif

                {{-- Fatture Acquisto --}}
                @if($userPermissions['fatture_acquisto'] ?? false)
                <li class="nav-item">
                    <a href="{{ route('admin.fatture-acquisto.index') }}" class="nav-link">
                        <i class="fas fa-file-invoice me-2"></i>
                        <span>Fatture Acquisto</span>
                    </a>
                </li>
                @endif

                {{-- Fatture Emesse --}}
                @if($userPermissions['fatture_emesse'] ?? false)
                <li class="nav-item">
                    <a href="{{ route('admin.fatture-emesse.index') }}" class="nav-link">
                        <i class="fas fa-file-invoice-dollar me-2"></i>
                        <span>Fatture Emesse</span>
                    </a>
                </li>
                @endif

                {{-- Rate --}}
                @if($userPermissions['rate'] ?? false)
                <li class="nav-item">
                    <a href="{{ route('admin.rate.index') }}" class="nav-link">
                        <i class="fas fa-money-bill-wave me-2"></i>
                        <span>Gestione Rate</span>
                    </a>
                </li>
                @endif

                {{-- Assemblee --}}
                @if($userPermissions['assemblee'] ?? false)
                <li class="nav-item">
                    <a href="#assemblee Submenu" class="nav-link collapse-toggle" data-bs-toggle="collapse" role="button" aria-expanded="false">
                        <i class="fas fa-users-cog me-2"></i>
                        <span>Assemblee</span>
                        <i class="fas fa-chevron-down ms-auto collapse-icon"></i>
                    </a>
                    <div class="collapse" id="assemblee Submenu">
                        <ul class="nav flex-column ms-3">
                            <li class="nav-item">
                                <a href="{{ route('admin.assemblee.index') }}" class="nav-link">
                                    <i class="fas fa-gavel me-2"></i>Lista Assemblee
                                </a>
                            </li>
                            @if($userPermissions['calendario'] ?? false)
                            <li class="nav-item">
                                <a href="{{ route('admin.calendario.index') }}" class="nav-link">
                                    <i class="fas fa-calendar me-2"></i>Calendario
                                </a>
                            </li>
                            @endif
                        </ul>
                    </div>
                </li>
                @endif

                {{-- Area Manutentori --}}
                @if($userPermissions['manutentori'] ?? false)
                <li class="nav-item">
                    <a href="#manutentoriSubmenu" class="nav-link collapse-toggle" data-bs-toggle="collapse" role="button" aria-expanded="false">
                        <i class="fas fa-tools me-2"></i>
                        <span>Area Manutentori</span>
                        <i class="fas fa-chevron-down ms-auto collapse-icon"></i>
                    </a>
                    <div class="collapse" id="manutentoriSubmenu">
                        <ul class="nav flex-column ms-3">
                            <li class="nav-item">
                                <a href="#" class="nav-link disabled" title="Dashboard Manutenzioni - Funzionalità in sviluppo">
                                    <i class="fas fa-wrench me-2"></i>Dashboard Manutenzioni
                                </a>
                            </li>
                            @if($userPermissions['xml_fatture'] ?? false)
                            <li class="nav-item">
                                <a href="#" class="nav-link disabled" title="Preparazione XML - Funzionalità in sviluppo">
                                    <i class="fas fa-file-code me-2"></i>Preparazione XML
                                </a>
                            </li>
                            @endif
                        </ul>
                    </div>
                </li>
                @endif

                {{-- Gestione Documentale --}}
                @if($userPermissions['documenti'] ?? true)
                <li class="nav-item">
                    <a href="#documentiSubmenu" class="nav-link collapse-toggle" data-bs-toggle="collapse" role="button" aria-expanded="false">
                        <i class="fas fa-file-archive me-2"></i>
                        <span>Gestione Documentale</span>
                        <i class="fas fa-chevron-down ms-auto collapse-icon"></i>
                    </a>
                    <div class="collapse" id="documentiSubmenu">
                        <ul class="nav flex-column ms-3">
                            <li class="nav-item">
                                <a href="#" class="nav-link dashboard-nav-link" data-section="documenti" data-action="protocollo">
                                    <i class="fas fa-stamp me-2"></i>Protocollo Generale
                                </a>
                            </li>
                            <li class="nav-item">
                                <a href="#" class="nav-link dashboard-nav-link" data-section="documenti" data-action="archivio-stabili">
                                    <i class="fas fa-building me-2"></i>Archivio Stabili
                                </a>
                            </li>
                            <li class="nav-item">
                                <a href="#" class="nav-link dashboard-nav-link" data-section="documenti" data-action="archivio-gestionale">
                                    <i class="fas fa-folder-open me-2"></i>Archivio Gestionale
                                </a>
                            </li>
                            <li class="nav-item">
                                <a href="#" class="nav-link dashboard-nav-link" data-section="documenti" data-action="etichette-barcode">
                                    <i class="fas fa-barcode me-2"></i>Etichette & Barcode
                                </a>
                            </li>
                            <li class="nav-item">
                                <a href="#" class="nav-link dashboard-nav-link" data-section="documenti" data-action="controllo-integrita">
                                    <i class="fas fa-shield-alt me-2"></i>Controllo Integrità
                                </a>
                            </li>
                            <li class="nav-item">
                                <a href="#" class="nav-link dashboard-nav-link" data-section="documenti" data-action="ricerca-avanzata">
                                    <i class="fas fa-search me-2"></i>Ricerca Avanzata
                                </a>
                            </li>
                        </ul>
                    </div>
                </li>
                @endif

                {{-- Separatore --}}
                @if($userPermissions['amministrazione'] ?? false)
                <li class="nav-divider my-2"></li>
                @endif

                {{-- Amministrazione --}}
                @if($userPermissions['amministrazione'] ?? false)
                <li class="nav-item">
                    <a href="#adminSubmenu" class="nav-link collapse-toggle" data-bs-toggle="collapse" role="button" aria-expanded="false">
                        <i class="fas fa-cog me-2"></i>
                        <span>Amministrazione</span>
                        <i class="fas fa-chevron-down ms-auto collapse-icon"></i>
                    </a>
                    <div class="collapse" id="adminSubmenu">
                        <ul class="nav flex-column ms-3">
                            @if($userPermissions['gestione_permessi'] ?? false)
                            <li class="nav-item">
                                <a href="{{ route('admin.permissions.index') }}" class="nav-link">
                                    <i class="fas fa-user-shield me-2"></i>Gestione Permessi
                                </a>
                            </li>
                            @endif
                            <li class="nav-item">
                                <a href="{{ route('admin.configurazioni.index') }}" class="nav-link">
                                    <i class="fas fa-sliders-h me-2"></i>Configurazioni
                                </a>
                            </li>
                            <li class="nav-item">
                                <a href="{{ route('admin.backup.index') }}" class="nav-link">
                                    <i class="fas fa-database me-2"></i>Backup Sistema
                                </a>
                            </li>
                        </ul>
                    </div>
                </li>
                @endif

                {{-- Super Admin --}}
                @if($userPermissions['super_admin'] ?? false)
                <li class="nav-item">
                    <a href="#superAdminSubmenu" class="nav-link collapse-toggle text-danger" data-bs-toggle="collapse" role="button" aria-expanded="false">
                        <i class="fas fa-crown me-2"></i>
                        <span>Super Admin</span>
                        <i class="fas fa-chevron-down ms-auto collapse-icon"></i>
                    </a>
                    <div class="collapse" id="superAdminSubmenu">
                        <ul class="nav flex-column ms-3">
                            <li class="nav-item">
                                <a href="#" class="nav-link dashboard-nav-link" data-section="superadmin" data-action="dashboard">
                                    <i class="fas fa-tachometer-alt me-2"></i>Dashboard SuperAdmin
                                </a>
                            </li>
                            <li class="nav-item">
                                <a href="#" class="nav-link dashboard-nav-link" data-section="superadmin" data-action="comuni">
                                    <i class="fas fa-map-marked-alt me-2"></i>Gestione Comuni
                                </a>
                            </li>
                            <li class="nav-item">
                                <a href="#" class="nav-link dashboard-nav-link" data-section="superadmin" data-action="import-data">
                                    <i class="fas fa-file-import me-2"></i>Import Dati Legacy
                                </a>
                            </li>
                            <li class="nav-item">
                                <a href="#" class="nav-link dashboard-nav-link" data-section="superadmin" data-action="archivi-comuni">
                                    <i class="fas fa-archive me-2"></i>Archivi Comuni
                                </a>
                            </li>
                            <li class="nav-item">
                                <a href="#" class="nav-link dashboard-nav-link" data-section="superadmin" data-action="validazione-dati">
                                    <i class="fas fa-check-double me-2"></i>Validazione Dati
                                </a>
                            </li>
                            <li class="nav-item">
                                <a href="#" class="nav-link text-muted disabled" title="Impersonifica - Funzionalità in sviluppo">
                                    <i class="fas fa-mask me-2"></i>Impersonifica Utente
                                </a>
                            </li>
                            <li class="nav-item">
                                <a href="#" class="nav-link text-muted disabled" title="Log Sistema - Funzionalità in sviluppo">
                                    <i class="fas fa-file-alt me-2"></i>Log Sistema
                                </a>
                            </li>
                            <li class="nav-item">
                                <a href="#" class="nav-link text-muted disabled" title="Stato Sistema - Funzionalità in sviluppo">
                                    <i class="fas fa-heartbeat me-2"></i>Stato Sistema
                                </a>
                            </li>
                        </ul>
                    </div>
                </li>
                @endif

                {{-- Menu SuperAdmin - Solo per Super-Admin --}}
                @if(auth()->user()->hasRole('super-admin'))
                <li class="nav-item">
                    <hr class="border-secondary my-2">
                    <a href="#superadminSubmenu" class="nav-link collapse-toggle" 
                       data-bs-toggle="collapse" role="button" aria-expanded="false">
                        <i class="fas fa-crown me-2 text-warning"></i>
                        <span class="text-warning fw-bold">SuperAdmin</span>
                        <i class="fas fa-chevron-down ms-auto collapse-icon text-warning"></i>
                    </a>
                    <div class="collapse" id="superadminSubmenu">
                        <ul class="nav flex-column ms-3">
                            <li class="nav-item">
                                <a href="{{ route('superadmin.dashboard') }}" class="nav-link">
                                    <i class="fas fa-crown me-2 text-warning"></i>Dashboard SuperAdmin
                                </a>
                            </li>
                            <li class="nav-item">
                                <a href="{{ route('superadmin.archivi.index') }}" class="nav-link">
                                    <i class="fas fa-database me-2"></i>Archivi Sistema
                                </a>
                            </li>
                            <li class="nav-item">
                                <a href="{{ route('superadmin.comuni-italiani.index') }}" class="nav-link">
                                    <i class="fas fa-city me-2"></i>Comuni Italiani
                                </a>
                            </li>
                            <li class="nav-item">
                                <a href="{{ route('superadmin.users.index') }}" class="nav-link">
                                    <i class="fas fa-users me-2"></i>Gestione Utenti
                                </a>
                            </li>
                            <li class="nav-item">
                                <a href="{{ route('superadmin.impostazioni.index') }}" class="nav-link">
                                    <i class="fas fa-cog me-2"></i>Impostazioni Sistema
                                </a>
                            </li>
                        </ul>
                    </div>
                </li>
                @endif
            </ul>
        </nav>
    </div>

    {{-- Footer sidebar --}}
    <div class="sidebar-footer p-3 border-top border-secondary mt-auto">
        <div class="d-flex align-items-center justify-content-between">
            <div class="d-flex align-items-center">
                <form method="POST" action="{{ route('logout') }}" class="d-inline">
                    @csrf
                    <button type="submit" class="btn btn-outline-light btn-sm">
                        <i class="fas fa-sign-out-alt me-1"></i>Logout
                    </button>
                </form>
            </div>
            <div class="text-muted">
                <small>v1.0</small>
            </div>
        </div>
    </div>
</div>

{{-- CSS specifico per il menu --}}
<style>
.sidebar {
    width: 280px;
    height: 100vh;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 1000;
    display: flex;
    flex-direction: column;
    transition: transform 0.3s ease;
}

.sidebar-content {
    flex: 1;
    overflow-y: auto;
}

.sidebar .nav-link {
    color: rgba(255, 255, 255, 0.8);
    padding: 0.75rem 1rem;
    border-radius: 0;
    transition: all 0.2s ease;
}

.sidebar .nav-link:hover {
    color: white;
    background-color: rgba(255, 255, 255, 0.1);
}

.sidebar .nav-link.active {
    color: white;
    background-color: var(--bs-primary);
}

.sidebar .nav-divider {
    height: 1px;
    background-color: rgba(255, 255, 255, 0.1);
    margin: 0.5rem 1rem;
}

.collapse-icon {
    transition: transform 0.2s ease;
}

.collapse-toggle[aria-expanded="true"] .collapse-icon {
    transform: rotate(180deg);
}

.avatar {
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
}

@media (max-width: 768px) {
    .sidebar {
        transform: translateX(-100%);
    }
    
    .sidebar.show {
        transform: translateX(0);
    }
}
</style>

{{-- JavaScript per gestione menu --}}
<script>
document.addEventListener('DOMContentLoaded', function() {
    // Gestione collassi menu
    const collapseToggles = document.querySelectorAll('.collapse-toggle');
    collapseToggles.forEach(toggle => {
        toggle.addEventListener('click', function(e) {
            e.preventDefault();
            const targetId = this.getAttribute('href').substring(1);
            const targetElement = document.getElementById(targetId);
            
            if (targetElement) {
                const bsCollapse = new bootstrap.Collapse(targetElement);
            }
        });
    });

    // Mobile menu toggle
    const mobileToggle = document.getElementById('mobile-menu-toggle');
    const sidebar = document.getElementById('sidebar');
    
    if (mobileToggle && sidebar) {
        mobileToggle.addEventListener('click', function() {
            sidebar.classList.toggle('show');
        });
    }
});
</script>
