<?php
$root = dirname(__DIR__);
$version = json_decode(file_get_contents($root . '/DDB_VERSION.json'), true);
$repo = file_get_contents($root . '/app/Repositories/SpecialtyFinanceRepository.php');
$controller = file_get_contents($root . '/app/Controllers/SpecialtyFinanceController.php');
$view = file_get_contents($root . '/app/Views/specialty_finance/index.php');
$css = file_get_contents($root . '/public/assets/css/executive-ui-core.css');

$checks = [
    'version' => ($version['version'] ?? '') === '11.0.11.72',
    'phase' => ($version['phase'] ?? '') === 'H11.0-P11.1-R45-P6.0.1',
    'coverage_method' => strpos($repo, 'public function yearCoverage(') !== false,
    'current_year_injected' => strpos($controller, 'array_unshift($years, $currentYear)') !== false,
    'current_year_default' => strpos($controller, '$year = $currentYear;') !== false,
    'years_sorted' => strpos($controller, 'rsort($years, SORT_NUMERIC)') !== false,
    'coverage_loaded' => strpos($controller, '$currentCoverage = $repo->yearCoverage($year)') !== false,
    'coverage_passed_to_view' => strpos($controller, "'coverage' => [") !== false,
    'coverage_alert' => strpos($view, 'sf31-coverage-alert') !== false,
    'no_false_zero_kpi' => strpos($view, "\$currentYearHasData ? \$compact") !== false,
    'awaiting_source_text' => strpos($view, 'Awaiting source coverage') !== false,
    'css_contract' => strpos($css, '.sf31-coverage-alert') !== false,
    'oracle_writes_none' => ($version['r45_p6_0_1']['oracle_writes'] ?? '') === 'NONE',
];

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