<?php
declare(strict_types=1);

chdir(__DIR__);

$uri = (string)($_SERVER['REQUEST_URI'] ?? '/');
$path = parse_url($uri, PHP_URL_PATH);
$path = is_string($path) ? rawurldecode($path) : '/';
$path = trim($path, '/');

if ($path === '') {
    $_GET['slug'] = $_GET['slug'] ?? 'index';
    require __DIR__ . '/public-page.php';
    exit;
}

if (preg_match('/(?:^|\/)\.\.(?:\/|$)/', $path) || preg_match('/[\x00-\x1F\x7F]/', $path)) {
    http_response_code(404);
    exit('Nie znaleziono strony.');
}

if (preg_match('/^favicon\.ico$/i', $path)) {
    header('Content-Type: image/svg+xml; charset=utf-8');
    readfile(__DIR__ . '/assets/favicon.svg');
    exit;
}

if (preg_match('/^(?:db|config|bootstrap|commercial|csrf|autoload)(?:\.php)?$/i', $path)) {
    http_response_code(404);
    exit('Nie znaleziono strony.');
}

if (preg_match('/^wyrok\/([0-9]{4})\/([0-9]+)\/?$/i', $path, $match)) {
    $_GET['slug'] = 'wyrok';
    $_GET['year'] = $_GET['year'] ?? $match[1];
    $_GET['source_id'] = $_GET['source_id'] ?? $match[2];
    $_GET['id'] = $_GET['id'] ?? ('judgment-' . $match[1] . '-' . $match[2]);
    require __DIR__ . '/public-page.php';
    exit;
}

if (preg_match('/^wyrok\/([0-9]{4})\/([a-z0-9-]+)\/?$/i', $path, $match)) {
    $target = strtolower($match[2]) === 'orzecznictwo' ? '/orzecznictwo' : '/wyroki?year=' . rawurlencode($match[1]);
    header('Location: ' . $target, true, 302);
    exit;
}

if (preg_match('/^wyrok\/([0-9]+)\/?$/i', $path, $match)) {
    $_GET['slug'] = 'wyrok';
    $_GET['source_id'] = $_GET['source_id'] ?? $match[1];
    require __DIR__ . '/public-page.php';
    exit;
}

if (preg_match('/^podmiot\/krs\/([0-9]{10})\/sprawozdanie\/([0-9]{4})\/([a-z0-9-]+)\/?$/i', $path, $match)) {
    $_GET['krs'] = $_GET['krs'] ?? $match[1];
    $_GET['year'] = $_GET['year'] ?? $match[2];
    $_GET['report'] = $_GET['report'] ?? $match[3];
    require __DIR__ . '/rdf-sprawozdanie.php';
    exit;
}

if (preg_match('/^podmiot\/krs\/([0-9]{10})\/?$/i', $path, $match)) {
    $_GET['krs'] = $_GET['krs'] ?? $match[1];
    require __DIR__ . '/podmiot-krs.php';
    exit;
}

if (preg_match('/^pkd\/([0-9A-Za-z.-]+)\/?$/', $path, $match)) {
    $_GET['code'] = $_GET['code'] ?? $match[1];
    require __DIR__ . '/pkd.php';
    exit;
}

if (!preg_match('/^[a-z0-9][a-z0-9-]{0,100}(?:\.html|\.php)?$/i', $path)) {
    http_response_code(404);
    exit('Nie znaleziono strony.');
}

$slug = preg_replace('/\.(?:html|php)$/i', '', $path) ?? $path;
$slug = strtolower($slug);

$directPhpSlugs = [
    'admin' => true,
    'admin-importy' => true,
    'admin-rozliczenia' => true,
    'contact' => true,
    'kontakt' => true,
    'logowanie' => true,
    'dashboard' => true,
    'crbr-podmiot' => true,
    'newsy-prawne' => true,
    'pkd' => true,
    'podmiot-krs' => true,
    'rdf-sprawozdanie' => true,
    'rejestry-branzowe' => true,
    'robots' => true,
    'sitemap' => true,
    'sluzebnosc-przesylu' => true,
    'zmiany-prawa' => true,
];

$phpPath = __DIR__ . '/' . $slug . '.php';
if (isset($directPhpSlugs[$slug]) && is_file($phpPath) && is_readable($phpPath)) {
    require $phpPath;
    exit;
}

$_GET['slug'] = $_GET['slug'] ?? $slug;
require __DIR__ . '/public-page.php';
