<?php
$root = dirname(__DIR__);
$version = json_decode(file_get_contents($root . '/DDB_VERSION.json'), true);
$controller = file_get_contents($root . '/app/Controllers/BedsController.php');
$view = file_get_contents($root . '/app/Views/beds/index.php');
$css = file_get_contents($root . '/public/assets/css/beds-executive-parity.css');
$layout = file_get_contents($root . '/app/Views/layouts/main.php');
$js = file_get_contents($root . '/public/assets/js/beds-comparison.js');
$accept = file_get_contents($root . '/tools/beds-r45-p8-2-live-acceptance.php');

$checks = [
    'version' => ($version['version'] ?? '') === '11.0.11.79',
    'phase' => ($version['phase'] ?? '') === 'H11.0-P11.1-R45-P8.2',
    'unit_option_union' => strpos($controller, '$this->unitOptions($currentUnits, $compareUnits)') !== false,
    'comparison_kpi_grid' => strpos($view, 'beds82-comparison-kpi-grid') !== false,
    'live_snapshot_separated' => strpos($view, 'These four values describe the hospital now') !== false,
    'overall_two_period_values' => strpos($view, '$currentPeriodUtilization') !== false
        && strpos($view, '$comparePeriodUtilization') !== false,
    'selected_unit_two_period_values' => strpos($view, '$selectedUnitUtilization') !== false
        && strpos($view, '$selectedCompareUtilization') !== false,
    'comparison_leadership_signals' => strpos($view, 'The signals below are calculated from both matched periods') !== false,
    'unit_grouped_chart_target' => strpos($view, 'beds82_units_chart') !== false,
    'bed_grouped_chart_target' => strpos($view, 'beds82_beds_chart') !== false,
    'comparison_chart_config' => strpos($view, 'beds82_comparison_config') !== false,
    'old_current_only_chart_payload_removed' => strpos($view, 'beds134_units_data') === false
        && strpos($view, 'beds134_beds_data') === false,
    'old_single_series_render_removed' => strpos($view, 'renderHorizontal(') === false,
    'js_has_two_series' => strpos($js, "name: config.currentLabel") !== false
        && strpos($js, "name: config.compareLabel") !== false,
    'js_tooltip_has_both_periods' => strpos($js, "config.currentLabel + ': '") !== false
        && strpos($js, "config.compareLabel + ': '") !== false,
    'js_fallback_has_both_periods' => strpos($js, 'beds82-fallback-series is-compare') !== false,
    'unit_table_comparison_preserved' => strpos($view, '$unitComparison as $idx => $row') !== false,
    'bed_table_comparison_preserved' => strpos($view, '$bedComparison as $row') !== false,
    'comparison_css' => strpos($css, '.beds82-comparison-kpi-grid') !== false
        && strpos($css, '.beds82-primary-grid') !== false,
    'css_cache_identity' => strpos(
        $layout,
        'beds-executive-parity.css?v=11.0.11.79-r45-p8-2-full-comparison-view'
    ) !== false,
    'js_loaded_for_beds' => strpos(
        $layout,
        'beds-comparison.js?v=11.0.11.79-r45-p8-2-full-comparison-view'
    ) !== false,
    'live_acceptance' => strpos($accept, 'DDB_BEDS_R45_P8_2_LIVE_ACCEPTANCE_V1') !== false,
    'oracle_writes_none' => ($version['r45_p8_2']['oracle_writes'] ?? '') === 'NONE',
];

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

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