{{-- Ported from the design's sidebar.php. Chats and user are ours. --}}
@php
    $conversationRoute = $conversationRoute ?? (request()->routeIs('googlechat.*') ? 'googlechat.show' : 'chat.show');
    $conversations = collect($conversations ?? [])->take(5);
    $recentCreatives = collect($recentCreatives ?? [])->take(5);
    $recentPromptPages = collect($recentPromptPages ?? [])->take(5);

    $isCreative = request()->routeIs('creatives.*');
    $isLanding = request()->routeIs('landing-pages.*') || request()->routeIs('catalog-landing-pages.*') || request()->routeIs('prompt-landing-pages.*');
    $isChat = request()->routeIs('chat.*') || request()->routeIs('chats.*') || request()->routeIs('googlechat.*');
    $isPerformance = request()->routeIs('campaigns.*');

    // Expand only one section at a time by default based on current context
    $activeSection = $isCreative ? 'creativeNav' : ($isLanding ? 'landingPageNav' : 'chatNav');
@endphp
<aside class="mb-sidebar">
    <div class="mb-side-top">
        <a href="{{ route('home') }}" class="mb-logo" aria-label="{{ config('app.name') }} home">
            @include('partials.mb.logo')
        </a>
        <button class="mb-icon-btn" data-action="toggle-sidebar" aria-label="Toggle sidebar"><i class="bi bi-layout-sidebar"></i></button>
    </div>

    <div class="mb-side-scroll">
        <div class="mb-nav-row{{ request()->routeIs('home') || request()->routeIs('dashboard') ? ' active' : '' }}">
            <a href="{{ route('home') }}" class="mb-nav{{ request()->routeIs('home') || request()->routeIs('dashboard') ? ' active' : '' }}" title="Home" wire:navigate>
                <i class="bi bi-house-door"></i><span class="mb-label">Home</span>
            </a>
        </div>

        <div class="mb-rail-search">
            <a href="{{ route(request()->routeIs('googlechat.*') ? 'googlechat.new' : 'chat.new') }}" class="mb-nav" title="New chat">
                <i class="bi bi-plus-lg"></i><span class="mb-label">New chat</span>
            </a>
        </div>

        <div class="mb-nav-row{{ $isChat ? ' active' : '' }}">
            <a href="{{ route('chats.index') }}" class="mb-nav{{ $isChat ? ' active' : '' }}" title="Chats" wire:navigate>
                <i class="bi bi-chat-dots"></i><span class="mb-label">Chats</span>
            </a>
            <button type="button" class="mb-nav-toggle" data-action="toggle-nav" data-target="chatNav" aria-expanded="{{ $activeSection === 'chatNav' ? 'true' : 'false' }}" aria-controls="chatNav" aria-label="Toggle chats">
                <i class="bi bi-chevron-down"></i>
            </button>
        </div>
        <div class="mb-chats">
            <div class="mb-chats-panel{{ $activeSection === 'chatNav' ? '' : ' is-closed' }}" id="chatNav">
                <div class="mb-side-head">
                    <span>Recent chats</span>
                    {{-- Starting a new chat lived only in the collapsed rail:
                         .mb-rail-search is display:none unless .is-collapsed, so
                         with the sidebar open, which is how it opens, there was
                         no way to start one at all. --}}
                    <a href="{{ route(request()->routeIs('googlechat.*') ? 'googlechat.new' : 'chat.new') }}"
                       class="mb-icon-btn mb-icon-sm" title="New chat" aria-label="New chat"><i class="bi bi-plus-lg"></i></a>
                    <button type="button" class="mb-icon-btn mb-icon-sm" data-action="open-chat-search" aria-label="Search chats" aria-expanded="false" aria-controls="chatSearch"><i class="bi bi-search"></i></button>
                    <a href="{{ route('chats.index') }}" class="mb-side-head-link" wire:navigate>All</a>
                </div>
                <form class="mb-chat-search" id="chatSearch" method="GET" action="{{ route('chats.index') }}" @if (! request()->routeIs('chats.index') || ! request()->filled('q')) hidden @endif>
                    <label class="mb-search">
                        <i class="bi bi-search"></i>
                        <input type="search" name="q" id="chatSearchInput" value="{{ request()->routeIs('chats.index') ? request('q') : '' }}" placeholder="Search chats…" aria-label="Search chats">
                    </label>
                </form>
                <div class="mb-chat-list" id="chatList">
                    @foreach ($conversations as $conversation)
                        @php
                            $conversationKey = $conversation->id;
                            $conversationTitle = $conversation->title ?: 'Untitled';
                        @endphp
                        <a class="mb-chat-row{{ ($conversationId ?? null) === $conversationKey ? ' active' : '' }}"
                           href="{{ route($conversationRoute, ['conversation' => $conversationKey]) }}"
                           data-id="{{ $conversationKey }}"
                           data-title="{{ $conversationTitle }}"
                           title="{{ $conversationTitle }}">
                            <span class="mb-hist-dot"></span>
                            <span class="mb-chat-name">{{ $conversationTitle }}</span>
                            @if ($conversation->updated_at ?? $conversation->created_at)
                                <span class="mb-hist-time">{{ ($conversation->updated_at ?? $conversation->created_at)->diffForHumans(null, true) }}</span>
                            @endif
                        </a>
                    @endforeach
                </div>
                <div class="mb-chat-hint" id="chatEmpty" @if ($conversations->isNotEmpty()) hidden @endif>No chats yet.</div>
                <div class="mb-chat-hint" id="chatSearchEmpty" hidden>No matching chats. Press Enter to search the listing.</div>
            </div>
        </div>

        <div class="mb-nav-row{{ $isPerformance ? ' active' : '' }}">
            <a href="{{ route('campaigns.performance') }}" class="mb-nav{{ $isPerformance ? ' active' : '' }}" title="Campaign performance" wire:navigate>
                <i class="bi bi-graph-up-arrow"></i><span class="mb-label">Campaign performance</span>
            </a>
        </div>

        {{-- AdStudio: ported from the design's sidebar.php nav-row + creative-history.php panel. --}}
        <div class="mb-nav-row{{ $isCreative ? ' active' : '' }}">
            <a href="{{ route('creatives.index') }}" class="mb-nav{{ $isCreative ? ' active' : '' }}" title="AdStudio" wire:navigate>
                <i class="bi bi-palette"></i><span class="mb-label">AdStudio</span>
            </a>
            <button type="button" class="mb-nav-toggle" data-action="toggle-nav" data-target="creativeNav" aria-expanded="{{ $activeSection === 'creativeNav' ? 'true' : 'false' }}" aria-controls="creativeNav" aria-label="Toggle AdStudio">
                <i class="bi bi-chevron-down"></i>
            </button>
        </div>
        <div class="mb-nav-panel{{ $activeSection === 'creativeNav' ? '' : ' is-closed' }}" id="creativeNav">
            <div class="mb-side-head">
                <span>Recent creatives</span>
                <a href="{{ route('creatives.index') }}" class="mb-side-head-link" wire:navigate>All</a>
            </div>
            @if ($recentCreatives->isEmpty())
                <div class="mb-nav-empty">No creatives yet</div>
            @else
                @foreach ($recentCreatives as $item)
                    @php
                        $itemName = $item->title ?? $item->prompt ?? $item->product_url ?? ('Creative #'.$item->id);
                        // The design's dot only knows completed/generating/queued/failed; our
                        // "processing" (queued job actively rendering) reads as "generating" too.
                        $itemDot = $item->status === 'processing' ? 'generating' : $item->status;
                    @endphp
                    <a class="mb-hist{{ request()->routeIs('creatives.show') && request()->route('creative')?->id === $item->id ? ' active' : '' }}"
                       href="{{ route('creatives.show', $item) }}" wire:navigate
                       title="{{ $itemName }} - {{ $item->status }}">
                        <span class="mb-hist-dot is-{{ $itemDot }}"></span>
                        <span class="mb-hist-name">{{ $itemName }}</span>
                        <span class="mb-hist-time">{{ $item->created_at->diffForHumans(null, true) }}</span>
                    </a>
                @endforeach
            @endif
        </div>

        <div class="mb-nav-row{{ $isLanding ? ' active' : '' }}">
            <a href="{{ route('landing-pages.index', ['tab' => request('tab', 'catalog')]) }}" class="mb-nav{{ $isLanding ? ' active' : '' }}" title="Landing Pages">
                <i class="bi bi-layout-text-window-reverse"></i><span class="mb-label">Landing Pages</span>
            </a>
            <button type="button" class="mb-nav-toggle" data-action="toggle-nav" data-target="landingPageNav" aria-expanded="{{ $activeSection === 'landingPageNav' ? 'true' : 'false' }}" aria-controls="landingPageNav" aria-label="Toggle Landing Pages">
                <i class="bi bi-chevron-down"></i>
            </button>
        </div>
        <div class="mb-nav-panel{{ $activeSection === 'landingPageNav' ? '' : ' is-closed' }}" id="landingPageNav">
            <div class="mb-side-head">
                <span>Recent landing pages</span>
                <a href="{{ route('landing-pages.index', ['tab' => request('tab', 'catalog')]) }}" class="mb-side-head-link">All</a>
            </div>
            @if ($recentPromptPages->isEmpty())
                <div class="mb-nav-empty">No landing pages yet</div>
            @else
                @foreach ($recentPromptPages as $item)
                    @php
                        $itemDot = match ($item->status) {
                            'searching', 'analysing', 'generating' => 'generating',
                            'pending_approval' => 'queued',
                            'approved' => 'completed',
                            'rejected' => 'failed',
                            default => $item->status,
                        };
                    @endphp
                    <a class="mb-hist{{ request()->routeIs('prompt-landing-pages.show') && (int) request()->route('page')?->id === $item->id ? ' active' : '' }}"
                       href="{{ route('prompt-landing-pages.show', $item) }}"
                       title="{{ $item->displayTitle() }} - {{ $item->status }}">
                        <span class="mb-hist-dot is-{{ $itemDot }}"></span>
                        <span class="mb-hist-name">{{ $item->displayTitle() }}</span>
                        <span class="mb-hist-time">{{ $item->created_at->diffForHumans(null, true) }}</span>
                    </a>
                @endforeach
            @endif
        </div>
    </div>

    <div class="mb-side-bottom">
        <div class="mb-user" data-pop="user" role="button" tabindex="0" aria-haspopup="menu">
            <span class="mb-avatar">{{ strtoupper(substr(auth()->user()?->name ?? '?', 0, 1)) }}</span>
            <span class="mb-user-text">
                <div class="mb-user-name">{{ auth()->user()?->name }}</div>
                <div class="mb-user-plan">{{ auth()->user()?->email }}</div>
            </span>
            <span class="mb-icon-btn mb-user-gear ms-auto" aria-hidden="true"><i class="bi bi-gear"></i></span>
        </div>
    </div>

    @include('partials.mb.user-menu')
</aside>
