<?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.95',
    'phase' => ($version['phase'] ?? '') === 'H11.0-P11.1-R45-P11.2.6',
    'monthly_breadth_used' => strpos($view, '$monthlyBreadth') !== false,
    'six_month_cards_source' => strpos($view, 'branch126-month-grid') !== false
        && strpos($view, 'foreach ($monthIds as $monthId => $monthName)') !== false,
    'improved_count' => strpos($view, "improved', 'تحسنت") !== false,
    'declined_count' => strpos($view, "declined', 'تراجعت") !== false,
    'stable_count' => strpos($view, "stable', 'استقرت") !== false,
    'plain_status' => strpos($view, 'More services improved') !== false
        && strpos($view, 'More services declined') !== false
        && strpos($view, 'Balanced movement') !== false,
    'most_improvements_wording' => strpos($view, 'Most service improvements') !== false,
    'most_declines_wording' => strpos($view, 'Most service declines') !== false,
    'tie_aware_months' => strpos($view, 'mostImprovedMonths') !== false
        && strpos($view, 'mostDeclinedMonths') !== false
        && strpos($view, "implode(', ',") !== false,
    'ambiguous_chart_removed' => strpos($view, 'branch125_monthly_improvement_chart') === false,
    'ambiguous_best_weakest_section_removed' => strpos($view, 'Best and weakest months') === false,
    'monthly_chart_js_removed' => strpos($js, 'renderMonthlyImprovement') === false,
    'six_month_cards_gate' => strpos($js, 'monthCards === 6') !== false,
    'responsive_month_cards' => strpos($css, '.branch126-month-grid') !== false
        && strpos($css, '@media(max-width:700px)') !== false,
    'asset_cache_busted' => strpos($layout, '11.0.11.95-r45-p11-2-6-monthly-clarity') !== 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_6']['oracle_contract_changed'] ?? true) === false,
    'query_count_unchanged' => ($version['r45_p11_2_6']['query_count_changed'] ?? true) === false,
    'oracle_writes_none' => ($version['r45_p11_2_6']['oracle_writes'] ?? '') === 'NONE',
];

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

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