<?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/SpecialtyFinanceController.php');
$repo = (string)file_get_contents($root . '/app/Repositories/SpecialtyFinanceRepository.php');
$view = (string)file_get_contents($root . '/app/Views/specialty_finance/index.php');
$css = (string)file_get_contents($root . '/public/assets/css/specialty-finance-executive-parity.css');
$js = (string)file_get_contents($root . '/public/assets/js/specialty-finance-activity.js');
$layout = (string)file_get_contents($root . '/app/Views/layouts/main.php');
$env = (string)file_get_contents($root . '/.env');

$runtime = $controller . "\n" . $view . "\n" . $js;

$checks = [
    'version' => ($version['version'] ?? '') === '11.0.12.2',
    'phase' => ($version['phase'] ?? '') === 'H11.0-P12.1',
    'live_contract_pass_recorded' => (
        $version['r45_p12_1']['live_contract_status'] ?? ''
    ) === 'PASS',
    'period_contract_called' => strpos(
        $controller,
        'specialtyActivityComparison('
    ) !== false,
    'monthly_contract_called' => strpos(
        $controller,
        'specialtyActivityMonthlyComparison('
    ) !== false,
    'activity_workspace_builder' => strpos(
        $controller,
        'buildActivityWorkspace('
    ) !== false,
    'outpatient_card' => strpos(
        $controller,
        "'outpatient' => \$this->card("
    ) !== false,
    'admissions_card' => strpos(
        $controller,
        "'admissions' => \$this->card("
    ) !== false,
    'activity_table_rows' => strpos(
        $controller,
        "'rows' => \$tableRows"
    ) !== false,
    'monthly_chart_configs' => strpos(
        $controller,
        "'chart_configs' => \$chartConfigs"
    ) !== false,
    'outpatient_ui' => strpos($view, 'Outpatient Visits') !== false
        && strpos($view, 'زيارات العيادات الخارجية') !== false,
    'admissions_ui' => strpos($view, 'Admissions') !== false
        && strpos($view, 'حالات الدخول') !== false,
    'complete_activity_table' => strpos(
        $view,
        'data-sf121-activity-row'
    ) !== false,
    'current_compare_columns' => strpos(
        $view,
        'Outpatient current'
    ) !== false
        && strpos($view, 'Outpatient comparison') !== false
        && strpos($view, 'Admissions current') !== false
        && strpos($view, 'Admissions comparison') !== false,
    'specialty_selector' => strpos(
        $view,
        'data-sf121-specialty-select'
    ) !== false,
    'metric_tabs' => substr_count($view, 'data-sf121-metric=') === 2,
    'single_activity_chart' => substr_count(
        $view,
        'id="sf121_activity_chart"'
    ) === 1,
    'chart_json_config' => strpos(
        $view,
        'id="sf121_activity_chart_config"'
    ) !== false,
    'no_revenue_per_visit_claim' => stripos(
        $view,
        'revenue per visit'
    ) === false
        && strpos($controller, '/ $currentOutpatient') === false,
    'no_revenue_per_admission_claim' => stripos(
        $view,
        'revenue per admission'
    ) === false
        && strpos($controller, '/ $currentAdmissions') === false,
    'explicit_no_ratio_note' => strpos(
        $view,
        'No revenue-per-visit or revenue-per-admission value is inferred'
    ) !== false,
    'financial_charts_preserved' => strpos(
        $view,
        "foreach (['moh', 'drug', 'net'] as \$key)"
    ) !== false,
    'financial_matrix_preserved' => strpos(
        $view,
        'Specialty Performance Overview'
    ) !== false,
    'activity_js_render' => strpos($js, 'renderBarCompareChart') !== false,
    'activity_js_selector' => strpos($js, 'activeSpecialty') !== false,
    'activity_js_metric' => strpos($js, 'activeMetric') !== false,
    'browser_gate' => strpos(
        $js,
        'DDB_SPECIALTY_FINANCE_P12_1_BROWSER_GATE'
    ) !== false,
    'responsive_css' => strpos(
        $css,
        'data-specialty-activity-recovery="P12.1"'
    ) !== false
        && strpos($css, '@media(max-width:720px)') !== false,
    'asset_cache_busted' => strpos(
        $layout,
        '11.0.12.2-p12-1-activity-integration'
    ) !== false,
    'layout_description_updated' => strpos(
        $layout,
        'Revenue, admissions and outpatient activity by specialty.'
    ) !== false,
    'repository_contract_preserved' => strpos(
        $repo,
        'SPECIALTY + DOCTOR deduplication before specialty totals'
    ) !== false,
    'repository_query_unchanged' => (
        $version['r45_p12_1']['repository_query_changed'] ?? true
    ) === false,
    'php7_match_absent' => preg_match('/\bmatch\s*\(/', $runtime) !== 1,
    'php7_nullsafe_absent' => strpos($runtime, '?->') === false,
    'php7_attributes_absent' => preg_match('/#\s*\[/', $runtime) !== 1,
    'php7_arrow_functions_absent' => preg_match('/\bfn\s*\(/', $runtime) !== 1,
    '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_writes_none' => (
        $version['r45_p12_1']['oracle_writes'] ?? ''
    ) === 'NONE',
];

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

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