@php
    use Illuminate\Support\Facades\Auth;
    use Filament\Facades\Filament;

    // Safely get navigation
    try {
        $navigation = Filament::getNavigation();
    } catch (\Throwable $e) {
        $navigation = [];
    }

    $user = Auth::user();
    $userName = $user?->name ?? 'Admin';
    $userInitial = strtoupper(substr($userName, 0, 1));

    // Safe logo URL — fall back gracefully if no image exists
    try {
        $adminLogo = \App\Models\Setting::getValue('admin_logo');
        $logoUrl = $adminLogo
            ? asset('storage/' . $adminLogo)
            : null;
    } catch (\Throwable $e) {
        $logoUrl = null;
    }

    // Safe profile URL
    try {
        $profileUrl = \App\Filament\Pages\Auth\EditProfile::getUrl();
    } catch (\Throwable $e) {
        $profileUrl = '#';
    }
@endphp

<div
    id="custom-admin-sidebar"
    x-data="{
        collapsed: false,
        init() {
            try {
                this.collapsed = localStorage.getItem('sidebar_collapsed') === 'true';
            } catch (e) {
                this.collapsed = false;
            }
            this.$watch('collapsed', val => {
                try {
                    localStorage.setItem('sidebar_collapsed', val);
                } catch (e) {}
            });
            if (this.collapsed) this.$el.classList.add('sidebar-collapsed');
        },
        toggle() {
            this.collapsed = !this.collapsed;
            this.$el.classList.toggle('sidebar-collapsed', this.collapsed);
        }
    }"
>
    {{-- Toggle Button --}}
    <button
        class="sidebar-toggle-btn"
        @click="toggle()"
        title="Toggle Sidebar"
        aria-label="Toggle Sidebar"
    >
        <svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round">
            <path d="M15 18l-6-6 6-6"/>
        </svg>
    </button>

    {{-- Header --}}
    <div class="sidebar-header">
        @if ($logoUrl)
            <img src="{{ $logoUrl }}" alt="Logo" class="sidebar-logo" />
        @else
            {{-- SVG logo fallback --}}
            <div class="sidebar-logo-fallback">
                <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 36 36" fill="none">
                    <rect width="36" height="36" rx="8" fill="#f59e0b"/>
                    <text x="18" y="25" text-anchor="middle" font-size="18" font-weight="bold" fill="white" font-family="sans-serif">S</text>
                </svg>
            </div>
        @endif
        <span class="sidebar-brand-name">Sawdabazar</span>
    </div>

    {{-- Navigation --}}
    <nav class="sidebar-nav">

        {{-- Dashboard --}}
        <a
            href="{{ route('filament.admin.pages.dashboard') }}"
            data-label="Dashboard"
            class="nav-item {{ request()->routeIs('filament.admin.pages.dashboard') ? 'active' : '' }}"
        >
            <svg class="nav-icon" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.8" stroke="currentColor">
                <path stroke-linecap="round" stroke-linejoin="round" d="M3.75 6A2.25 2.25 0 0 1 6 3.75h2.25A2.25 2.25 0 0 1 10.5 6v2.25a2.25 2.25 0 0 1-2.25 2.25H6a2.25 2.25 0 0 1-2.25-2.25V6ZM3.75 15.75A2.25 2.25 0 0 1 6 13.5h2.25a2.25 2.25 0 0 1 2.25 2.25V18a2.25 2.25 0 0 1-2.25 2.25H6A2.25 2.25 0 0 1 3.75 18v-2.25ZM13.5 6a2.25 2.25 0 0 1 2.25-2.25H18A2.25 2.25 0 0 1 20.25 6v2.25A2.25 2.25 0 0 1 18 10.5h-2.25a2.25 2.25 0 0 1-2.25-2.25V6ZM13.5 15.75a2.25 2.25 0 0 1 2.25-2.25H18a2.25 2.25 0 0 1 2.25 2.25V18A2.25 2.25 0 0 1 18 20.25h-2.25A2.25 2.25 0 0 1 13.5 18v-2.25Z"/>
            </svg>
            <span class="nav-label">Dashboard</span>
        </a>

        {{-- Dynamic Navigation Groups from Filament --}}
        @foreach ($navigation as $group)
            @if ($group->getLabel())
                <div class="nav-group-label">{{ $group->getLabel() }}</div>
            @endif

            @foreach ($group->getItems() as $item)
                @php
                    $itemUrl = '#';
                    try { $itemUrl = $item->getUrl(); } catch (\Throwable $e) {}
                    $isActive = false;
                    try { $isActive = $item->isActive(); } catch (\Throwable $e) {}
                    $itemLabel = '';
                    try { $itemLabel = $item->getLabel(); } catch (\Throwable $e) {}
                    if ($itemLabel === 'Dashboard') {
                        continue;
                    }
                    $itemIcon = null;
                    try { $itemIcon = $item->getIcon(); } catch (\Throwable $e) {}
                    $itemBadge = null;
                    try { $itemBadge = $item->getBadge(); } catch (\Throwable $e) {}
                    $newTab = false;
                    try { $newTab = $item->shouldOpenUrlInNewTab(); } catch (\Throwable $e) {}
                @endphp
                <a
                    href="{{ $itemUrl }}"
                    data-label="{{ $itemLabel }}"
                    class="nav-item {{ $isActive ? 'active' : '' }}"
                    @if($newTab) target="_blank" @endif
                >
                    @if ($itemIcon)
                        <x-filament::icon
                            :icon="$itemIcon"
                            class="nav-icon"
                        />
                    @else
                        <svg class="nav-icon" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.8" stroke="currentColor">
                            <path stroke-linecap="round" stroke-linejoin="round" d="M5 12h14"/>
                        </svg>
                    @endif
                    <span class="nav-label">{{ $itemLabel }}</span>

                    @if ($itemBadge)
                        <span class="nav-badge">{{ $itemBadge }}</span>
                    @endif
                </a>
            @endforeach
        @endforeach

    </nav>

    {{-- Footer / User --}}
    <div class="sidebar-footer">
        <a href="{{ $profileUrl }}" class="sidebar-user">
            <div class="sidebar-avatar">{{ $userInitial }}</div>
            <div class="sidebar-user-info">
                <div class="sidebar-user-name">{{ $userName }}</div>
                <div class="sidebar-user-role">Administrator</div>
            </div>
        </a>
    </div>
</div>
