<?php
declare(strict_types=1);
$root = dirname(__DIR__);
$v = json_decode((string)file_get_contents($root . '/DDB_VERSION.json'), true);
$c = (string)file_get_contents($root . '/app/Controllers/SpecialtyFinanceController.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 = $c . "\n" . $view . "\n" . $layout;
$checks = [
 'version' => ($v['version'] ?? '') === '11.0.12.4',
 'phase' => ($v['phase'] ?? '') === 'H11.0-P12.1.2',
 'all_config' => strpos($c, "'id' => '__all__'") !== false,
 'all_default' => strpos($c, "'default_specialty_id' => '__all__'") !== false,
 'aggregate_op' => strpos($c, '$aggregateCurrentOutpatient') !== false,
 'aggregate_ip' => strpos($c, '$aggregateCurrentAdmissions') !== false,
 'all_option' => strpos($view, 'value="__all__"') !== false,
 'scope_label' => strpos($view, "Scope', 'النطاق") !== false,
 'five_rows' => strpos($view, '$activityIndex >= 5') !== false,
 'view_all' => strpos($view, 'data-sf121-view-all') !== false,
 'cards' => substr_count($view, 'data-sf121-signal-metric=') === 4,
 'cells' => substr_count($view, 'data-sf121-metric-cell=') === 6,
 'correct_signal_css' => strpos($css, '.sf121-activity[data-sf121-active-metric="outpatient"]') !== false,
 'stacked' => strpos($css, '.sf121-workspace{' . "\n" . '    grid-template-columns:1fr;') !== false,
 'gate' => strpos($js, 'allSpecialtiesReady') !== false,
 'cache' => strpos($layout, '11.0.12.4-p12-1-2-all-specialties') !== false,
 'php7_match_absent' => preg_match('/\bmatch\s*\(/', $runtime) !== 1,
 'php7_nullsafe_absent' => strpos($runtime, '?->') === false,
 'env_summary' => preg_match('/^USE_REVENUE_SUMMARY=1$/m', $env) === 1,
 'env_age' => preg_match('/^REVENUE_SUMMARY_MAX_AGE_HOURS=30$/m', $env) === 1,
 'oracle_writes' => ($v['r45_p12_1_2']['oracle_writes'] ?? '') === 'NONE',
];
$failed=[];
foreach($checks as $k=>$ok){if(!$ok)$failed[]=$k;}
echo json_encode(['schema'=>'R45_P12_1_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);
