<?php
$root = dirname(__DIR__);
$version = json_decode(file_get_contents($root . '/DDB_VERSION.json'), true);
$tool = file_get_contents($root . '/tools/specialty-finance-source-refresh-discovery.php');

$checks = [
    'version' => ($version['version'] ?? '') === '11.0.11.73',
    'phase' => ($version['phase'] ?? '') === 'H11.0-P11.1-R45-P6.0.2',
    'schema' => strpos($tool, 'DDB_SPECIALTY_FINANCE_SOURCE_REFRESH_DISCOVERY_V1') !== false,
    'all_source' => strpos($tool, 'all_source') !== false,
    'all_dependencies' => strpos($tool, 'all_dependencies') !== false,
    'scheduler_jobs' => strpos($tool, 'all_scheduler_jobs') !== false,
    'legacy_jobs' => strpos($tool, 'all_jobs') !== false,
    'triggers' => strpos($tool, 'all_triggers') !== false,
    'mviews' => strpos($tool, 'all_mviews') !== false,
    'synonyms' => strpos($tool, 'all_synonyms') !== false,
    'year_coverage' => strpos($tool, 'year_coverage') !== false,
    'no_auto_write' => strpos($tool, "'can_auto_fix_now' => false") !== false,
    'oracle_writes_none' => ($version['r45_p6_0_2']['oracle_writes'] ?? '') === 'NONE',
];

$failed = [];
foreach ($checks as $name => $ok) {
    if (!$ok) $failed[] = $name;
}
echo json_encode([
    'schema' => 'R45_P6_0_2_QA',
    'checks' => $checks,
    'passed' => count($checks) - count($failed),
    'failed' => $failed,
], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES), PHP_EOL;
exit(count($failed) === 0 ? 0 : 1);
