<?php
$root = dirname(__DIR__);
$version = json_decode(file_get_contents($root . '/DDB_VERSION.json'), true);
$controller = file_get_contents($root . '/app/Controllers/LabRadiologyController.php');
$view = file_get_contents($root . '/app/Views/lab_radiology/index.php');
$layout = file_get_contents($root . '/app/Views/layouts/main.php');
$css = file_get_contents($root . '/public/assets/css/diagnostics-executive-redesign.css');
$js = file_get_contents($root . '/public/assets/js/diagnostics-comparison.js');
$repo = file_get_contents($root . '/app/Repositories/DiagnosticsRepository.php');
$live = file_get_contents($root . '/tools/diagnostics-r45-p10-live-acceptance.php');

$checks = [
    'version' => ($version['version'] ?? '') === '11.0.11.84',
    'phase' => ($version['phase'] ?? '') === 'H11.0-P11.1-R45-P10',
    'fair_policy_loaded' => strpos($controller, 'use App\\Services\\FairYearComparisonPolicy;') !== false,
    'fair_contract_resolved' => strpos($controller, '(new FairYearComparisonPolicy())->resolve') !== false,
    'sargable_year_ranges' => strpos($controller, "\$rangeFrom = sprintf('%04d-01-01', \$year)") !== false
        && strpos($controller, "\$rangeTo = sprintf('%04d-12-31', \$year)") !== false,
    'monthly_filter' => substr_count($controller, '$this->filterMonthlyRows(') === 2,
    'combined_chart' => strpos($controller, "'combined' => [") !== false
        && strpos($view, "data-diagnostics110-metric=\"combined\"") !== false,
    'all_matched_months' => strpos($view, 'topFiveDiagnosticsPayload') === false,
    'single_executive_hero' => substr_count($view, 'diagnostics110-hero') >= 1,
    'one_chart_canvas' => substr_count($view, 'id="diagnostics110_chart"') === 1,
    'decision_agenda' => strpos($view, 'diagnostics110-agenda') !== false,
    'service_balance' => strpos($view, 'diagnostics110-service-balance') !== false,
    'details_collapsed' => strpos($view, '<details class="diagnostics110-details"') !== false,
    'old_r38_shell_removed' => strpos($view, 'diagnostics138-shell') === false,
    'stable_hover_payload' => strpos($controller, "\$payload['stableHover'] = true") !== false,
    'flow_safe_header' => strpos($layout, "['operations', 'lab']") !== false,
    'css_loaded' => strpos($layout, 'diagnostics-executive-redesign.css?v=11.0.11.84-r45-p10-fair-executive') !== false,
    'js_loaded' => strpos($layout, 'diagnostics-comparison.js?v=11.0.11.84-r45-p10-fair-executive') !== false,
    'browser_gate' => strpos($js, 'DDB_DIAGNOSTICS_R45_P10_BROWSER_GATE') !== false,
    'responsive_css' => strpos($css, '@media(max-width:760px)') !== false,
    'repository_select_only' => stripos($repo, 'insert ') === false
        && stripos($repo, 'update ') === false
        && stripos($repo, 'delete ') === false,
    'live_acceptance' => strpos($live, 'DDB_DIAGNOSTICS_R45_P10_LIVE_ACCEPTANCE_V1') !== false,
    'oracle_writes_none' => ($version['r45_p10']['oracle_writes'] ?? '') === 'NONE',
];

$failed = [];
foreach ($checks as $name => $ok) {
    if (!$ok) $failed[] = $name;
}

echo json_encode([
    'schema' => 'R45_P10_QA',
    'checks' => $checks,
    'passed' => count($checks) - count($failed),
    'failed' => $failed,
], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES), PHP_EOL;

exit(count($failed) === 0 ? 0 : 1);
