{{-- Sidebar Principal --}}
@php
    use App\Helpers\MenuHelper;
    $user = auth()->user();
    $menus = MenuHelper::getMenusUsuario($user);

    // Obtener iniciales del nombre
    $initials = '';
    $nameParts = explode(' ', $user->name);
    if (count($nameParts) >= 2) {
        $initials = strtoupper(substr($nameParts[0], 0, 1) . substr($nameParts[1], 0, 1));
    } else {
        $initials = strtoupper(substr($user->name, 0, 2));
    }

    // Obtener perfil (desde usuario vinculado o directo)
    $perfilNombre = 'Sin perfil';
    if ($user->esInterno() && $user->usuario && $user->usuario->perfil) {
        $perfilNombre = $user->usuario->perfil->per_nombre;
    } elseif ($user->perfil) {
        $perfilNombre = $user->perfil->per_nombre;
    }
@endphp

<aside class="main-sidebar" id="mainSidebar">
    {{-- Brand --}}
    <div class="sidebar-brand">
        <img src="{{ asset('assets/img/logo-siryc-white.png') }}" alt="SIRYC" class="logo-full" onerror="this.style.display='none'">
        <img src="{{ asset('assets/img/logo_miniatura.png') }}" alt="SIRYC" class="logo-mini" onerror="this.src='{{ asset('assets/img/logo-siryc-white.png') }}'">
        <span class="brand-text">SIRYC</span>
    </div>

    {{-- Navigation --}}
    <nav class="sidebar-nav-container">
        <ul class="sidebar-nav">
            {{-- Dashboard (siempre visible) --}}
            <li class="nav-item">
                <a href="{{ route('dashboard') }}" class="nav-link {{ request()->routeIs('dashboard') ? 'active' : '' }}">
                    <i class="fas fa-tachometer-alt"></i>
                    <span>Dashboard</span>
                </a>
                <div class="nav-tooltip">Dashboard</div>
            </li>

            {{-- Menús dinámicos según permisos --}}
            @foreach($menus as $seccion => $config)
                <li class="nav-section">{{ $config['titulo'] }}</li>

                @foreach($config['items'] as $item)
                    <li class="nav-item">
                        <a href="{{ route($item['ruta']) }}"
                           class="nav-link {{ request()->routeIs(str_replace('.index', '.*', $item['ruta'])) ? 'active' : '' }}">
                            <i class="fas {{ $item['icono'] }}"></i>
                            <span>{{ $item['nombre'] }}</span>
                            @if(isset($item['badge_count']) && $item['badge_count'] > 0)
                                <span class="badge {{ $item['badge']['clase'] }}">{{ $item['badge_count'] }}</span>
                            @endif
                        </a>
                        <div class="nav-tooltip">{{ $item['nombre'] }}</div>
                    </li>
                @endforeach
            @endforeach
        </ul>
    </nav>

    {{-- User Panel --}}
    <div class="user-panel">
        <div class="user-avatar">
            {{ $initials }}
        </div>
        <div class="user-info">
            <div class="user-name">{{ $user->name }}</div>
            <div class="user-role">{{ $perfilNombre }}</div>
        </div>
    </div>
</aside>
