{{--
    Main site header.

    Structure:
      1. Top row: logo + search (desktop) + (phone | login-or-account | favorites | cart)
      2. Mobile-only search row (collapses out under md)
      3. Main navigation (category links + Akcijos on the right)

    Auth state is driven by auth()->check() at render time:
      - Guest → "Prisijungti" button, no account dropdown
      - Logged in → account dropdown with user info and links to /user-*

    Currently all category links and /favor, /cart routes are placeholders
    — they will 404 until the matching modules ship. That's deliberate for
    Sub-etapas 2.1: hardcoded homepage first, wire real routes later.
--}}
<header class="site-header">
    <div class="container py-3">
        <div class="d-flex align-items-center gap-3">

            <a href="/" class="logo flex-shrink-0">
                <span class="logo-mark">P</span>
                <span>PadanguParduotuvė<span class="dim">.lt</span></span>
            </a>

            {{--
                Desktop search (centered, between logo and right-side icons).

                Task-35 (v18.36) — replaced the legacy Core /paieska +
                /paieska/suggest wiring with the HeaderSearch drop-in
                partial. The partial points at /paieska-katalogas (LT) /
                /en/catalog-search (EN) and surfaces Products / Categories
                / Models / Brands. Core's /paieska remains fully functional
                via direct URL (back-button history, indexed pages, menu
                links) but is no longer the default header target.

                Drop-in include preserves analytics + click tracking +
                localStorage recent searches set up by task-19.
            --}}
            <div class="d-none d-lg-block mx-auto" style="flex:1 1 auto;max-width:680px;">
                @include('headersearch::web._header-search-box')
            </div>

            <div class="d-flex align-items-center gap-2 ms-auto">
                {{-- Phone pill — hidden below xl to save space --}}
                <a href="tel:+37061482222" class="phone-pill d-none d-xl-inline-flex">
                    <i class="bi bi-telephone-fill"></i> +370 614 82222
                </a>

                @auth
                    {{-- Logged-in: account dropdown --}}
                    @php
                        $user = auth()->user();
                        $displayName = $user->name ?? 'Vartotojas';
                        $initial = strtoupper(mb_substr($displayName, 0, 1));
                    @endphp
                    <div class="dropdown">
                        <button class="account-trigger" type="button" data-bs-toggle="dropdown" aria-expanded="false">
                            <span class="avatar">{{ $initial }}</span>
                            <span class="d-none d-md-inline">{{ explode(' ', $displayName)[0] }}</span>
                            <i class="bi bi-chevron-down caret"></i>
                        </button>
                        <ul class="dropdown-menu dropdown-menu-end dropdown-menu-account">
                            <li class="acc-head">
                                <div class="name">{{ $user->name }}</div>
                                <div class="email">{{ $user->email }}</div>
                            </li>
                            <li><a class="dropdown-item" href="{{ cms_route('web.account.index') }}"><i class="bi bi-person"></i> Mano paskyra</a></li>
                            <li><a class="dropdown-item" href="/user-orders"><i class="bi bi-box-seam"></i> Mano užsakymai</a></li>
                            <li><a class="dropdown-item" href="/favor"><i class="bi bi-heart"></i> Mėgstamiausi</a></li>
                            <li><a class="dropdown-item" href="/user-addresses"><i class="bi bi-geo-alt"></i> Pristatymo adresai</a></li>
                            <li><hr></li>
                            <li><a class="dropdown-item" href="{{ cms_route('web.account.settings') }}"><i class="bi bi-gear"></i> Nustatymai</a></li>
                            <li>
                                {{-- Logout uses POST form so we honour CSRF. Hidden form triggered
                                     from the dropdown-item click. --}}
                                <form method="POST" action="{{ route(cms_route_name_for_current_locale('web.auth.logout')) }}" id="headerLogoutForm">
                                    @csrf
                                    <a class="dropdown-item logout" href="#" onclick="event.preventDefault(); document.getElementById('headerLogoutForm').submit();">
                                        <i class="bi bi-box-arrow-right"></i> Atsijungti
                                    </a>
                                </form>
                            </li>
                        </ul>
                    </div>
                @else
                    {{--
                        Mobile-only guest auth — compact person icon that opens a Bootstrap
                        dropdown with Login + Register links. Hidden on >=md where the full
                        .btn-login / .btn-register pills fit on the topbar (see below).
                        Frontend/task-1: gives mobile guests a header entry into auth UI
                        instead of having to type the /prisijungti URL by hand.
                    --}}
                    <div class="dropdown d-md-none" data-mobile-auth>
                        <button class="header-icon" type="button"
                                data-bs-toggle="dropdown" aria-expanded="false"
                                aria-label="{{ __('auth::messages.account') }}">
                            <i class="bi bi-person"></i>
                        </button>
                        <ul class="dropdown-menu dropdown-menu-end dropdown-menu-account">
                            <li>
                                <a class="dropdown-item" href="{{ route(cms_route_name_for_current_locale('web.auth.login')) }}">
                                    <i class="bi bi-box-arrow-in-right"></i> {{ __('auth::messages.sign_in') }}
                                </a>
                            </li>
                            <li>
                                <a class="dropdown-item" href="{{ route(cms_route_name_for_current_locale('web.auth.register')) }}">
                                    <i class="bi bi-person-plus"></i> {{ __('auth::messages.register') }}
                                </a>
                            </li>
                        </ul>
                    </div>

                    {{-- Desktop guest: full-width login + register pills (hidden on <md by d-none). --}}
                    <a href="{{ route(cms_route_name_for_current_locale('web.auth.login')) }}" class="btn-login d-none d-md-inline-flex">
                        <i class="bi bi-person"></i>
                        <span>{{ __('auth::messages.sign_in') }}</span>
                    </a>
                    <a href="{{ route(cms_route_name_for_current_locale('web.auth.register')) }}" class="btn-register d-none d-md-inline-flex">
                        <i class="bi bi-person-plus"></i>
                        <span>{{ __('auth::messages.register') }}</span>
                    </a>
                @endauth

                <a href="/favor" class="header-icon" aria-label="Mėgstamiausi"><i class="bi bi-heart"></i></a>
                <a href="/cart" class="header-icon" aria-label="Krepšelis">
                    <i class="bi bi-bag"></i><span class="badge-dot">0</span>
                </a>
            </div>
        </div>

        {{--
            Mobile search row — drop-in @include matches the desktop form's
            contract. Task-35 swap. See desktop block above for the
            migration note; the partial is shared.
        --}}
        <div class="d-lg-none mt-3">
            @include('headersearch::web._header-search-box')
        </div>
    </div>

    <nav class="main-nav">
        <div class="container">
            <button class="btn border-0 px-0 py-2 d-lg-none" type="button"
                    data-bs-toggle="collapse" data-bs-target="#mainNav">
                <i class="bi bi-list fs-3"></i> <span class="fw-semibold ms-1">Kategorijos</span>
            </button>
            <div class="collapse d-lg-block" id="mainNav">
                @if(config('menus.use_db_navigation'))
                    {!! menu('web.main') !!}
                @else
                    {{--
                        LEGACY hardcoded category nav — fallback when MENUS_DB_DRIVEN=false.
                        Will be removed in v17.0 once DB-driven navigation has been
                        validated in prod.

                        Pipeline/task-1 fix (depth 2): the legacy fallback now carries
                        the same `data-menu="web.main"` test hook as the DB-driven
                        component (menu-bootstrap-navbar.blade.php) AND a top-level
                        "Naujienos" link. The smoke sentinel
                        ("storefront header surfaces Naujienos top-level nav item")
                        scopes its assertion to `[data-menu="web.main"]`, which only
                        the DB-driven path used to emit — so with MENUS_DB_DRIVEN=false
                        the locator never matched and the legacy nav was also missing
                        Naujienos entirely. Surfacing News here keeps the storefront
                        contract identical in both toggle modes (News is a live module).
                        `/naujienos` is the seeded root NewsCategory listing (catchall
                        priority 450).
                    --}}
                    <ul class="nav" data-menu="web.main">
                        <li class="nav-item"><a class="nav-link {{ request()->is('lengvuju-automobiliu-padangos') ? 'active' : '' }}" href="/lengvuju-automobiliu-padangos">Lengvųjų automobilių padangos</a></li>
                        <li class="nav-item"><a class="nav-link {{ request()->is('sunkveziminiu-padangos') ? 'active' : '' }}" href="/sunkveziminiu-padangos">Sunkvežimių padangos</a></li>
                        <li class="nav-item"><a class="nav-link {{ request()->is('motociklu-padangos') ? 'active' : '' }}" href="/motociklu-padangos">Motociklų padangos</a></li>
                        <li class="nav-item"><a class="nav-link {{ request()->is('lieti-ratlankiai') ? 'active' : '' }}" href="/lieti-ratlankiai">Ratlankiai</a></li>
                        <li class="nav-item"><a class="nav-link {{ request()->is('akumuliatoriai') ? 'active' : '' }}" href="/akumuliatoriai">Akumuliatoriai</a></li>
                        <li class="nav-item"><a class="nav-link {{ request()->is('aksesuarai') ? 'active' : '' }}" href="/aksesuarai">Aksesuarai</a></li>
                        <li class="nav-item"><a class="nav-link {{ request()->is('naujienos*') ? 'active' : '' }}" href="/naujienos">Naujienos</a></li>
                        <li class="nav-item ms-lg-auto"><a class="nav-link text-danger {{ request()->is('akcijos') ? 'active' : '' }}" href="/akcijos"><i class="bi bi-fire"></i> Akcijos</a></li>
                    </ul>
                @endif
            </div>
        </div>
    </nav>
</header>
