<?php
declare(strict_types=1);

require __DIR__ . '/site/Site.php';
require __DIR__ . '/site/Http.php';

$demo = Atucha\Http::isDemo($_SERVER['SCRIPT_NAME'] ?? '/index.php');
Atucha\Http::pageHeaders($demo);

try {
    $path = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH);
    $name = basename(is_string($path) ? $path : '');
    $page = $name === '' || str_ends_with((string) $path, '/')
        ? 'index'
        : preg_replace('/\.(?:html|php)$/', '', $name);
    echo (new Atucha\Site(__DIR__))->renderPage($page, $demo);
} catch (Throwable $error) {
    $status = $error->getCode() === 404 ? 404 : 500;
    http_response_code($status);
    echo $status === 404 ? 'Página no encontrada.' : 'No se pudo cargar la página. Intentá nuevamente.';
}
