<?php
declare(strict_types=1);

$root = dirname(__DIR__);
$version = json_decode((string)file_get_contents($root . '/DDB_VERSION.json'), true);
$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.96',
    'phase' => ($version['phase'] ?? '') === 'H11.0-P11.1-R45-P11.2.7',
    'pulse_section' => strpos($view, 'branch127-monthly') !== false,
    'two_branch_rows_source' => strpos($view, 'branch127-pulse-row') !== false
        && strpos($view, 'foreach ($branchDefinitions as $branchCode => $branch)') !== false,
    'six_month_columns_source' => strpos($view, 'foreach ($monthIds as $monthName)') !== false,
    'simple_status_labels' => strpos($view, "Improving', 'تحسن") !== false
        && strpos($view, "Declining', 'تراجع") !== false
        && strpos($view, "Balanced', 'متوازن") !== false,
    'latest_month_insight' => strpos($view, 'LATEST COMPLETED MONTH') !== false,
    'review_month_insight' => strpos($view, 'MONTH REQUIRING REVIEW') !== false,
    'monthly_difference_insight' => strpos($view, 'LARGEST MONTHLY DIFFERENCE') !== false,
    'broader_improvement_removed' => strpos($view, 'months with broader improvement') === false,
    'best_month_removed_from_branch_card' => strpos($view, "Best month', 'أفضل شهر") === false,
    'weakest_month_removed_from_branch_card' => strpos($view, "Weakest month', 'أضعف شهر") === false,
    'old_month_cards_not_in_view' => strpos($view, 'branch126-month-card') === false,
    'pulse_css' => strpos($css, '.branch127-pulse-table') !== false,
    'three_count_branch_cards' => strpos($css, 'grid-template-columns:repeat(3,minmax(0,1fr))') !== false,
    'browser_gate_two_rows' => strpos($js, 'pulseRows === 2') !== false,
    'browser_gate_twelve_cells' => strpos($js, 'pulseCells === 12') !== false,
    'browser_gate_three_insights' => strpos($js, 'monthlyInsights === 3') !== false,
    'asset_cache_busted' => strpos($layout, '11.0.11.96-r45-p11-2-7-monthly-executive-pulse') !== false,
    'order_hotfix_preserved' => strpos($repo, 'ORDER BY 1, 2') !== false,
    'emergency_hotfix_preserved' => strpos($repo, 'clinic_id IN (16, 297, 298)') !== 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_7']['oracle_contract_changed'] ?? true) === false,
    'query_count_unchanged' => ($version['r45_p11_2_7']['query_count_changed'] ?? true) === false,
    'oracle_writes_none' => ($version['r45_p11_2_7']['oracle_writes'] ?? '') === 'NONE',
];

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

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