{{-- 
========================================
BREADCRUMB MODULARE
========================================
Breadcrumb intelligente con auto-generazione basata su route
e personalizzazione manuale.

Props:
- $items (array): Items personalizzati del breadcrumb
- $showHome (bool): Mostra link Home
- $separator (string): Separatore custom
- $autoGenerate (bool): Auto-genera da route corrente

Autore: NetGesCon Development Team
Data: 2024
========================================
--}}

@props([
    'items' => [],
    'showHome' => true,
    'separator' => null,
    'autoGenerate' => true
])

@php
// Auto-genera breadcrumb se non fornito manualmente
if (empty($items) && $autoGenerate) {
    $items = generateBreadcrumbFromRoute();
}

// Funzione helper per generare breadcrumb dalla route corrente
function generateBreadcrumbFromRoute() {
    $routeName = request()->route()->getName();
    $segments = explode('.', $routeName);
    $breadcrumb = [];
    
    // Mapping delle route ai nomi visualizzati
    $routeNames = [
        'dashboard' => 'Dashboard',
        'admin' => 'Amministrazione',
        'superadmin' => 'Super Admin',
        'condomino' => 'Area Condomino',
        'stabili' => 'Stabili',
        'condomini' => 'Condomini',
        'tickets' => 'Tickets',
        'contabilita' => 'Contabilità',
        'fiscale' => 'Fiscale',
        'assemblee' => 'Assemblee',
        'comunicazioni' => 'Comunicazioni',
        'documenti' => 'Documenti',
        'fornitori' => 'Fornitori',
        'manutentori' => 'Manutentori',
        'users' => 'Utenti',
        'settings' => 'Impostazioni',
        'index' => 'Lista',
        'create' => 'Nuovo',
        'edit' => 'Modifica',
        'show' => 'Dettagli'
    ];
    
    $path = '';
    foreach ($segments as $index => $segment) {
        $path .= ($index > 0 ? '.' : '') . $segment;
        
        // Skip alcuni segmenti finali
        if (in_array($segment, ['index']) && $index === count($segments) - 1) {
            continue;
        }
        
        $name = $routeNames[$segment] ?? ucfirst($segment);
        
        // Costruisci URL se possibile
        $url = '#';
        try {
            if ($index < count($segments) - 1) {
                $testRoute = implode('.', array_slice($segments, 0, $index + 1)) . '.index';
                if (Route::has($testRoute)) {
                    $url = route($testRoute);
                }
            }
        } catch (Exception $e) {
            // Route non esistente, usa #
        }
        
        $breadcrumb[] = [
            'name' => $name,
            'url' => $url,
            'active' => $index === count($segments) - 1
        ];
    }
    
    return $breadcrumb;
}
@endphp

@if(!empty($items) || $showHome)
    <nav aria-label="breadcrumb" class="netgescon-breadcrumb">
        <ol class="breadcrumb mb-0">
            
            {{-- Home link --}}
            @if($showHome)
                <li class="breadcrumb-item">
                    <a href="{{ route('dashboard') }}" class="text-decoration-none">
                        <i class="fas fa-home me-1"></i>Home
                    </a>
                </li>
            @endif
            
            {{-- Breadcrumb items --}}
            @foreach($items as $item)
                <li class="breadcrumb-item {{ $item['active'] ?? false ? 'active' : '' }}">
                    @if(isset($item['active']) && $item['active'])
                        <span class="text-muted">{{ $item['name'] }}</span>
                    @else
                        <a href="{{ $item['url'] ?? '#' }}" class="text-decoration-none">
                            @if(isset($item['icon']))
                                <i class="{{ $item['icon'] }} me-1"></i>
                            @endif
                            {{ $item['name'] }}
                        </a>
                    @endif
                </li>
            @endforeach
            
        </ol>
    </nav>
@endif

{{-- CSS per breadcrumb --}}
@push('styles')
<style>
.netgescon-breadcrumb {
    padding: 1rem 0;
    background: transparent;
}

.netgescon-breadcrumb .breadcrumb {
    background: transparent;
    padding: 0;
    margin: 0;
}

.netgescon-breadcrumb .breadcrumb-item {
    font-size: 0.875rem;
}

.netgescon-breadcrumb .breadcrumb-item + .breadcrumb-item::before {
    @if($separator)
        content: "{{ $separator }}";
    @else
        content: ">";
    @endif
    color: var(--bs-gray-500);
    padding: 0 0.5rem;
}

.netgescon-breadcrumb .breadcrumb-item a {
    color: var(--bs-primary);
    transition: color 0.2s ease;
}

.netgescon-breadcrumb .breadcrumb-item a:hover {
    color: var(--bs-primary);
    text-decoration: underline !important;
}

.netgescon-breadcrumb .breadcrumb-item.active {
    color: var(--bs-gray-600);
}

/* Tema scuro */
.dark .netgescon-breadcrumb .breadcrumb-item.active {
    color: var(--bs-gray-400);
}

.dark .netgescon-breadcrumb .breadcrumb-item + .breadcrumb-item::before {
    color: var(--bs-gray-600);
}

/* Responsive */
@media (max-width: 768px) {
    .netgescon-breadcrumb {
        padding: 0.5rem 0;
    }
    
    .netgescon-breadcrumb .breadcrumb-item {
        font-size: 0.8rem;
    }
    
    /* Nascondi alcuni elementi su mobile */
    .netgescon-breadcrumb .breadcrumb-item:not(.active):not(:first-child):not(:last-child) {
        display: none;
    }
    
    /* Mostra solo Home -> ... -> Corrente */
    .netgescon-breadcrumb .breadcrumb-item:nth-last-child(2):not(:first-child)::before {
        content: "...";
        padding: 0 0.5rem;
    }
}
</style>
@endpush

{{-- JavaScript per funzionalità aggiuntive --}}
@push('scripts')
<script>
document.addEventListener('DOMContentLoaded', function() {
    // Aggiungi tooltips ai breadcrumb su mobile
    const breadcrumbItems = document.querySelectorAll('.netgescon-breadcrumb .breadcrumb-item a');
    
    breadcrumbItems.forEach(item => {
        item.addEventListener('mouseenter', function() {
            // Aggiungi logica per tooltip se necessario
        });
    });
    
    // Salva breadcrumb per navigazione indietro
    const currentBreadcrumb = document.querySelector('.netgescon-breadcrumb');
    if (currentBreadcrumb) {
        const breadcrumbData = Array.from(currentBreadcrumb.querySelectorAll('.breadcrumb-item')).map(item => {
            const link = item.querySelector('a');
            return {
                name: item.textContent.trim(),
                url: link ? link.href : null,
                active: item.classList.contains('active')
            };
        });
        
        sessionStorage.setItem('lastBreadcrumb', JSON.stringify(breadcrumbData));
    }
});
</script>
@endpush
