<?php
declare(strict_types=1);

// Installer-only CLI probe. It validates the exact PHP/MariaDB connection
// used by the application and runs the core schema bootstrap before the UI opens.
if (PHP_SAPI !== 'cli') {
    http_response_code(404);
    exit(1);
}

require_once __DIR__ . '/db_bootstrap.php';
$db = ensure_db(false);
if (!($db instanceof mysqli)) {
    fwrite(STDERR, "Database connection failed.\n");
    exit(1);
}

// Never allow the installer to report success with a partial database. Force
// one repair pass, then validate the tables used as soon as the UI opens.
$_ENV['UA_FORCE_SCHEMA_MIGRATE'] = '1';
ua_core_schema_migrate($db);
$missing = ua_core_schema_missing_tables($db);
if ($missing !== []) {
    fwrite(STDERR, 'Database schema incomplete. Missing: ' . implode(', ', $missing) . "\n");
    exit(2);
}
fwrite(STDOUT, "Database schema ready.\n");
exit(0);
