<?php

declare(strict_types=1);

require_once dirname(__DIR__) . '/app/bootstrap.php';

use App\Repositories\LeadershipHuddleRepository;

$hospital = strtolower(trim((string)($_GET['hospital'] ?? 'njch'))) ?: 'njch';
header('Content-Type: text/plain; charset=utf-8');

echo "Executive Leadership Typography & Readability QA\n";
echo "Phase: 10.7.2 - Executive Leadership Typography & Readability Upgrade\n";
echo "Hospital: " . strtoupper($hospital) . "\n";
echo "Standard: Executive Leadership UI Standard v1.0\n";
echo "Write operations: NONE\n\n";

try {
    $report = (new LeadershipHuddleRepository($hospital))->report();
    $changes = is_array($report['meaningful_changes'] ?? null) ? $report['meaningful_changes'] : [];
    $agenda = is_array($report['decision_agenda'] ?? null) ? $report['decision_agenda'] : [];
    $commitmentRoot = is_array($report['open_commitments'] ?? null) ? $report['open_commitments'] : [];
    $commitments = is_array($commitmentRoot['rows'] ?? null) ? $commitmentRoot['rows'] : [];
    $meeting = is_array($report['meeting_status'] ?? null) ? $report['meeting_status'] : [];

    $root = dirname(__DIR__);
    $cssFile = __DIR__ . '/assets/css/leadership-huddle.css';
    $viewFile = $root . '/app/Views/leadership_huddle/index.php';
    $layoutFile = $root . '/app/Views/layouts/main.php';
    $repoFile = $root . '/app/Repositories/LeadershipHuddleRepository.php';
    $dbFile = $root . '/app/Core/Database.php';
    $reviewRepoFile = $root . '/app/Repositories/ExecutiveActionReviewRepository.php';
    $govRepoFile = $root . '/app/Repositories/ExecutiveActionGovernanceRepository.php';

    $css = is_file($cssFile) ? (string)file_get_contents($cssFile) : '';
    $view = is_file($viewFile) ? (string)file_get_contents($viewFile) : '';
    $layout = is_file($layoutFile) ? (string)file_get_contents($layoutFile) : '';
    $repo = is_file($repoFile) ? (string)file_get_contents($repoFile) : '';

    $passCount = 0;
    $failCount = 0;
    $pass = static function (bool $ok, string $label, string $detail = '') use (&$passCount, &$failCount): void {
        if ($ok) { $passCount++; } else { $failCount++; }
        echo ($ok ? '[PASS] ' : '[FAIL] ') . str_pad($label, 50) . ($detail !== '' ? ' | ' . $detail : '') . "\n";
    };

    $requiredCssMarkers = [
        'Executive Leadership UI Standard v1.0',
        '--exec-title:clamp(40px,3vw,44px)',
        '--exec-section:clamp(24px,1.7vw,28px)',
        '--exec-body:16px',
        '--exec-secondary:14px',
        '--exec-label:13px',
        '--exec-button:16px',
        '.huddle2-decision-grid p{margin-top:8px;font-size:16px;line-height:1.62',
        '.huddle2-agenda-title-row h4{font-size:21px',
        '.huddle2-agenda-footer span{min-height:36px;padding:8px 11px;font-size:14px',
        '.huddle2-tags span{padding:6px 9px;font-size:12px',
        '.huddle2-agenda-title-row>a{min-height:44px',
        'html[dir="rtl"] .huddle2-page{font-size:17px',
        'html[dir="rtl"] .huddle2-decision-grid p{font-size:17px',
    ];
    $cssMarkersReady = true;
    foreach ($requiredCssMarkers as $marker) {
        if (strpos($css, $marker) === false) { $cssMarkersReady = false; break; }
    }

    $glanceStructure = count($changes) <= 4
        && count($agenda) <= 4
        && array_key_exists('decision_count', $meeting)
        && strpos($view, 'huddle2-focus-strip') !== false
        && strpos($view, 'huddle2-hero-pulse') !== false;

    $lowDensity = count($changes) <= 4 && count($agenda) <= 4 && count($commitments) <= 5;

    $readOnly = strpos($repo, 'Database::execute') === false
        && stripos($repo, 'DELETE FROM') === false
        && stripos($repo, 'UPDATE DASH_') === false
        && stripos($repo, 'INSERT INTO') === false;

    $protectedFilesPresent = is_file($dbFile) && is_file($reviewRepoFile) && is_file($govRepoFile);

    echo "QA Gates:\n";
    $pass((string)($report['phase'] ?? '') === '10.7.2', 'report_phase', (string)($report['phase'] ?? '-'));
    $pass(strpos($css, 'Phase 10.7.2 - Executive Leadership Typography & Readability Upgrade') !== false, 'leadership_standard_phase_integrated', '10.7.2');
    $pass($cssMarkersReady, 'executive_typography_tokens_present', 'title + section + body + label + button');
    $pass(strpos($css, '--exec-body:16px') !== false, 'primary_body_minimum_16px', '16px');
    $pass(strpos($css, '--exec-secondary:14px') !== false, 'secondary_text_minimum_14px', '14px');
    $pass(strpos($css, '--exec-section:clamp(24px,1.7vw,28px)') !== false, 'section_titles_24_to_28px', '24-28px');
    $pass(strpos($css, '.huddle2-agenda-title-row h4{font-size:21px') !== false, 'action_titles_21px', '21px');
    $pass(strpos($css, '.huddle2-decision-grid p{margin-top:8px;font-size:16px') !== false, 'decision_text_minimum_16px', '16px');
    $pass(strpos($css, '.huddle2-agenda-footer span{min-height:36px;padding:8px 11px;font-size:14px') !== false, 'owner_due_text_minimum_14px', '14px');
    $pass(strpos($css, '.huddle2-tags span{padding:6px 9px;font-size:12px') !== false, 'status_badges_minimum_12px', '12px');
    $pass(strpos($css, '--exec-button:16px') !== false && strpos($css, 'min-height:48px') !== false, 'executive_buttons_readable', '16px + 48px target');
    $pass(strpos($css, 'html[dir="rtl"] .huddle2-page{font-size:17px') !== false, 'arabic_body_readability_plus_one', '17px');
    $pass(strpos($css, 'html[dir="rtl"] .huddle2-decision-grid p{font-size:17px') !== false, 'arabic_decision_text_readability', '17px');
    $pass($glanceStructure, 'three_to_five_second_glance_structure', 'hero + meeting status + max 4 changes + max 4 decisions');
    $pass($lowDensity, 'leadership_information_density', 'changes<=4, decisions<=4, commitments<=5');
    $pass(strpos($layout, 'phase10-7-2-executive-leadership-readability') !== false, 'css_cache_buster_updated', '10.7.2 asset version');
    $pass(strpos($layout, 'Phase 10.7.2') !== false, 'top_header_phase_updated', 'Executive Leadership Readability');
    $pass(strpos($view, 'Phase 10.7.2') !== false, 'visible_report_phase_updated', '10.7.2');
    $pass(strpos($css, '@media screen and (max-width:680px)') !== false, 'mobile_readability_rules_present', '16px body floor');
    $pass(strpos($css, '@media print') !== false, 'print_layout_preserved', 'compact executive print');
    $pass($readOnly, 'huddle_repository_remains_read_only', 'no insert/update/delete path');
    $pass($protectedFilesPresent, 'oracle_review_governance_files_present', 'protected integration files available');

    echo "\nExecutive Readability Summary:\n";
    echo "primary_body          | 16px minimum\n";
    echo "arabic_body           | 17px\n";
    echo "section_titles        | 24-28px\n";
    echo "action_titles         | 21px\n";
    echo "decision_text         | 16px EN / 17px AR\n";
    echo "owner_due             | 14px minimum\n";
    echo "status_badges         | 12px minimum\n";
    echo "buttons               | 16px, 48px primary target\n";
    echo "meaningful_changes    | " . count($changes) . "\n";
    echo "decision_agenda       | " . count($agenda) . "\n";
    echo "visible_commitments   | " . count($commitments) . "\n";
    echo "write_operations      | 0\n";

    echo "\nLeadership comprehension model:\n";
    echo "3-5 seconds            | status + material change + decisions needed\n";
    echo "30 seconds             | why now + decision required + owner + due\n";
    echo "drill-down              | Action Review / Governance dashboards\n";

    echo "\nReport URL:\n";
    echo "index.php?page=leadership-huddle&lang=en\n";
    echo "\nQA result: " . ($failCount === 0 ? 'PASS' : 'FAIL') . "\n";
    echo "PASS gates: " . $passCount . "\n";
    echo "FAIL gates: " . $failCount . "\n";
    echo "QA is read-only. No review, governance, Oracle or audit row is modified.\n";
} catch (Throwable $e) {
    http_response_code(500);
    echo "ERROR: " . $e->getMessage() . "\n";
}
