<?php
declare(strict_types=1);

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

$runtimeFiles = $view . "\n" . $controller . "\n" . $layout;

$checks = [
    'version' => ($version['version'] ?? '') === '11.0.11.99',
    'phase' => ($version['phase'] ?? '') === 'H11.0-P11.1-R45-P11.3.1',
    'match_removed' => preg_match('/\bmatch\s*\(/', $runtimeFiles) !== 1,
    'switch_replacement_present' => strpos($view, 'switch ($state)') !== false,
    'state_label_switch' => strpos($view, "case 'both_improved':") !== false
        && strpos($view, "case 'both_declined':") !== false,
    'state_class_switch' => substr_count($view, 'switch ($state)') === 2,
    'nullsafe_operator_absent' => strpos($runtimeFiles, '?->') === false,
    'php_attributes_absent' => preg_match('/#\s*\[/', $runtimeFiles) !== 1,
    'arrow_functions_absent' => preg_match('/\bfn\s*\(/', $runtimeFiles) !== 1,
    'service_first_preserved' => strpos($controller, 'buildServiceFirstAnalysis(') !== false,
    'mixed_totals_disabled' => strpos($controller, "'mixed_service_totals_used' => false") !== false,
    'service_tabs_preserved' => strpos($view, 'data-branch113-service-tab') !== false,
    'six_service_cards_preserved' => strpos($view, 'branch113-service-grid') !== false,
    'latest_month_preserved' => strpos($view, 'LATEST COMPLETED MONTH') !== false,
    'asset_version_updated' => strpos($layout, '11.0.11.99-r45-p11-3-1-php7-hotfix') !== 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,
    'functional_logic_unchanged' => ($version['r45_p11_3_1']['functional_logic_changed'] ?? true) === false,
    'oracle_contract_unchanged' => ($version['r45_p11_3_1']['oracle_contract_changed'] ?? true) === false,
    'query_count_unchanged' => ($version['r45_p11_3_1']['query_count_changed'] ?? true) === false,
    'oracle_writes_none' => ($version['r45_p11_3_1']['oracle_writes'] ?? '') === 'NONE',
];

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

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