<?php
declare(strict_types=1);
require_once __DIR__ . '/install_bootstrap.php';
rozklad_redirect_to_install_if_needed();
rozklad_require_valid_license();

$runtime = is_file(__DIR__ . '/db_runtime.php') ? require __DIR__ . '/db_runtime.php' : [];
if (!is_array($runtime) || empty($runtime['host']) || empty($runtime['name']) || empty($runtime['user'])) {
    http_response_code(500);
    die('База даних ще не налаштована. Відкрийте install.php.');
}

$db_host = (string)$runtime['host'];
$db_port = (int)($runtime['port'] ?? 3306);
$db_user = (string)$runtime['user'];
$db_pass = (string)($runtime['pass'] ?? '');
$db_name = (string)$runtime['name'];

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
try {
    $conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name, $db_port);
    if (!$conn) {
        throw new RuntimeException(mysqli_connect_error());
    }
    mysqli_set_charset($conn, 'utf8mb4');
} catch (Throwable $e) {
    http_response_code(500);
    die('Помилка підключення до БД: ' . htmlspecialchars($e->getMessage(), ENT_QUOTES, 'UTF-8'));
}
