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

$root = dirname(__DIR__);
$indexFile = $root . '/app/Views/action_center/index.php';
$reviewFile = $root . '/app/Views/action_center/review.php';
$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';

$index = is_file($indexFile) ? (string)file_get_contents($indexFile) : '';
$review = is_file($reviewFile) ? (string)file_get_contents($reviewFile) : '';
$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) : '';

$mainColumnPos = strpos($review, 'class="review-main-column"');
$auditPos = strpos($review, 'class="review-audit-timeline"');
$sidePos = strpos($review, 'class="review-side-card"');

$checks = [
    'css_cache_buster_updated' => strpos($layout, 'phase10-6-4-2-layout-stability-whitespace-hotfix') !== false,
    'desktop_shell_width_stable' => strpos($css, 'width: auto !important;') !== false && strpos($css, 'box-sizing: border-box !important;') !== false,
    'sidebar_scrollbar_hidden' => strpos($css, '.elite-sidebar::-webkit-scrollbar') !== false && strpos($css, 'scrollbar-width: none;') !== false,
    'context_sticky_gap_removed' => strpos($css, 'top: 12px !important;') !== false && strpos($css, 'overflow: hidden !important;') !== false,
    'context_cards_fill_viewport' => strpos($css, 'grid-template-rows: repeat(6, minmax(66px, 1fr));') !== false,
    'review_main_column_present' => $mainColumnPos !== false,
    'audit_follows_main_content' => $mainColumnPos !== false && $auditPos !== false && $sidePos !== false && $mainColumnPos < $auditPos && $auditPos < $sidePos,
    'latest_activity_fallback_clean' => strpos($index, "Status changed to") !== false && strpos($index, '$lastActivityHasTransition') !== 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 Layout Stability QA
";
echo "Phase: 10.6.4.2 — Layout Stability & Whitespace Hotfix

";
echo "QA Gates:
";
foreach ($checks as $name => $pass) {
    echo sprintf("[%s] %-40s | %s
", $pass ? 'PASS' : 'FAIL', $name, $pass ? 'YES' : 'NO');
}
echo "
Result: " . ($allPass ? 'PASS' : 'FAIL') . "
";
echo "Scope: layout and display copy only; Oracle persistence, audit tables, save logic, and transition rules remain unchanged.
";
