<?php
declare(strict_types=1);

require_once dirname(__DIR__) . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'bootstrap.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . 'lib.php';

/** @param array{ok: bool, checks: array<int, array{key: string, ok: bool, message: string}>} $result */
function install_render_requirements(array $result, string $locale = 'ar'): void
{
    $text = install_translations($locale);
    $direction = $locale === 'ar' ? 'rtl' : 'ltr';
    $title = install_escape($text['requirements']);

    echo '<!doctype html><html lang="' . install_escape($locale) . '" dir="' . $direction . '"><head>';
    echo '<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">';
    echo '<title>' . $title . ' · Quaeriva</title></head><body style="font-family:system-ui,sans-serif;max-width:52rem;margin:3rem auto;padding:0 1rem">';
    echo '<h1>' . $title . '</h1><ul>';

    foreach ($result['checks'] as $check) {
        $state = $check['ok'] ? '✓' : (empty($check['advisory']) ? '✗' : '!');
        echo '<li><strong>' . $state . '</strong> ' . install_escape($check['message']) . '</li>';
    }

    echo '</ul></body></html>';
}

$isDirectRequest = isset($_SERVER['SCRIPT_FILENAME'])
    && realpath((string) $_SERVER['SCRIPT_FILENAME']) === realpath(__FILE__);

if ($isDirectRequest) {
    app_bootstrap();
    $locale = install_requested_locale();
    $result = install_check_requirements(app_config('database'), dirname(__DIR__));
    install_render_requirements($result, $locale);
    return $result;
}

return null;
