<?php
/**
 * Primary application layout for authenticated areas (dashboard, admin, auth).
 *
 * Child views set `$this->layout('layouts.app')`, define a `title` and their
 * content is injected via `$this->yield('content')`. Shared services (auth,
 * csrf, session, flash) are provided by the base Controller.
 *
 * @var \BotGuard\Core\View $this
 * @var \BotGuard\Core\Auth|null $auth
 */
$appName = e(config('app.name', 'BotGuard'));
$title = isset($title) ? e($title) . ' · ' . $appName : $appName;
$isAuth = isset($auth) && $auth->check();
$isAdmin = $isAuth && $auth->isAdmin();
$flashSuccess = isset($session) ? $session->getFlash('success') : null;
$flashError = isset($session) ? $session->getFlash('error') : null;
?>
<!DOCTYPE html>
<html lang="en" data-bs-theme="dark">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="csrf-token" content="<?= isset($csrf) ? e($csrf->token()) : '' ?>">
    <title><?= $title ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css" rel="stylesheet">
    <link href="/assets/css/app.css" rel="stylesheet">
</head>
<body class="bg-body">
<nav class="navbar navbar-expand-lg border-bottom sticky-top bg-body-tertiary">
    <div class="container-fluid px-lg-4">
        <a class="navbar-brand fw-bold d-flex align-items-center gap-2" href="/">
            <i class="bi bi-shield-check text-primary"></i> <?= $appName ?>
        </a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#nav">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="nav">
            <ul class="navbar-nav me-auto">
                <?php if ($isAuth): ?>
                    <li class="nav-item"><a class="nav-link" href="/dashboard"><i class="bi bi-speedometer2"></i> Dashboard</a></li>
                    <li class="nav-item"><a class="nav-link" href="/dashboard/analytics"><i class="bi bi-graph-up"></i> Analytics</a></li>
                    <li class="nav-item"><a class="nav-link" href="/dashboard/logs"><i class="bi bi-list-check"></i> Logs</a></li>
                    <li class="nav-item"><a class="nav-link" href="/dashboard/redirects"><i class="bi bi-signpost-split"></i> Redirects</a></li>
                    <li class="nav-item"><a class="nav-link" href="/dashboard/api-keys"><i class="bi bi-key"></i> API Keys</a></li>
                    <?php if ($isAdmin): ?>
                        <li class="nav-item"><a class="nav-link text-warning" href="/admin"><i class="bi bi-gear"></i> Admin</a></li>
                    <?php endif; ?>
                <?php endif; ?>
            </ul>
            <div class="d-flex align-items-center gap-2">
                <button id="themeToggle" class="btn btn-outline-secondary btn-sm" type="button" title="Toggle theme">
                    <i class="bi bi-moon-stars"></i>
                </button>
                <?php if ($isAuth): ?>
                    <form method="post" action="/logout" class="m-0">
                        <?= isset($csrf) ? $csrf->field() : '' ?>
                        <button class="btn btn-outline-danger btn-sm" type="submit"><i class="bi bi-box-arrow-right"></i> Logout</button>
                    </form>
                <?php else: ?>
                    <a class="btn btn-outline-primary btn-sm" href="/login">Sign in</a>
                <?php endif; ?>
            </div>
        </div>
    </div>
</nav>

<main class="container-fluid px-lg-4 py-4">
    <?php if ($flashSuccess): ?>
        <div class="alert alert-success alert-dismissible fade show" role="alert">
            <i class="bi bi-check-circle"></i> <?= e($flashSuccess) ?>
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    <?php endif; ?>
    <?php if ($flashError): ?>
        <div class="alert alert-danger alert-dismissible fade show" role="alert">
            <i class="bi bi-exclamation-triangle"></i> <?= e($flashError) ?>
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    <?php endif; ?>

    <?= $this->yield('content') ?>
</main>

<footer class="text-center text-secondary small py-4 border-top">
    <?= $appName ?> · Human Verification &amp; Bot Detection Platform
</footer>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.1/dist/chart.umd.min.js"></script>
<script src="/assets/js/theme.js"></script>
<?= $this->yield('scripts') ?>
</body>
</html>
