<?php

declare(strict_types=1);

$options = getopt('', ['target::']);
require __DIR__ . DIRECTORY_SEPARATOR . 'diar6b4-r2-lib.php';

try {
    $root = diarR2Bootstrap($options);
    $result = diarR2ReadOnlyDiagnostics();
    echo 'DIAR_6B_4_R2_DIAGNOSTICS|PASS|DB=ORANJC|SCHEMA=WATANY|RAW_CASES=' . (int)($result['integrity']['raw_total_cases'] ?? 0)
        . '|GOVERNED_CASES=' . (int)($result['integrity']['governed_cases'] ?? 0)
        . '|EXCLUDED_STORE_CASES=' . (int)($result['integrity']['excluded_store_cases'] ?? 0)
        . '|QA_CASES=' . (int)($result['qa_case_count'] ?? 0)
        . '|ROLE_MATRIX=PASS|WRITES=NONE|SOURCE_MM_WRITES=NONE|DB_LINK=NONE' . PHP_EOL;
    echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . PHP_EOL;
    exit(0);
} catch (Throwable $error) {
    fwrite(STDERR, 'DIAR_6B_4_R2_DIAGNOSTICS|FAIL|ERROR=' . $error->getMessage() . PHP_EOL);
    exit(20);
}
