<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>@yield('title', 'Admin') — {{ platform_name() }}</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Fraunces:opsz,wght@9..144,500;9..144,600;9..144,700&family=Source+Sans+3:wght@400;500;600;700&display=swap" rel="stylesheet">
    <style>body { margin: 0; font-family: "Source Sans 3", system-ui, sans-serif; }</style>
    <link rel="icon" href="{{ platform_logo('favicon') }}" type="image/svg+xml">
    <link rel="stylesheet" href="{{ asset('css/fundi.css') }}">
    @stack('head')
</head>
<body>
@php
    $pendingAuthCount = \App\Models\AuthorizationQueue::where('status', 'pending')->count();
    $user = auth()->user();
    $currentRoute = request()->route()?->getName() ?? '';
    $navItems = \App\Models\NavigationMenu::forLocation('admin');
    $badges = ['pending_auth' => $pendingAuthCount];
@endphp
<div class="fundi-admin">
    <aside class="fundi-sidebar">
        <div class="fundi-sidebar__brand">
            <x-brand-logo variant="light" size="sm" :tagline="true" />
        </div>
        <ul class="fundi-nav">
            @foreach($navItems as $item)
                <li class="fundi-nav__item">
                    <a href="{{ $item->href() }}"
                       class="{{ $item->isActive($currentRoute) ? 'active' : '' }}"
                       @if($item->open_in_new_tab) target="_blank" rel="noopener" @endif>
                        {{ $item->label }}
                        @if($item->badge_key && !empty($badges[$item->badge_key]))
                            <span class="fundi-nav__badge">{{ $badges[$item->badge_key] }}</span>
                        @endif
                    </a>
                </li>
            @endforeach
        </ul>
    </aside>

    <div class="fundi-main">
        <header class="fundi-topbar">
            <div class="fundi-topbar__title">@yield('title', 'Dashboard')</div>
            <div class="fundi-topbar__user">
                <div>
                    <div class="fundi-topbar__name">{{ $user->name ?? 'Staff' }}</div>
                    <div class="fundi-topbar__role">{{ $user->role->name ?? $user->designation ?? 'Staff' }}</div>
                </div>
                <form method="POST" action="{{ route('logout') }}">
                    @csrf
                    <button type="submit" class="fundi-btn fundi-btn--secondary fundi-btn--sm">Logout</button>
                </form>
            </div>
        </header>

        <main class="fundi-content">
            @if(session('success'))
                <div class="fundi-alert fundi-alert--success">{{ session('success') }}</div>
            @endif
            @if($errors->any())
                <div class="fundi-alert fundi-alert--error">
                    <strong>Please correct the following:</strong>
                    <ul>
                        @foreach($errors->all() as $error)
                            <li>{{ $error }}</li>
                        @endforeach
                    </ul>
                </div>
            @endif

            @yield('content')
        </main>
    </div>
</div>
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.7/dist/chart.umd.min.js"></script>
@stack('scripts')
</body>
</html>
