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

$checks = [
    'version' => ($version['version'] ?? '') === '11.0.11.66',
    'phase' => ($version['phase'] ?? '') === 'H11.0-P11.1-R45-P4.1',
    'previous_card_head_restored' => strpos($view, 'eri-r45p1-controls-head') !== false,
    'previous_active_comparison_restored' => strpos($view, 'eri-r45p1-active-comparison') !== false,
    'previous_fair_notice_restored' => strpos($view, 'eri-r45p1-fair-notice') !== false,
    'previous_filter_grid_restored' => strpos($view, 'eri-r45p1-filter-grid') !== false,
    'p4_restructure_removed' => strpos($view, 'eri-r45p4-head') === false
        && strpos($view, 'eri-r45p4-policy-note') === false,
    'date_parameter_contract_preserved' => strpos($view, 'name="from_date"') !== false
        && strpos($view, 'name="to_date"') !== false,
    'outer_border_added' => strpos($css, '.eri-r45p1-controls {') !== false
        && strpos($css, 'border: 1px solid #d8e2ef') !== false,
    'inner_padding_added' => strpos($css, 'padding: 22px 24px 20px') !== false,
    'field_gap_added' => strpos($css, '.eri-r45p1-filter-grid {') !== false
        && strpos($css, 'gap: 14px') !== false,
    'mobile_spacing_contract' => strpos($css, '@media (max-width: 720px)') !== false,
    'no_oracle_change' => ($version['r45_p4_1']['oracle_writes'] ?? '') === 'NONE',
];

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

$result = [
    'schema' => 'R45_P4_1_QA',
    'checks' => $checks,
    'passed' => count($checks) - count($failed),
    'failed' => $failed,
];
echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES), PHP_EOL;
exit(count($failed) === 0 ? 0 : 1);
