<?php
require dirname(__DIR__) . '/app/bootstrap.php';

$root = dirname(__DIR__);
$layoutFile = $root . '/app/Views/layouts/main.php';
$cssFile = __DIR__ . '/assets/css/action-center.css';
$repoFile = $root . '/app/Repositories/ExecutiveActionReviewRepository.php';
$dbFile = $root . '/app/Core/Database.php';

$layout = is_file($layoutFile) ? (string)file_get_contents($layoutFile) : '';
$css = is_file($cssFile) ? (string)file_get_contents($cssFile) : '';

$repoExpected = '5145e0f7fa342233d6cde0357f4734ebc5db96f0ef64ec0176bb3020ee6110dd';
$dbExpected = '42a55158ec93d7abec3e8ddaa726f14fa630011718066e5f8e853dd41f15c177';
$repoHash = is_file($repoFile) ? hash_file('sha256', $repoFile) : '';
$dbHash = is_file($dbFile) ? hash_file('sha256', $dbFile) : '';

$marker = 'Phase 10.6.4.2.1 — Sidebar Structure & Left Offset Hotfix';
$markerPos = strpos($css, $marker);
$hotfixCss = $markerPos === false ? '' : substr($css, $markerPos);

$checks = [
    'css_cache_buster_updated' => strpos($layout, 'phase10-6-4-2-1-sidebar-structure-left-offset-hotfix') !== false,
    'root_cause_hotfix_present' => $markerPos !== false,
    'desktop_rule_has_no_max_width_cap' => strpos($hotfixCss, '@media (min-width: 981px)') !== false && strpos($hotfixCss, 'max-width: 1900px') === false,
    'compact_rail_width_atomic' => strpos($hotfixCss, '--ac-rail-width: 88px;') !== false
        && strpos($hotfixCss, 'min-width: var(--ac-rail-width) !important;') !== false
        && strpos($hotfixCss, 'max-width: var(--ac-rail-width) !important;') !== false,
    'brand_text_forced_hidden' => strpos($hotfixCss, '.elite-brand > span:not(.elite-brand-mark)') !== false
        && strpos($hotfixCss, 'visibility: hidden !important;') !== false,
    'nav_labels_forced_hidden' => strpos($hotfixCss, '.elite-nav a > b') !== false
        && strpos($hotfixCss, '.elite-nav a > em') !== false,
    'nav_items_cannot_spill' => strpos($hotfixCss, 'font-size: 0 !important;') !== false
        && strpos($hotfixCss, 'overflow: hidden !important;') !== false
        && strpos($hotfixCss, 'max-width: 56px !important;') !== false,
    'sidebar_scrollbar_forced_hidden' => strpos($hotfixCss, 'scrollbar-width: none !important;') !== false
        && strpos($hotfixCss, '.elite-sidebar *::-webkit-scrollbar') !== false,
    'ltr_sidebar_and_main_aligned' => strpos($hotfixCss, 'html[dir="ltr"] body.page-action-center .elite-sidebar') !== false
        && strpos($hotfixCss, 'margin-left: var(--ac-rail-width) !important;') !== false,
    'rtl_sidebar_and_main_aligned' => strpos($hotfixCss, 'html[dir="rtl"] body.page-action-center .elite-sidebar') !== false
        && strpos($hotfixCss, 'margin-right: var(--ac-rail-width) !important;') !== false,
    'mobile_drawer_preserved' => strpos($hotfixCss, '@media (max-width: 980px)') !== false
        && strpos($hotfixCss, '--ac-rail-width: 0px;') !== false,
    'oracle_repository_unchanged' => $repoHash !== '' && hash_equals($repoExpected, $repoHash),
    'database_core_unchanged' => $dbHash !== '' && hash_equals($dbExpected, $dbHash),
];

$allPass = !in_array(false, $checks, true);
http_response_code($allPass ? 200 : 500);
header('Content-Type: text/plain; charset=UTF-8');

echo "Executive Action Review Sidebar Structure QA\n";
echo "Phase: 10.6.4.2.1 — Sidebar Structure & Left Offset Hotfix\n\n";
echo "Root cause:\n";
echo "Compact rail width was active on all desktop sizes, but label hiding stopped above a 1900px CSS viewport. Browser zoom/wide viewports could therefore show clipped HMH/navigation text inside the narrow rail.\n\n";
echo "QA Gates:\n";
foreach ($checks as $name => $pass) {
    echo sprintf("[%s] %-42s | %s\n", $pass ? 'PASS' : 'FAIL', $name, $pass ? 'YES' : 'NO');
}
echo "\nResult: " . ($allPass ? 'PASS' : 'FAIL') . "\n";
echo "Scope: sidebar structure, overflow containment, and main-content offset only. Oracle, audit history, save logic, and transition rules are unchanged.\n";
