<?php

declare(strict_types=1);

$root = dirname(__DIR__);
$viewPath = $root . '/app/Views/dashboard/index.php';
$cssPath = $root . '/public/assets/css/dashboard-executive-command.css';
$versionPath = $root . '/DDB_VERSION.json';

$view = (string)@file_get_contents($viewPath);
$css = (string)@file_get_contents($cssPath);
$version = json_decode((string)@file_get_contents($versionPath), true);

$gates = [];
$add = static function (string $name, bool $pass, string $detail = '') use (&$gates): void {
    $gates[] = ['name' => $name, 'status' => $pass ? 'PASS' : 'FAIL', 'detail' => $detail];
};

$heroStart = strpos($view, '<section class="command139-hero"');
$heroEnd = strpos($view, '<section class="command139-decision-lens"');
$hero = ($heroStart !== false && $heroEnd !== false && $heroEnd > $heroStart) ? substr($view, $heroStart, $heroEnd - $heroStart) : '';

$add('R42.2 version contract', ($version['release'] ?? '') === '11.0.11.51' && ($version['phase'] ?? '') === 'H11.0-P11.1-R42.2');
$add('R42.2 decision metadata exists', isset($version['r42_2_executive_hero_today_revenue_removal']));
$add('No Oracle schema changes', ($version['database_changes'] ?? '') === 'NONE');
$add('No Oracle writes by patch', ($version['oracle_write_operations'] ?? '') === 'NONE');
$add('No calculation rules changed', ($version['calculation_rules_changed'] ?? true) === false);
$add('Executive hero section found', $hero !== '');
$add('Today revenue hero card removed', !str_contains($hero, "Today revenue") && !str_contains($hero, 'إيراد اليوم') && !str_contains($hero, 'is-revenue'));
$add('Dashboard view no longer reads todayRevenue for hero', !str_contains($view, '$todayRevenue'));
$add('Hospital status hero card preserved', str_contains($hero, "Hospital status") && str_contains($hero, 'is-status'));
$add('Current occupancy hero card preserved', str_contains($hero, "Current occupancy") && str_contains($hero, 'is-capacity'));
$add('Emergency pressure hero card preserved', str_contains($hero, "Emergency pressure") && str_contains($hero, 'is-pressure'));
$add('Exactly three executive hero KPI cards remain', substr_count($hero, '<article class="command139-kpi ') === 3);
$add('Desktop hero KPI grid uses three equal columns', str_contains($css, '.command139-kpi-grid {') && str_contains($css, 'grid-template-columns: repeat(3, minmax(0, 1fr));'));
$add('Tablet two-column responsiveness preserved', str_contains($css, '.command139-kpi-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }'));
$add('Mobile one-column responsiveness preserved', preg_match('/\.command139-kpi-grid,\s*\n\s*body\.page-dashboard \.command139-pulse-grid/', $css) === 1);
$add('Dead revenue-specific hero styling removed', !str_contains($css, '.command139-kpi.is-revenue'));
$add('Other revenue pages explicitly unchanged by contract', (($version['r42_2_executive_hero_today_revenue_removal']['other_revenue_pages_changed'] ?? true) === false));
$add('Official baseline is not pre-claimed', (($version['r42_2_executive_hero_today_revenue_removal']['official_baseline_claimed'] ?? true) === false));

$pass = count(array_filter($gates, static fn(array $g): bool => $g['status'] === 'PASS'));
$fail = count($gates) - $pass;
$out = [
    'patch' => 'H11.0-P11.1-R42.2',
    'target' => '11.0.11.51',
    'status' => $fail === 0 ? 'PASS' : 'FAIL',
    'pass_gates' => $pass,
    'fail_gates' => $fail,
    'total_gates' => count($gates),
    'decision' => 'REMOVE_TODAY_REVENUE_FROM_EXECUTIVE_HERO',
    'reason' => 'The displayed batch-derived value is not sufficiently precise to present as actual hospital income on the most important leadership page.',
    'remaining_hero_cards' => ['Hospital status', 'Current occupancy', 'Emergency pressure'],
    'gates' => $gates,
    'truth_boundary' => 'UI removal PASS is not browser acceptance. Inherited Live Oracle 8/8 remains independently required for R42/R42.1 KPI parity.',
];

echo json_encode($out, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), PHP_EOL;
exit($fail === 0 ? 0 : 1);
