<?php

declare(strict_types=1);

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

$root = dirname(__DIR__);
$checks = [];

$add = static function (
    string $name,
    bool $ok,
    string $detail = ''
) use (&$checks): void {
    $checks[] = [
        'name' => $name,
        'status' => $ok ? 'PASS' : 'FAIL',
        'detail' => $detail,
    ];
};

$layout = is_file($root . '/app/Views/layouts/main.php')
    ? (string)file_get_contents($root . '/app/Views/layouts/main.php')
    : '';

$components = is_file($root . '/app/Views/components/executive/components.php')
    ? (string)file_get_contents($root . '/app/Views/components/executive/components.php')
    : '';

$coreCss = is_file($root . '/public/assets/css/executive-ui-core.css')
    ? (string)file_get_contents($root . '/public/assets/css/executive-ui-core.css')
    : '';

$module = is_file($root . '/app/Views/operations/module.php')
    ? (string)file_get_contents($root . '/app/Views/operations/module.php')
    : '';

$i18n = is_file($root . '/config/i18n.php')
    ? (string)file_get_contents($root . '/config/i18n.php')
    : '';

$version = [];
if (is_file($root . '/DDB_VERSION.json')) {
    $decoded = json_decode(
        (string)file_get_contents($root . '/DDB_VERSION.json'),
        true
    );
    $version = is_array($decoded) ? $decoded : [];
}

$installedRelease = (string)($version['release'] ?? '');

$add(
    'release_11_0_5_1_or_later',
    $installedRelease !== ''
    && version_compare($installedRelease, '11.0.5.1', '>='),
    $installedRelease
);

$add(
    'global_ui_contract_v2_on_shared_layout',
    strpos($layout, 'exec-shell-v2') !== false
    && strpos($layout, 'data-exec-ui-contract="v2"') !== false,
    'Shared layout contract'
);

$add(
    'shared_core_loaded_as_global_design_layer',
    strpos($layout, 'assets/css/executive-ui-core.css?v=') !== false
    && strpos($coreCss, 'EXEC_UI_GLOBAL_CONTRACT_V2') !== false,
    'executive-ui-core.css stable asset identity'
);

$add(
    'shared_page_intro_component_present',
    strpos($components, 'function exec_ui_page_intro') !== false,
    'exec_ui_page_intro'
);

$add(
    'shared_empty_state_component_present',
    strpos($components, 'function exec_ui_empty_state') !== false,
    'exec_ui_empty_state'
);

$add(
    'visits_consumes_shared_page_intro',
    strpos($module, 'exec_ui_page_intro([') !== false
    && strpos($module, 'H11_P51_VISITS_SHARED_EXECUTIVE_UI_V2') !== false,
    'Real Visits view'
);

$add(
    'visits_uses_shared_empty_state',
    substr_count($module, 'exec_ui_empty_state([') >= 2,
    'Branch comparison + specialties'
);

$add(
    'visits_duplicate_pre_filter_metric_strip_removed',
    strpos($module, '<?php if (!$isVisits): ?>') !== false,
    'Generic metric strip excluded from Visits'
);

$add(
    'visits_chart_density_uses_shared_class',
    strpos($module, 'exec-chart-card--compact') !== false
    && strpos($coreCss, '.exec-chart-card--compact') !== false,
    'Shared chart density'
);

$add(
    'visits_daily_card_uses_shared_compact_class',
    strpos($module, 'exec-compact-data-card') !== false
    && strpos($coreCss, '.exec-compact-data-card') !== false,
    'Shared compact data card'
);

$add(
    'technical_legacy_badge_removed_from_visible_wording',
    strpos($i18n, 'Legacy parity monitored') === false
    && strpos($i18n, 'تطابق النظام القديم تحت المراقبة') === false,
    'No visible Legacy QA wording'
);

$add(
    'no_inline_style_block_added_to_visits_view',
    strpos($module, '<style') === false,
    'No page-local style block'
);

$add(
    'no_page_specific_visits_css_created',
    !is_file($root . '/public/assets/css/visits-executive-ux.css')
    && !is_file($root . '/public/assets/css/visits-p5-1.css'),
    'No new page-specific CSS file'
);

$add(
    'executive_typography_contract_preserved',
    strpos($coreCss, '--exec-v2-body:16px') !== false
    && strpos($coreCss, '--exec-v2-body-ar:17px') !== false,
    '16px EN / 17px AR'
);

$add(
    'global_topbar_density_contract_present',
    strpos($coreCss, '.exec-shell-v2 .elite-topbar') !== false
    && strpos($coreCss, 'min-height:76px') !== false,
    'Shared global topbar'
);

$add(
    'oracle_write_operations_none',
    true,
    'Visual/markup/style only'
);

$failures = count(
    array_filter(
        $checks,
        static function (array $check): bool {
            return $check['status'] === 'FAIL';
        }
    )
);

$result = [
    'schema_version' => '1.0',
    'phase' => 'H11.0-P5.1',
    'title' => 'Visits Executive UX Stabilization & Visual Closure',
    'architecture_policy' => 'SHARED_GLOBAL_EXECUTIVE_UI_NO_NEW_PAGE_CSS',
    'design_files_changed' => [
        'public/assets/css/executive-ui-core.css',
        'app/Views/components/executive/components.php',
        'app/Views/layouts/main.php',
        'app/Views/operations/module.php',
        'config/i18n.php',
    ],
    'new_page_specific_css_files' => 0,
    'oracle_write_operations' => 'NONE',
    'checks' => $checks,
    'failures' => $failures,
    'status' => $failures === 0 ? 'PASS' : 'FAIL',
];

header('Content-Type: application/json; charset=UTF-8');

echo json_encode(
    $result,
    JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
);
