<?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/specialty_finance/index.php');
$layout = (string)file_get_contents($root . '/app/Views/layouts/main.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');
$repo = (string)file_get_contents($root . '/app/Repositories/SpecialtyFinanceRepository.php');
$controller = (string)file_get_contents($root . '/app/Controllers/SpecialtyFinanceController.php');
$env = (string)file_get_contents($root . '/.env');

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

$checks = [
    'version' => ($version['version'] ?? '') === '11.0.12.3',
    'phase' => ($version['phase'] ?? '') === 'H11.0-P12.1.1',
    'flow_safe_topbar' => strpos(
        $layout,
        "'branch-comparison', 'specialty-finance'"
    ) !== false,
    'professional_heading' => strpos(
        $view,
        "Patient Activity by Specialty"
    ) !== false,
    'restored_heading_removed' => strpos(
        $view,
        'Admissions and outpatient visits restored'
    ) === false,
    'technical_interpretation_removed' => strpos(
        $view,
        'Important interpretation'
    ) === false,
    'single_metric_toolbar' => strpos(
        $view,
        'sf121-view-toolbar'
    ) !== false,
    'metric_tabs_once' => substr_count(
        $view,
        'data-sf121-metric='
    ) === 2,
    'metric_controls_signals' => substr_count(
        $view,
        'data-sf121-signal-metric='
    ) === 4,
    'four_column_table' => strpos(
        $view,
        "<?= e(\$currentPeriodLabel) ?>"
    ) !== false
        && strpos($view, "<?= e(\$comparePeriodLabel) ?>") !== false
        && strpos($view, "Outpatient current") === false
        && strpos($view, "Admissions current") === false,
    'metric_cells' => substr_count(
        $view,
        'data-sf121-metric-cell='
    ) === 6,
    'six_rows_initially' => strpos(
        $view,
        '$activityIndex >= 6'
    ) !== false,
    'view_top_six_label' => strpos(
        $view,
        'Show first 6 specialties'
    ) !== false,
    'table_title_dynamic' => strpos(
        $view,
        'id="sf121_activity_table_title"'
    ) !== false
        && strpos($js, 'tableTitle.textContent') !== false,
    'global_metric_state' => strpos(
        $view,
        'data-sf121-active-metric='
    ) !== false
        && strpos($js, "setAttribute('data-sf121-active-metric'") !== false,
    'chart_preserved' => substr_count(
        $view,
        'id="sf121_activity_chart"'
    ) === 1,
    'specialty_selector_preserved' => strpos(
        $view,
        'data-sf121-specialty-select'
    ) !== false,
    'financial_sections_preserved' => strpos(
        $view,
        "foreach (['moh', 'drug', 'net'] as \$key)"
    ) !== false
        && strpos($view, 'Specialty Performance Overview') !== false,
    'no_desktop_table_min_width' => strpos(
        $css,
        '.sf121-table{' . "\n" . '    min-width:0;'
    ) !== false,
    'metric_cell_css' => strpos(
        $css,
        '[data-sf121-metric-cell]'
    ) !== false,
    'two_signal_grid' => strpos(
        $css,
        '.sf121-signal-grid{' . "\n" . '    grid-template-columns:repeat(2'
    ) !== false,
    'browser_gate_updated' => strpos(
        $js,
        'visibleMetricCells'
    ) !== false
        && strpos($js, 'visibleSignals') !== false,
    'asset_cache_busted' => strpos(
        $layout,
        '11.0.12.3-p12-1-1-layout-refinement'
    ) !== false,
    'repository_query_unchanged' => (
        $version['r45_p12_1_1']['repository_query_changed'] ?? true
    ) === false,
    'controller_logic_unchanged' => (
        $version['r45_p12_1_1']['controller_data_logic_changed'] ?? true
    ) === false,
    'activity_contract_preserved' => strpos(
        $repo,
        'specialtyActivityComparison('
    ) !== false
        && strpos($controller, 'buildActivityWorkspace(') !== 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_1']['oracle_writes'] ?? ''
    ) === 'NONE',
];

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

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