<?php
/**
 * Shared layout helpers for the admin panel pages.
 */

declare(strict_types=1);

require_once __DIR__ . '/bootstrap.php';

function admin_page_start(string $title, string $activeTab = 'dashboard', array $options = []): void
{
    $navItems = [
        'dashboard' => ['label' => 'Dashboard', 'url' => '/admin.php', 'icon' => '&#128202;'],
        'users'     => ['label' => 'Users', 'url' => '/admin_users.php', 'icon' => '&#128101;'],
        'content'   => ['label' => 'Content', 'url' => '/admin_content_management.php', 'icon' => '&#127916;'],
        'tokens'    => ['label' => 'Tokens', 'url' => '/admin_token_dashboard.php', 'icon' => '&#128176;'],
        'settings'  => ['label' => 'Settings', 'url' => '/admin_settings.php', 'icon' => '&#9881;'],
    ];

    $skipTokenCheck = $options['skip_token_setup_check'] ?? false;
    if (!$skipTokenCheck && !admin_is_token_setup_complete()) {
        $current = basename(parse_url($_SERVER['REQUEST_URI'] ?? '', PHP_URL_PATH));
        if ($current !== 'admin_token_setup.php') {
            header('Location: /admin_token_setup.php');
            exit;
        }
    }
    ?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?= admin_escape($title) ?> · EasyStream Admin</title>
    <style>
        :root {
            --gradient: linear-gradient(135deg, #6366f1, #8b5cf6);
            --muted: #f4f5f7;
            --card: #ffffff;
            --border: rgba(99, 102, 241, 0.18);
            --text: #1f2937;
            --text-muted: #6b7280;
            --radius-lg: 18px;
            --radius-md: 12px;
            --radius-sm: 8px;
        }

        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }

        body {
            font-family: "Inter", "Segoe UI", system-ui, -apple-system, sans-serif;
            background: var(--muted);
            color: var(--text);
            line-height: 1.5;
        }

        a {
            color: inherit;
            text-decoration: none;
        }

        header {
            background: var(--gradient);
            color: #ffffff;
            padding: 24px 0 16px;
            position: sticky;
            top: 0;
            z-index: 100;
            box-shadow: 0 8px 24px rgba(99, 102, 241, 0.25);
        }

        .admin-header {
            max-width: 1280px;
            margin: 0 auto;
            padding: 0 24px;
            display: flex;
            flex-direction: column;
            gap: 18px;
        }

        .admin-header__top {
            display: flex;
            align-items: center;
            justify-content: space-between;
            flex-wrap: wrap;
            gap: 12px;
        }

        .admin-header__title {
            font-size: 1.8rem;
            font-weight: 700;
            letter-spacing: -0.02em;
        }

        .admin-header__actions {
            display: flex;
            gap: 10px;
            flex-wrap: wrap;
        }

        .admin-button {
            display: inline-flex;
            align-items: center;
            gap: 8px;
            padding: 10px 16px;
            border-radius: var(--radius-sm);
            border: 1px solid rgba(255, 255, 255, 0.25);
            background: rgba(255, 255, 255, 0.15);
            color: #ffffff;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.2s ease;
        }

        .admin-button:hover {
            background: rgba(255, 255, 255, 0.25);
        }

        .admin-button--primary {
            background: #22c55e;
            border-color: #22c55e;
        }

        .admin-button--primary:hover {
            background: #16a34a;
            border-color: #16a34a;
        }

        .admin-button--ghost {
            background: transparent;
            color: #4338ca;
            border-color: rgba(99, 102, 241, 0.35);
        }

        .admin-button--ghost:hover {
            background: rgba(99, 102, 241, 0.12);
        }

        nav ul {
            display: flex;
            gap: 12px;
            list-style: none;
            flex-wrap: wrap;
        }

        nav a {
            display: inline-flex;
            align-items: center;
            gap: 8px;
            padding: 10px 18px;
            border-radius: var(--radius-sm);
            font-weight: 600;
            font-size: 0.95rem;
            color: rgba(255, 255, 255, 0.85);
            transition: background 0.2s ease, color 0.2s ease;
        }

        nav a:hover {
            background: rgba(255, 255, 255, 0.2);
            color: #ffffff;
        }

        nav a.active {
            background: rgba(255, 255, 255, 0.95);
            color: #4338ca;
        }

        main {
            max-width: 1280px;
            margin: 32px auto;
            padding: 0 24px 64px;
        }

        .grid {
            display: grid;
            gap: 20px;
        }

        .grid--two {
            grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
        }

        .stats-grid {
            display: grid;
            gap: 20px;
            grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
            margin-bottom: 28px;
        }

        .stat-card {
            background: var(--card);
            border-radius: var(--radius-lg);
            padding: 24px;
            border: 1px solid var(--border);
            box-shadow: 0 12px 28px rgba(99, 102, 241, 0.08);
            display: flex;
            flex-direction: column;
            gap: 12px;
        }

        .stat-card__icon {
            font-size: 1.8rem;
        }

        .stat-card__label {
            font-weight: 600;
            color: var(--text-muted);
            text-transform: uppercase;
            letter-spacing: 0.08em;
            font-size: 0.75rem;
        }

        .stat-card__value {
            font-size: 2.2rem;
            font-weight: 700;
            letter-spacing: -0.04em;
        }

        .stat-card__meta {
            display: flex;
            gap: 18px;
            flex-wrap: wrap;
            color: var(--text-muted);
            font-size: 0.9rem;
        }

        .card {
            background: var(--card);
            border-radius: var(--radius-lg);
            padding: 24px;
            border: 1px solid var(--border);
            box-shadow: 0 10px 30px rgba(15, 23, 42, 0.06);
        }

        .card__header {
            display: flex;
            align-items: center;
            justify-content: space-between;
            margin-bottom: 18px;
        }

        .card__header-actions {
            display: flex;
            align-items: center;
            gap: 10px;
        }

        table {
            width: 100%;
            border-collapse: collapse;
        }

        th, td {
            padding: 10px 12px;
            text-align: left;
            border-bottom: 1px solid #e5e7eb;
            font-size: 0.92rem;
        }

        th {
            font-weight: 600;
            color: var(--text-muted);
            background: #f9fafb;
        }

        tbody tr:hover {
            background: #f9fafb;
        }

        .badge {
            display: inline-flex;
            align-items: center;
            padding: 4px 10px;
            border-radius: 999px;
            font-size: 0.78rem;
            font-weight: 600;
        }

        .badge--success {
            background: rgba(34, 197, 94, 0.12);
            color: #15803d;
        }

        .badge--warning {
            background: rgba(234, 179, 8, 0.15);
            color: #b45309;
        }

        .badge--danger {
            background: rgba(239, 68, 68, 0.12);
            color: #b91c1c;
        }

        .empty-state {
            text-align: center;
            color: var(--text-muted);
            padding: 24px 12px;
            border: 2px dashed #e5e7eb;
            border-radius: var(--radius-md);
        }

        .timeline {
            display: flex;
            flex-direction: column;
            gap: 18px;
        }

        .timeline__item {
            display: flex;
            gap: 14px;
        }

        .timeline__icon {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background: rgba(99, 102, 241, 0.08);
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 1.1rem;
            color: #4338ca;
        }

        .timeline__title {
            font-weight: 600;
            margin-bottom: 4px;
        }

        .timeline__meta {
            color: var(--text-muted);
            font-size: 0.82rem;
            margin-bottom: 4px;
        }

        .timeline__details {
            font-size: 0.85rem;
            color: var(--text);
            opacity: 0.85;
        }

        .health-grid {
            display: grid;
            gap: 12px;
        }

        .health-card {
            display: flex;
            gap: 12px;
            align-items: center;
            border-radius: var(--radius-md);
            padding: 14px 16px;
            border: 1px solid transparent;
        }

        .health-card__icon {
            font-size: 1.4rem;
        }

        .health-card__title {
            font-weight: 600;
        }

        .health-card__details {
            font-size: 0.88rem;
            color: var(--text-muted);
        }

        .health-card--success {
            background: rgba(34, 197, 94, 0.12);
            border-color: rgba(34, 197, 94, 0.35);
        }

        .health-card--warning {
            background: rgba(234, 179, 8, 0.12);
            border-color: rgba(234, 179, 8, 0.35);
        }

        .health-card--danger {
            background: rgba(239, 68, 68, 0.12);
            border-color: rgba(239, 68, 68, 0.35);
        }

        .health-card--muted {
            background: rgba(148, 163, 184, 0.12);
            border-color: rgba(148, 163, 184, 0.35);
        }

        .quick-actions {
            display: grid;
            gap: 16px;
            grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
        }

        .quick-actions__item {
            display: flex;
            align-items: flex-start;
            gap: 12px;
            padding: 16px;
            border-radius: var(--radius-md);
            border: 1px solid var(--border);
            background: rgba(255, 255, 255, 0.92);
            transition: transform 0.2s ease, box-shadow 0.2s ease;
        }

        .quick-actions__item:hover {
            transform: translateY(-3px);
            box-shadow: 0 12px 20px rgba(79, 70, 229, 0.15);
        }

        .quick-actions__icon {
            font-size: 1.6rem;
        }

        .quick-actions__title {
            font-weight: 600;
            margin-bottom: 4px;
        }

        .quick-actions__desc {
            font-size: 0.85rem;
            color: var(--text-muted);
        }

        .spinner {
            width: 18px;
            height: 18px;
            border-radius: 50%;
            border: 3px solid rgba(255, 255, 255, 0.3);
            border-top-color: #4338ca;
            animation: spin 0.8s linear infinite;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        @media (max-width: 768px) {
            .admin-header__top {
                flex-direction: column;
                align-items: flex-start;
            }

            nav ul {
                flex-wrap: wrap;
            }

            .stat-card__value {
                font-size: 1.8rem;
            }
        }
    </style>
</head>
<body>
    <header>
        <div class="admin-header">
            <div class="admin-header__top">
                <div>
                    <div class="admin-header__title"><?= admin_escape($title) ?></div>
                    <div style="opacity: 0.8; margin-top: 4px;">
                        Signed in as <?= admin_escape($_SESSION['ADMIN_NAME'] ?? 'Administrator') ?>
                    </div>
                </div>
                <div class="admin-header__actions">
                    <a class="admin-button" href="/" target="_blank" rel="noopener">View Site</a>
                    <a class="admin-button" href="/f_modules/m_backend/main.php" target="_blank" rel="noopener">Legacy Admin</a>
                    <a class="admin-button admin-button--primary" href="/login.php?logout=1">Logout</a>
                </div>
            </div>
            <nav>
                <ul>
                    <?php foreach ($navItems as $key => $item): ?>
                        <li>
                            <a href="<?= admin_escape($item['url']) ?>"
                               class="<?= $key === $activeTab ? 'active' : '' ?>">
                                <span><?= $item['icon'] ?></span>
                                <span><?= admin_escape($item['label']) ?></span>
                            </a>
                        </li>
                    <?php endforeach; ?>
                </ul>
            </nav>
        </div>
    </header>
    <main>
<?php
}

function admin_page_end(string $extraScripts = ''): void
{
    ?>
    </main>
    <?php if ($extraScripts !== ''): ?>
    <script><?= $extraScripts ?></script>
    <?php endif; ?>
</body>
</html>
<?php
}

/**
 * Render sidebar navigation for Bootstrap-based admin pages
 */
function render_admin_sidebar(string $activeTab = 'dashboard'): void
{
    $navItems = [
        'dashboard' => ['label' => 'Dashboard', 'url' => '/admin.php', 'icon' => 'bi-speedometer2'],
        'users'     => ['label' => 'Users', 'url' => '/admin_users.php', 'icon' => 'bi-people-fill'],
        'content'   => ['label' => 'Content', 'url' => '/admin_content_management.php', 'icon' => 'bi-collection-play-fill'],
        'tokens'    => ['label' => 'Tokens', 'url' => '/admin_token_dashboard.php', 'icon' => 'bi-coin'],
        'settings'  => ['label' => 'Settings', 'url' => '/admin_settings.php', 'icon' => 'bi-gear-fill'],
    ];
    ?>
    <style>
        .admin-sidebar {
            position: fixed;
            left: 0;
            top: 0;
            width: 250px;
            height: 100vh;
            background: linear-gradient(135deg, #6366f1, #8b5cf6);
            color: white;
            padding: 20px 0;
            box-shadow: 2px 0 10px rgba(0,0,0,0.1);
            z-index: 1000;
        }
        .admin-sidebar__brand {
            padding: 0 20px 20px;
            font-size: 1.5rem;
            font-weight: 700;
            border-bottom: 1px solid rgba(255,255,255,0.2);
            margin-bottom: 20px;
        }
        .admin-sidebar__nav {
            list-style: none;
            padding: 0;
            margin: 0;
        }
        .admin-sidebar__nav-item {
            margin-bottom: 5px;
        }
        .admin-sidebar__nav-link {
            display: flex;
            align-items: center;
            gap: 12px;
            padding: 12px 20px;
            color: rgba(255,255,255,0.8);
            transition: all 0.2s;
            text-decoration: none;
        }
        .admin-sidebar__nav-link:hover {
            background: rgba(255,255,255,0.1);
            color: white;
        }
        .admin-sidebar__nav-link.active {
            background: rgba(255,255,255,0.15);
            color: white;
            border-left: 3px solid white;
        }
        .admin-sidebar__footer {
            position: absolute;
            bottom: 20px;
            left: 0;
            right: 0;
            padding: 0 20px;
            font-size: 0.75rem;
            color: rgba(255,255,255,0.6);
            text-align: center;
        }
    </style>
    <div class="admin-sidebar">
        <div class="admin-sidebar__brand">
            EasyStream Admin
        </div>
        <ul class="admin-sidebar__nav">
            <?php foreach ($navItems as $key => $item): ?>
                <li class="admin-sidebar__nav-item">
                    <a href="<?= htmlspecialchars($item['url']) ?>"
                       class="admin-sidebar__nav-link <?= $key === $activeTab ? 'active' : '' ?>">
                        <i class="bi <?= htmlspecialchars($item['icon']) ?>"></i>
                        <span><?= htmlspecialchars($item['label']) ?></span>
                    </a>
                </li>
            <?php endforeach; ?>
        </ul>
        <div class="admin-sidebar__footer">
            Logged in as<br>
            <?= htmlspecialchars($_SESSION['ADMIN_NAME'] ?? 'Administrator') ?>
        </div>
    </div>
    <?php
}
