<?php
declare(strict_types=1);

function render_header(string $title, array $options = []): void
{
    $active = $options['active'] ?? '';
    $hideNav = $options['hide_nav'] ?? false;
    $extraCss = $options['extra_css'] ?? [];
    $appName = config('app.name', 'QR Attendance');
    $user = current_user();
    $flashes = get_flashes();

    $GLOBALS['__layout_hide_nav'] = $hideNav;

    header('Content-Type: text/html; charset=utf-8');
    ?>
    <!doctype html>
    <html lang="en" data-bs-theme="light">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title><?= sanitize($title) ?> | <?= sanitize($appName) ?></title>
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css">
        <link rel="stylesheet" href="<?= asset_url('css/style.css') ?>">
        <?php foreach ($extraCss as $css): ?>
            <link rel="stylesheet" href="<?= sanitize($css) ?>">
        <?php endforeach; ?>
    </head>
    <body>
    <?php if ($hideNav): ?>
        <main class="auth-shell container py-5">
    <?php else: ?>
        <div class="app-shell">
            <aside class="sidebar bg-white border-end">
                <div class="sidebar-brand mb-4">
                    <div class="sidebar-icon bg-primary-subtle text-primary">QR</div>
                    <div>
                        <div class="fw-semibold text-primary">QR Code Attendance</div>
                        <small class="text-muted">Admin Console</small>
                    </div>
                </div>
                <nav class="sidebar-nav flex-grow-1">
                    <?php foreach (nav_items() as $item): ?>
                        <?php if (!$item['show']): continue; endif; ?>
                        <a class="sidebar-link <?= $active === $item['key'] ? 'active' : '' ?>"
                           href="<?= url_for($item['path']) ?>">
                            <?= sanitize($item['label']) ?>
                        </a>
                    <?php endforeach; ?>
                </nav>
                <?php if ($user): ?>
                    <div class="sidebar-user mt-4">
                        <div>
                            <div class="fw-semibold"><?= sanitize($user['full_name']) ?></div>
                            <small class="text-muted text-uppercase"><?= sanitize($user['role']) ?></small>
                        </div>
                        <a href="<?= url_for('auth/logout.php') ?>" class="btn btn-sm btn-outline-primary ms-auto">
                            Logout
                        </a>
                    </div>
                <?php endif; ?>
            </aside>
            <div class="content-area flex-grow-1">
                <header class="content-topbar bg-white border-bottom d-flex justify-content-between align-items-center px-4 py-3">
                    <div class="d-flex align-items-center gap-3">
                        <button class="btn btn-outline-secondary btn-sm sidebar-toggle" type="button" data-sidebar-toggle>
                            <span class="sidebar-toggle-icon">&#9776;</span>
                        </button>
                        <div class="text-muted small">QR Attendance System</div>
                        <h1 class="h5 mb-0 mb-0"><?= sanitize($title) ?></h1>
                    </div>
                    <span class="badge text-bg-light"><?= date('D, M j') ?></span>
                </header>
                <main class="content-body px-4 py-4">
    <?php endif; ?>
        <?php foreach ($flashes as $type => $messages): ?>
            <?php foreach ($messages as $message): ?>
                <div class="alert alert-<?= sanitize($type) ?> alert-dismissible fade show" role="alert">
                    <?= sanitize($message) ?>
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>
            <?php endforeach; ?>
        <?php endforeach; ?>
    <?php
}

function render_footer(array $options = []): void
{
    $extraJs = $options['extra_js'] ?? [];
    $hideNav = $GLOBALS['__layout_hide_nav'] ?? false;
    ?>
    <?php if ($hideNav): ?>
        </main>
    <?php else: ?>
                </main>
            </div>
        </div>
    <?php endif; ?>
    <?php foreach ($extraJs as $script): ?>
        <script src="<?= sanitize($script) ?>"></script>
    <?php endforeach; ?>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
    <script src="<?= asset_url('js/admin.js') ?>" defer></script>
    <script src="<?= asset_url('js/scan.js') ?>" defer></script>
    </body>
    </html>
    <?php
}

function nav_items(): array
{
    $user = current_user();
    $isAdmin = $user && $user['role'] === 'admin';

    return [
        [
            'key' => 'dashboard',
            'label' => 'Dashboard',
            'path' => 'admin/dashboard.php',
            'show' => (bool) $user,
        ],
        [
            'key' => 'students',
            'label' => 'Students',
            'path' => 'admin/students.php',
            'show' => (bool) $user,
        ],
        [
            'key' => 'activities',
            'label' => 'Activities',
            'path' => 'admin/activities.php',
            'show' => (bool) $user,
        ],
        [
            'key' => 'courses',
            'label' => 'Courses',
            'path' => 'admin/courses.php',
            'show' => (bool) $user,
        ],
        [
            'key' => 'departments',
            'label' => 'Departments',
            'path' => 'admin/departments.php',
            'show' => (bool) $user,
        ],
        [
            'key' => 'attendance',
            'label' => 'Scan Center',
            'path' => 'admin/attendance.php',
            'show' => $isAdmin,
        ],
        [
            'key' => 'reports',
            'label' => 'Reports',
            'path' => 'admin/reports.php',
            'show' => (bool) $user,
        ],
        [
            'key' => 'settings',
            'label' => 'Settings',
            'path' => 'admin/settings.php',
            'show' => $isAdmin,
        ],
        [
            'key' => 'users',
            'label' => 'Users',
            'path' => 'admin/users.php',
            'show' => $isAdmin,
        ],
    ];
}
