<?php
$root = dirname(__DIR__);
$version = json_decode(file_get_contents($root . '/DDB_VERSION.json'), true);
$controller = file_get_contents($root . '/app/Controllers/OperationsController.php');
$view = file_get_contents($root . '/app/Views/operations/module.php');
$layout = file_get_contents($root . '/app/Views/layouts/main.php');
$coreCss = file_get_contents($root . '/public/assets/css/executive-ui-core.css');
$opsCss = file_get_contents($root . '/public/assets/css/operations-executive-redesign.css');
$adapter = file_get_contents($root . '/public/assets/js/ddb-echarts-adapter.js');
$opsJs = file_get_contents($root . '/public/assets/js/operations-comparison.js');
$live = file_get_contents($root . '/tools/operations-r45-p9-1-live-acceptance.php');

$checks = [
    'version' => ($version['version'] ?? '') === '11.0.11.83',
    'phase' => ($version['phase'] ?? '') === 'H11.0-P11.1-R45-P9.1',
    'ranking_current_first' => strpos($controller, "\$currentOrder = ((float)\$b['current_value']) <=> ((float)\$a['current_value'])") !== false,
    'max_two_year_sort_removed' => strpos($controller, 'max((float)$a[\'current_value\'], (float)$a[\'compare_value\'])') === false,
    'zero_current_semantics' => strpos($controller, "\$row['zero_current_loss']") !== false,
    'current_leader_filters' => strpos($view, '$currentSpecialtyLeaders') !== false
        && strpos($view, '$currentDoctorLeaders') !== false,
    'top_lists_exclude_zero_current' => strpos($view, "return (float)(\$row['current_value'] ?? 0) > 0.000001") !== false,
    'independent_loss_signal' => strpos($view, '$largestActivityLoss') !== false
        && strpos($view, "No current-period activity") !== false,
    'ranking_basis_explained' => strpos($view, 'Ranked by current-period volume') !== false,
    'period_line_no_wrap' => strpos($opsCss, 'white-space: nowrap') !== false
        && strpos($opsCss, 'minmax(300px, .72fr)') !== false,
    'flow_safe_header_class' => strpos($layout, "elite-topbar--flow-safe") !== false,
    'shared_flow_safe_css' => strpos($coreCss, '.elite-topbar.elite-topbar--flow-safe') !== false
        && strpos($coreCss, 'position: relative !important') !== false,
    'stable_hover_payload' => substr_count($view, "'stableHover' => true") === 3,
    'stable_hover_adapter' => strpos($adapter, 'var stableHover = payload.stableHover === true') !== false
        && strpos($adapter, 'disabled: true') !== false,
    'browser_header_gate' => strpos($opsJs, 'data-operations-r45-p9-1-browser-gate') !== false
        && strpos($opsJs, "topbarPosition !== 'sticky'") !== false,
    'live_ranking_acceptance' => strpos($live, 'DDB_OPERATIONS_R45_P9_1_LIVE_ACCEPTANCE_V1') !== false,
    'oracle_writes_none' => ($version['r45_p9_1']['oracle_writes'] ?? '') === 'NONE',
];

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

echo json_encode([
    'schema' => 'R45_P9_1_QA',
    'checks' => $checks,
    'passed' => count($checks) - count($failed),
    'failed' => $failed,
], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES), PHP_EOL;

exit(count($failed) === 0 ? 0 : 1);
