<?php
$root = dirname(__DIR__);
$version = json_decode(file_get_contents($root . '/DDB_VERSION.json'), true);
$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');
$controller = file_get_contents($root . '/app/Controllers/LabRadiologyController.php');

$checks = [
    'version' => ($version['version'] ?? '') === '11.0.11.85',
    'phase' => ($version['phase'] ?? '') === 'H11.0-P11.1-R45-P10.1',
    'light_kpi_grid' => strpos($view, 'diagnostics110-kpi-grid') !== false,
    'four_kpi_definitions' => substr_count($view, "'tone' =>") === 4,
    'light_summary_card' => strpos($view, 'diagnostics110-summary-card') !== false,
    'dark_hero_removed' => strpos($view, '<section class="diagnostics110-hero">') === false,
    'semantic_positive_green' => strpos($css, '#047857') !== false
        && strpos($css, '.diagnostics110-delta.up') !== false,
    'semantic_negative_red' => strpos($css, '#c5283d') !== false
        && strpos($css, '.diagnostics110-delta.down') !== false,
    'summary_positive_negative' => strpos($css, '.diagnostics110-summary-signals .is-positive') !== false
        && strpos($css, '.diagnostics110-summary-signals .is-negative') !== false,
    'fair_comparison_preserved' => strpos($controller, 'FairYearComparisonPolicy') !== false
        && strpos($view, 'Fair comparison:') !== false,
    'all_month_chart_preserved' => strpos($view, 'id="diagnostics110_chart"') !== false
        && strpos($view, "data-diagnostics110-metric=\"combined\"") !== false,
    'decision_agenda_preserved' => strpos($view, 'diagnostics110-agenda') !== false,
    'details_collapsed' => strpos($view, '<details class="diagnostics110-details"') !== false,
    'css_cache_busted' => strpos($layout, '11.0.11.85-r45-p10-1-light-summary-semantic-colors') !== false,
    'oracle_writes_none' => ($version['r45_p10_1']['oracle_writes'] ?? '') === 'NONE',
];

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

echo json_encode([
    'schema' => 'R45_P10_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);
