<?php

declare(strict_types=1);

$root = dirname(__DIR__);
$version = json_decode((string)file_get_contents($root . '/DDB_VERSION.json'), true);
$controller = (string)file_get_contents($root . '/app/Controllers/BranchComparisonController.php');
$repo = (string)file_get_contents($root . '/app/Repositories/BranchComparisonRepository.php');
$view = (string)file_get_contents($root . '/app/Views/branch_comparison/index.php');
$css = (string)file_get_contents($root . '/public/assets/css/branch-comparison.css');
$js = (string)file_get_contents($root . '/public/assets/js/branch-comparison.js');
$layout = (string)file_get_contents($root . '/app/Views/layouts/main.php');
$env = (string)file_get_contents($root . '/.env');

$checks = [
    'version' => ($version['version'] ?? '') === '11.0.11.93',
    'phase' => ($version['phase'] ?? '') === 'H11.0-P11.1-R45-P11.2.4',
    'executive_analysis_contract' => strpos($controller, 'buildExecutiveAnalysis(') !== false,
    'domain_decision_status' => strpos($controller, 'domainDecisionStatus(') !== false,
    'domain_leadership_focus' => strpos($controller, 'domainLeadershipFocus(') !== false,
    'volume_not_score_contract' => strpos($controller, 'higher workload does not automatically mean better performance') !== false,
    'executive_hero' => strpos($view, 'branch124-hero') !== false
        && strpos($view, 'EXECUTIVE BRANCH POSITION') !== false,
    'four_decision_kpis' => strpos($view, 'Higher recorded volume') !== false
        && strpos($view, 'Branch under watch') !== false
        && strpos($view, 'Strongest improvement') !== false
        && strpos($view, 'Largest decline') !== false,
    'one_initial_overview_chart' => substr_count($view, 'id="branch124_overview_chart"') === 1,
    'three_item_agenda' => substr_count($view, '<article>') >= 3
        && strpos($view, 'Immediate attention') !== false
        && strpos($view, 'Cross-branch divergence') !== false
        && strpos($view, 'Positive signal') !== false,
    'six_row_matrix_source' => strpos($view, 'branch124-matrix') !== false
        && strpos($view, 'foreach ($domainRows as $row)') !== false,
    'manager_friendly_matrix' => strpos($view, 'Volume is workload, not a performance score.') !== false,
    'details_collapsed' => strpos($view, '<details class="branch124-card branch124-deep-dive"') !== false,
    'six_lazy_detail_configs' => strpos($view, 'branch124_detail_chart_config') !== false
        && strpos($js, 'renderDetail(key)') !== false,
    'no_visible_legacy_domain_cards' => strpos($view, 'LEGACY DOMAIN') === false
        && strpos($view, 'branch112-domain-grid') === false,
    'no_user_facing_query_count' => strpos($view, 'Four consolidated read-only queries') === false,
    'browser_gate' => strpos($js, 'DDB_BRANCH_COMPARISON_R45_P11_2_4_BROWSER_GATE') !== false,
    'initial_density_gate' => strpos($js, 'visibleMonthlyCharts') !== false
        && strpos($js, 'expected: 0') !== false,
    'matrix_rows_gate' => strpos($js, 'matrixRows === 6') !== false,
    'agenda_gate' => strpos($js, 'agendaItems === 3') !== false,
    'responsive_mobile_matrix' => strpos($css, '.branch124-matrix thead{display:none}') !== false,
    'semantic_positive' => strpos($css, '--bc124-positive:#047857') !== false,
    'semantic_negative' => strpos($css, '--bc124-negative:#c5283d') !== false,
    'semantic_divergence' => strpos($css, '--bc124-warning:#a16207') !== false,
    'asset_cache_busted' => strpos($layout, '11.0.11.93-r45-p11-2-4-executive-simplification') !== false,
    'order_hotfix_preserved' => strpos($repo, 'ORDER BY 1, 2') !== false,
    'emergency_hotfix_preserved' => strpos($repo, 'clinic_id IN (16, 297, 298)') !== false
        && strpos($repo, 'r.clinic_id') === false,
    'revenue_summary_enabled' => preg_match('/^USE_REVENUE_SUMMARY=1$/m', $env) === 1,
    'revenue_summary_age_30' => preg_match('/^REVENUE_SUMMARY_MAX_AGE_HOURS=30$/m', $env) === 1,
    'oracle_contract_unchanged' => ($version['r45_p11_2_4']['oracle_contract_changed'] ?? true) === false,
    'query_count_unchanged' => ($version['r45_p11_2_4']['query_count_changed'] ?? true) === false,
    'oracle_writes_none' => ($version['r45_p11_2_4']['oracle_writes'] ?? '') === 'NONE',
];

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

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

exit($failed === [] ? 0 : 1);
