<?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');
$controller = (string)file_get_contents($root . '/app/Controllers/BranchComparisonController.php');
$view = (string)file_get_contents($root . '/app/Views/branch_comparison/index.php');
$router = (string)file_get_contents($root . '/public/index.php');
$layout = (string)file_get_contents($root . '/app/Views/layouts/main.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');
$live = (string)file_get_contents($root . '/tools/branch-comparison-r45-p11-2-live-acceptance.php');

$checks = [
    'version' => ($version['version'] ?? '') === '11.0.11.89',
    'phase' => ($version['phase'] ?? '') === 'H11.0-P11.1-R45-P11.2',
    'route_import' => strpos($router, 'use App\\Controllers\\BranchComparisonController;') !== false,
    'route_dispatch' => strpos($router, "case 'branch-comparison':") !== false,
    'nav_live' => strpos($layout, "'page' => 'branch-comparison'") !== false
        && strpos($layout, "'icon' => '▥', 'live' => true") !== false,
    'page_title' => strpos($layout, "'branch-comparison' =>") !== false,
    'flow_safe_header' => strpos($layout, "'branch-comparison']") !== false,
    'css_loaded' => strpos($layout, 'branch-comparison.css?v=11.0.11.89-r45-p11-2-legacy-recovery') !== false,
    'js_loaded' => strpos($layout, 'branch-comparison.js?v=11.0.11.89-r45-p11-2-legacy-recovery') !== false,
    'six_exact_metrics' => preg_match_all(
        "/^\\s*'(outpatient|admissions|emergency|operations|laboratory|radiology)',\\s*$/m",
        $repo,
        $metricMatches
    ) === 6
        && count(array_unique($metricMatches[1] ?? [])) === 6
        && strpos($controller, "'outpatient' => [") !== false
        && strpos($controller, "'admissions' => [") !== false
        && strpos($controller, "'emergency' => [") !== false
        && strpos($controller, "'operations' => [") !== false
        && strpos($controller, "'laboratory' => [") !== false
        && strpos($controller, "'radiology' => [") !== false,
    'consolidated_single_period_sql' => strpos($repo, 'WITH') !== false
        && substr_count($repo, 'UNION ALL') === 5,
    'outpatient_exact_contract' => strpos($repo, 'v.classcode = 2') !== false
        && strpos($repo, "r.servstatus NOT IN ('C', 'F')") !== false
        && strpos($repo, 'r.sched_serial IS NOT NULL') !== false,
    'admission_exact_contract' => strpos($repo, 'NVL(e.baby_normal_bed, 1) = 1') !== false
        && strpos($repo, 'v.hosp_id = 1') !== false,
    'emergency_exact_contract' => strpos($repo, 'NVL(r.outpta_emergency_type, 0) = 1') !== false
        && strpos($repo, 'r.clinic_id IN (16, 297, 298)') !== false,
    'operation_exact_contract' => strpos($repo, 'JOIN {$operationSchedule} o') !== false
        && strpos($repo, "r.servstatus <> 'C'") !== false,
    'lab_rad_sysdate_contract' => strpos($repo, 'r.expecteddonedate <= SYSDATE') !== false
        && strpos($repo, 'r.machine_sch_from <= SYSDATE') !== false,
    'sargable_date_ranges' => substr_count($repo, ">= TO_DATE(:branch_compare_from, 'YYYY-MM-DD')") === 6
        && substr_count($repo, "< TO_DATE(:branch_compare_to, 'YYYY-MM-DD') + 1") === 6,
    'ready_branches_only' => strpos($controller, 'BranchReadiness::liveCodes()') !== false
        && strpos($repo, '!BranchReadiness::isLive($hospitalCode)') !== false,
    'fair_year_policy' => strpos($controller, 'FairYearComparisonPolicy') !== false
        && strpos($view, 'Fair comparison:') !== false,
    'no_silent_fallback' => strpos($controller, 'No partial rendering') !== false
        && strpos($view, 'NO SILENT FALLBACK') !== false,
    'six_visible_charts' => substr_count($view, 'branch112_chart_') >= 1
        && strpos($js, 'renderedCount === 6') !== false,
    'supporting_detail' => strpos($view, '<details class="branch112-details"') !== false,
    'browser_gate' => strpos($js, 'DDB_BRANCH_COMPARISON_R45_P11_2_BROWSER_GATE') !== false
        && strpos($js, 'horizontalOverflow') !== false,
    'responsive_css' => strpos($css, '@media(max-width:760px)') !== false,
    'live_acceptance' => strpos($live, 'DDB_BRANCH_COMPARISON_R45_P11_2_LIVE_ACCEPTANCE_V1') !== false,
    'oracle_writes_none' => ($version['r45_p11_2']['oracle_writes'] ?? '') === 'NONE',
];

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

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