<?php
/**
 * PHP built-in dev-server router.
 * Forces no-cache on every response so the Replit preview pane
 * always fetches fresh content after code changes.
 */
header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
header('Pragma: no-cache');
header('Expires: Thu, 01 Jan 1970 00:00:00 GMT');
header('X-Frame-Options: ALLOWALL');

$uri  = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$file = __DIR__ . $uri;

// Serve real static files (images, css, js, etc.) directly
if ($uri !== '/' && file_exists($file) && !is_dir($file)) {
    return false; // let the built-in server handle it
}

// Directory → look for index.php inside it
if (is_dir($file)) {
    $index = rtrim($file, '/') . '/index.php';
    if (file_exists($index)) {
        require $index;
        return true;
    }
}

// Exact .php file
if (file_exists($file . '.php')) {
    require $file . '.php';
    return true;
}

// Otherwise fall through to the built-in server (handles .php files natively)
return false;
