<?php
$root = dirname(__DIR__);
$cssPath = $root . '/public/assets/css/dashboard-executive-command.css';
$mainPath = $root . '/app/Views/layouts/main.php';
$versionPath = $root . '/DDB_VERSION.json';

$css = (string)@file_get_contents($cssPath);
$main = (string)@file_get_contents($mainPath);
$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];
};

$add('R41 version contract', ($version['release'] ?? '') === '11.0.11.48' && ($version['phase'] ?? '') === 'H11.0-P11.1-R41');
$add('R41 CSS marker', str_contains($css, 'H11.0-P11.1-R41'));
$add('Dashboard cache key R41', str_contains($main, 'r41-leadership-readability-typography-closure'));

preg_match_all('/font-size\s*:\s*(\d+)px/i', $css, $matches);
$pixelSizes = array_map('intval', $matches[1] ?? []);
$belowFloor = array_values(array_filter($pixelSizes, static fn(int $v): bool => $v < 14));
$add('Explicit dashboard font floor >= 14px', count($belowFloor) === 0, 'below_floor=' . json_encode($belowFloor));

$add('Executive shell base 16px', str_contains($css, '.command139-shell {') && str_contains($css, 'font-size: 16px;'));
$add('KPI label 14px', str_contains($css, '.command139-kpi small') && str_contains($css, 'font-size: 14px;'));
$add('KPI description 16px', str_contains($css, '.command139-kpi p') && str_contains($css, 'font-size: 16px;'));
$add('Section description 16px', str_contains($css, '.command139-section-head p') && str_contains($css, 'font-size: 16px;'));
$add('Panel label 14px', str_contains($css, '.command139-panel-head small') && str_contains($css, 'font-size: 14px;'));
$add('Action title 16px', str_contains($css, '.command139-action-grid strong') && str_contains($css, 'font-size: 16px;'));
$add('Action description 14px', str_contains($css, '.command139-action-grid p') && str_contains($css, 'font-size: 14px;'));
$add('Capacity unit names no ellipsis', str_contains($css, '.command139-unit-list span') && str_contains($css, 'text-overflow: clip;') && str_contains($css, 'white-space: normal;'));
$add('Ranking labels no ellipsis', str_contains($css, '.command139-ranking li > b') && str_contains($css, 'overflow-wrap: anywhere;'));
$add('Arabic body readability lift', str_contains($css, 'html[dir="rtl"] body.page-dashboard') && str_contains($css, 'font-size: 17px;'));
$add('Mobile single-column contract preserved', str_contains($css, '@media (max-width: 640px)') && str_contains($css, 'grid-template-columns: 1fr;'));
$add('Print suppression contract preserved', str_contains($css, '@media print') && str_contains($css, '.elite-sidebar'));
$add('No Oracle writes / no DB changes / no calculation changes',
    ($version['oracle_write_operations'] ?? '') === 'NONE' &&
    ($version['database_changes'] ?? '') === 'NONE' &&
    ($version['calculation_rules_changed'] ?? 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-R41',
    'target' => '11.0.11.48',
    'status' => $fail === 0 ? 'PASS' : 'FAIL',
    'pass_gates' => $pass,
    'fail_gates' => $fail,
    'total_gates' => count($gates),
    'gates' => $gates,
    'truth_boundary' => 'Static readability gates are not Live Browser acceptance. Missing Evidence is not PASS.',
];
echo json_encode($out, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), PHP_EOL;
exit($fail === 0 ? 0 : 1);
