<?php
declare(strict_types=1);

if (PHP_SAPI !== 'cli') {
    http_response_code(404);
    exit;
}

require_once __DIR__ . '/../app/bootstrap.php';

try {
    $manager = new ChildDatabaseManager($config);
    $manager->verifyProvisioningPrivilege();

    $secretA = 'check-a-' . bin2hex(random_bytes(16));
    $secretB = 'check-b-' . bin2hex(random_bytes(16));
    $prefixA = $manager->tablePrefixForSecret($secretA);
    $prefixB = $manager->tablePrefixForSecret($secretB);
    if ($prefixA === $prefixB) throw new RuntimeException('پیشوند دو ربات متفاوت یکسان تولید شد.');

    fwrite(STDOUT, "OK: main database access, automatic per-bot table namespace, unique prefixes, create/read/write/drop test and protected registry path all passed.\n");
    exit(0);
} catch (Throwable $e) {
    fwrite(STDERR, "FAILED: " . $e->getMessage() . "\n");
    exit(1);
}
