{{--
    Protera main-nav template override.

    Renders a flat <ul class="nav"> matching the legacy hardcoded
    `.main-nav` markup so toggling MENUS_DB_DRIVEN=true is visually
    neutral. Active state via `is_active` flag (URL prefix match)
    keeps `.nav-link.active` styling intact.

    The `.text-danger` accent on the "Akcijos" promo entry comes
    from the seeder's `css_class` field on the matching item.

    v17.80 (task-9): `type='section'` items render as a non-link
    `<li class="nav-item nav-section" data-menu-section>` with a
    `<span class="nav-link nav-section-label">` so admins can group
    items via section dividers without polluting the link list with
    fake `href="#"` anchors.

    v18.28 (task-27): root items that ship `children` (sections OR
    model links) become Bootstrap dropdown toggles. Click reveals the
    child <ul class="dropdown-menu"> beneath; CSS in web.css also
    opens the dropdown on hover at >=992px so power users don't have
    to click first. `data-menu-section` and `data-menu="<slug>"` hooks
    are preserved for existing E2E specs.

    Children render as flat `<li><a class="dropdown-item">` entries —
    Bootstrap 5 does NOT ship multi-level dropdowns natively, so a
    deeper tree collapses to a single dropdown-menu layer.
--}}
@props([
    'tree' => [],
    'menu' => null,
])

@php
    // Stable test hook — `data-menu="{slug}"` lets specs scope link
    // assertions to a single menu's <ul> without hardcoding fragile
    // class chains. Skipped silently if $menu wasn't passed (defensive
    // — every render path through MenuRenderer always supplies it).
    $menuSlug = $menu?->slug;
@endphp
<ul {{ $attributes->merge(['class' => 'nav']) }} @if($menuSlug) data-menu="{{ $menuSlug }}" @endif>
    @foreach($tree as $node)
        @php
            $type = $node['type'] ?? 'custom';
            $label = trim((string) ($node['label'] ?? ''));
            $hasChildren = ! empty($node['has_children']) && ! empty($node['children']);
            $isActive = ! empty($node['is_active']);
            $itemClasses = trim('nav-item ' . ($hasChildren ? 'dropdown ' : '') . ($node['css_class'] ?? ''));
            $url = $node['url'] ?? null;
        @endphp
        @if($type === 'section' && ! $hasChildren)
            <li class="nav-item nav-section" data-menu-section>
                <span class="nav-link nav-section-label fw-semibold text-uppercase text-muted small"
                      role="presentation">
                    @if(! empty($node['icon']))
                        <i class="bi {{ $node['icon'] }}"></i>
                    @endif
                    {{ $label !== '' ? $label : ($node['identifier'] ?? '') }}
                </span>
            </li>
        @else
            <li class="{{ $itemClasses }}"
                @if($type === 'section') data-menu-section @endif>
                @php
                    $linkClass = trim('nav-link '
                        . ($hasChildren ? 'dropdown-toggle ' : '')
                        . ($isActive ? 'active ' : '')
                        . ($node['css_class'] ?? ''));
                @endphp
                <a class="{{ $linkClass }}"
                   href="{{ $url ?: '#' }}"
                   @if($hasChildren) data-bs-toggle="dropdown" aria-expanded="false" @endif
                   @if($node['target'] ?? null) target="{{ $node['target'] }}" rel="noopener" @endif>
                    @if(! empty($node['icon']))
                        <i class="bi {{ $node['icon'] }}"></i>
                    @endif
                    {{ $label !== '' ? $label : ($node['identifier'] ?? '') }}
                </a>

                @if($hasChildren)
                    <ul class="dropdown-menu" data-menu-dropdown>
                        @foreach($node['children'] as $child)
                            @php
                                $childLabel = trim((string) ($child['label'] ?? ''));
                                $childUrl = $child['url'] ?? null;
                                $childActive = ! empty($child['is_active']);
                                $childClass = trim('dropdown-item '
                                    . ($childActive ? 'active ' : '')
                                    . ($child['css_class'] ?? ''));
                            @endphp
                            <li>
                                <a class="{{ $childClass }}"
                                   href="{{ $childUrl ?: '#' }}"
                                   @if($child['target'] ?? null) target="{{ $child['target'] }}" rel="noopener" @endif>
                                    @if(! empty($child['icon']))
                                        <i class="bi {{ $child['icon'] }}"></i>
                                    @endif
                                    {{ $childLabel !== '' ? $childLabel : ($child['identifier'] ?? '') }}
                                </a>
                            </li>
                        @endforeach
                    </ul>
                @endif
            </li>
        @endif
    @endforeach
</ul>
