<?php
require_once 'includes/config.php';
require_once 'includes/auth.php';

// Start output buffering before any HTML is sent
ob_start();

// Only admin can access anything here
requireAdmin();

// Determine which page to load
$allowedPages = [
    'dashboard',
    'courses',
    'lessons',
    'flashcards',
    'exam_questions',
    'users',
    'subscriptions',
    'transactions',
    'leaderboard'
];

$page = $_GET['page'] ?? 'dashboard';
if (!in_array($page, $allowedPages)) {
    $page = 'dashboard';
}

// Build the file path
$pageFile = __DIR__ . '/pages/' . $page . '.php';

// Load the page (wrapped in header/footer layout)
require_once 'includes/header.php';

if (file_exists($pageFile)) {
    require_once $pageFile;
} else {
    echo '<div class="alert alert-warning">Page not found.</div>';
}

require_once 'includes/footer.php';

// Flush the buffer and send output
ob_end_flush();