<?php
// ================================================
// Promuex Production Dynamic Router - Full Pass-through
// ================================================

// Normalize URI
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$uri = rtrim($uri, '/');
if ($uri === '') $uri = '/';

// Base directory
$baseDir = __DIR__;

// ----------------------
// Force HTTPS
// ----------------------
if (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] === 'off') {
    $redirect = 'https://' . $_SERVER['HTTP_HOST'] . $uri;
    if (!empty($_SERVER['QUERY_STRING'])) $redirect .= '?' . $_SERVER['QUERY_STRING'];
    header('Location: ' . $redirect, true, 301);
    exit;
}

// ----------------------
// Serve existing files directly (images, documents, CSS, JS, etc.)
// ----------------------
$requestedFile = realpath($baseDir . $uri);
if ($requestedFile && is_file($requestedFile)) {
    // Serve the file with proper headers
    $mimeType = mime_content_type($requestedFile);
    header('Content-Type: ' . $mimeType);
    header('Content-Length: ' . filesize($requestedFile));
    readfile($requestedFile);
    exit;
}

// ----------------------
// Default Home Page
// ----------------------
if ($uri === '/') {
    require $baseDir . '/home.php';
    exit;
}

// ----------------------
// Pretty URL Routes
// ----------------------
$routes = [
    '/ho-2901aa39-6fd2-4bfa-9cb8-1f47be369128-mo' => '/home.php',//Main Home page
    '/ex-68c3f0c7-eeb0-8327-95e1-e0b91a2f82af-am' => '/certification/exam/index.php',//Learner's login User UI
    '/ex-68c3f0c7-eeb0-8327-95e1-e0b91a2f82af-am/login-ui' => '/certification/exam/login-ui/index.php',//Learner's Qeury
    '/learner-ex-68c3f0c7-eeb0-8327-95e1-e0b91a2f82af-am-dashaboard' => '/certification/exam/home.php',//learner's dashbaord
    '/leaner-68c3f0c7-eeb0-8327-95e1-e0b91a2f82af-am-dashboardxx' => '/certification/exam/pages/home.php',//Home page Queries/Detailed
    '/exam/logoutExe' => '/certification/exam/query/logoutExe.php',
    '/reg-68c3f0c7-eeb0-8327-95e1-e0b91a2f82af-am-gistration' => '/certification/exam/registration.php',//Registration
    '/for-68c3f0c7-eeb0-8327-95e1-e0b91a2f82af-am-get_password' => '/certification/exam/forget_password.php',
     '/community_for-68c3f0c7-eeb0-8327-95e1-e0b91a2f82af-countries_progress' => '/countries_progress_home.php',// Promuex Global Learners Community
    '/about_us_for-68c3f0c7-eeb0-8327-95e1-e0b91a2f82af-about_us' => '/dashbaord_files/about_us.php',        // About us
    
    '/all' => '/all.php',
    '/demand' => '/demand.php',
    '/download' => '/download.php',
    '/download_document' => '/download_document.php',
    '/Fully_Sponsored' => '/Fully_Sponsored.php',
    '/newcert' => '/newcert.php',
    '/popular' => '/popular.php',
    '/top' => '/top.php',
    '/search-68c3f0c7-eeb0-8327-95e1-e0b91a2f82af-am-suggestion' => '/search_suggestion/index.php',
    '/general-68c3f0c7-eeb0-8327-95e1-e0b91a2f82af-am-Labour' => '/General_Labour.php',
    '/health-68c3f0c7-eeb0-8327-95e1-e0b91a2f82af-amservices' => '/health_services.php',
    '/contact-us' => '/contact-us.php',
    '/about_us' => '/dashbaord_files/about_us.php',        // About us
    '/how_promuex_works' => '/dashbaord_files/how_promuex_works.php',
    '/start_model_learner_lessons_update' => '/start_model_learner_lessons_update.php',
    '/verify' => '/verify.php',
    '/certificate_description' => '/certificate_description.php'
];

// ----------------------
// Handle mapped routes
// ----------------------
if (isset($routes[$uri])) {
    $file = $baseDir . $routes[$uri];
    if (file_exists($file)) {
        require $file;
        exit;
    }
}

// ----------------------
// Automatic fallback to PHP file (pretty URLs)
// ----------------------
$dynamicFile = $baseDir . $uri . '.php';
if (file_exists($dynamicFile)) {
    require $dynamicFile;
    exit;
}

// ----------------------
// Always allow uploads folder and subfolders
// ----------------------
if (preg_match('#^/certification/exam/uploads/#', $uri)) {
    $uploadFile = $baseDir . $uri;
    if (file_exists($uploadFile)) {
        $mimeType = mime_content_type($uploadFile);
        header('Content-Type: ' . $mimeType);
        header('Content-Length: ' . filesize($uploadFile));
        readfile($uploadFile);
        exit;
    }
}

// ----------------------
// 404 fallback
// ----------------------
http_response_code(404);
require $baseDir . '/pages/404.php';
exit;
