<?php
$root = dirname(__DIR__);
$view = file_get_contents($root . '/app/Views/dashboard/index.php');
$css = file_get_contents($root . '/public/assets/css/dashboard-executive-command.css');
$version = json_decode(file_get_contents($root . '/DDB_VERSION.json'), true);

$tests = [];
$add = static function (string $name, bool $ok, string $detail = '') use (&$tests): void {
    $tests[] = ['name' => $name, 'status' => $ok ? 'PASS' : 'FAIL', 'detail' => $detail];
};
$add('version_release', ($version['release'] ?? '') === '11.0.11.53');
$add('phase_contract', ($version['phase'] ?? '') === 'H11.0-P11.1-R44');
$add('calculation_rules_unchanged', ($version['calculation_rules_changed'] ?? null) === false);
$add('no_database_changes', ($version['database_changes'] ?? '') === 'NONE');
$add('no_oracle_writes', ($version['oracle_write_operations'] ?? '') === 'NONE');
$add('pulse_section_preserved', strpos($view, 'command139-section--pulse') !== false);
$add('eight_operational_cards_preserved', strpos($view, '$operationalCards') !== false && substr_count($view, 'command139-pulse-card') >= 1);
$add('decision_section_preserved', strpos($view, 'command139-section--decision') !== false);
$add('four_panel_tones', strpos($view, 'panel-flow') !== false && strpos($view, 'panel-capacity') !== false && strpos($view, 'panel-finance') !== false && strpos($view, 'panel-clinical') !== false);
$add('preferred_four_column_desktop', strpos($css, 'grid-template-columns: repeat(4, minmax(0, 1fr));') !== false);
$add('pulse_compact_height', strpos($css, 'min-height: 92px;') !== false);
$add('panel_background_artifacts_removed', strpos($css, '.command139-panel::after,') !== false && strpos($css, 'display: none !important;') !== false);
$add('finance_sparkline_present', strpos($view, 'command144-finance-sparkline') !== false && strpos($css, '.command144-finance-sparkline') !== false);
$add('long_term_unit_wrap_preserved', strpos($css, 'overflow-wrap: anywhere') !== false);
$add('no_strange_pulse_title_mark', strpos($view, 'command139-title-with-mark') === false && strpos($view, '>⌁<') === false);
$add('action_pills_preserved', substr_count($view, 'command139-panel-link') >= 4);
$add('responsive_two_column_fallback', strpos($css, '@media (max-width: 1479px) and (min-width: 1181px)') !== false);
$add('mobile_rules_preserved', strpos($css, '@media (max-width: 640px)') !== false);

$pass = count(array_filter($tests, fn($t) => $t['status'] === 'PASS'));
$fail = count($tests) - $pass;
foreach ($tests as $t) {
    echo '[' . $t['status'] . '] ' . $t['name'] . ($t['detail'] !== '' ? ' - ' . $t['detail'] : '') . PHP_EOL;
}
echo "SUMMARY {$pass}/" . count($tests) . " PASS, {$fail} FAIL" . PHP_EOL;
exit($fail === 0 ? 0 : 1);
