<?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.94',
    'phase' => ($version['phase'] ?? '') === 'H11.0-P11.1-R45-P11.2.5',
    'branch_profiles_contract' => strpos($controller, "'branch_profiles' => \$branchProfiles") !== false,
    'monthly_breadth_contract' => strpos($controller, "'monthly_breadth' => \$monthlyBreadth") !== false,
    'monthly_improvement_payload' => strpos($controller, "'monthly_improvement_chart_payload'") !== false,
    'branch_leading_services' => strpos($controller, "'leading_services' => 0") !== false,
    'branch_improving_services' => strpos($controller, "'improving_services' => 0") !== false,
    'branch_declining_services' => strpos($controller, "'declining_services' => 0") !== false,
    'best_and_weakest_months' => strpos($controller, "'best_month' => null") !== false
        && strpos($controller, "'weakest_month' => null") !== false,
    'technical_oracle_copy_removed' => stripos($view, 'Oracle') === false,
    'technical_read_only_copy_removed' => stripos($view, 'read-only') === false,
    'technical_approved_count_removed' => stripos($view, 'approved branches') === false,
    'technical_domains_count_removed' => strpos($view, '6/6 domains') === false,
    'branch_scorecards' => strpos($view, 'branch125-branch-grid') !== false,
    'two_branch_cards_source' => strpos($view, 'foreach ($branchDefinitions as $branchCode => $branch)') !== false,
    'monthly_chart' => strpos($view, 'branch125_monthly_improvement_chart') !== false,
    'monthly_highlights' => strpos($view, 'Best and weakest months') !== false,
    'six_service_cards_source' => strpos($view, 'branch125-service-grid') !== false
        && strpos($view, 'foreach ($domainRows as $row)') !== false,
    'old_horizontal_matrix_removed' => strpos($view, 'branch124-matrix-wrap') === false,
    'service_card_responsive' => strpos($css, '.branch125-service-grid') !== false
        && strpos($css, '@media(max-width:820px)') !== false,
    'branch_card_responsive' => strpos($css, '.branch125-branch-grid') !== false,
    'second_chart_rendered' => strpos($js, 'renderMonthlyImprovement') !== false,
    'browser_gate_service_cards' => strpos($js, 'serviceCards === 6') !== false,
    'browser_gate_branch_cards' => strpos($js, 'branchCards === 2') !== false,
    'no_initial_monthly_detail_charts' => strpos($js, 'visibleMonthlyCharts') !== false
        && strpos($js, 'expected: 0') !== false,
    'layout_nontechnical_description' => strpos($layout, 'Compare branch activity, movement, strengths and priorities at a glance.') !== false,
    'asset_cache_busted' => strpos($layout, '11.0.11.94-r45-p11-2-5-complete-management-view') !== 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_5']['oracle_contract_changed'] ?? true) === false,
    'query_count_unchanged' => ($version['r45_p11_2_5']['query_count_changed'] ?? true) === false,
    'oracle_writes_none' => ($version['r45_p11_2_5']['oracle_writes'] ?? '') === 'NONE',
];

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

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