<?php
declare(strict_types=1);

/**
 * Development router for PHP's built-in web server.
 * Run from the project root:
 * php -S 127.0.0.1:8003 -t public public/router.php
 */
require_once dirname(__DIR__) . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'bootstrap.php';
app_bootstrap();

$requestUri = (string) ($_SERVER['REQUEST_URI'] ?? '/');
$requestPath = parse_url($requestUri, PHP_URL_PATH);
$requestQuery = parse_url($requestUri, PHP_URL_QUERY);
$assetQuery = [];
if (is_string($requestQuery) && $requestQuery !== '') parse_str($requestQuery, $assetQuery);
$requestPath = is_string($requestPath) && $requestPath !== '' ? rawurldecode($requestPath) : '/';
$basePath = app_base_path((string) app_env('APP_BASE_URL', ''));
$relativePath = null;

if ($basePath === '') {
    $relativePath = $requestPath;
} elseif ($requestPath === $basePath || $requestPath === $basePath . '/') {
    $relativePath = '/';
} elseif (str_starts_with($requestPath, $basePath . '/')) {
    $relativePath = substr($requestPath, strlen($basePath));
}

if (is_string($relativePath)) {
    $staticPath = ltrim($relativePath, '/');
    $extension = strtolower(pathinfo($staticPath, PATHINFO_EXTENSION));
    $allowedExtensions = ['css', 'gif', 'ico', 'jpeg', 'jpg', 'js', 'map', 'png', 'svg', 'ttf', 'webp', 'woff', 'woff2'];
    $publicRoot = realpath(__DIR__);
    $candidate = $staticPath !== '' && !str_contains($staticPath, '..') && in_array($extension, $allowedExtensions, true) && $publicRoot !== false
        ? realpath($publicRoot . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $staticPath))
        : false;

    if ($publicRoot !== false && is_string($candidate) && str_starts_with($candidate, $publicRoot . DIRECTORY_SEPARATOR) && is_file($candidate)) {
        $contentTypes = [
            'css' => 'text/css; charset=UTF-8',
            'js' => 'application/javascript; charset=UTF-8',
            'map' => 'application/json; charset=UTF-8',
            'svg' => 'image/svg+xml',
            'png' => 'image/png',
            'jpg' => 'image/jpeg',
            'jpeg' => 'image/jpeg',
            'gif' => 'image/gif',
            'webp' => 'image/webp',
            'ico' => 'image/x-icon',
            'woff' => 'font/woff',
            'woff2' => 'font/woff2',
            'ttf' => 'font/ttf',
        ];
        header('Content-Type: ' . ($contentTypes[$extension] ?? 'application/octet-stream'));
        header('Content-Length: ' . (string) filesize($candidate));
        header('X-Content-Type-Options: nosniff');
        $assetVersion = is_scalar($assetQuery['v'] ?? null) ? (string) $assetQuery['v'] : '';
        if ($assetVersion !== '' && hash_equals(app_version(), $assetVersion)) {
            header('Cache-Control: public, max-age=31536000, immutable');
        } else {
            header('Cache-Control: public, max-age=3600');
        }
        readfile($candidate);
        exit;
    }
}

// Make the front controller execute its normal direct-request dispatch block.
$_SERVER['SCRIPT_FILENAME'] = __DIR__ . DIRECTORY_SEPARATOR . 'index.php';
require __DIR__ . DIRECTORY_SEPARATOR . 'index.php';
