<?php
declare(strict_types=1);
$root = dirname(__DIR__);
$checks = [];
$failures = [];
$require = static function (bool $condition, string $code) use (&$checks, &$failures): void {
    $checks[] = $code;
    if (!$condition) { $failures[] = $code; }
};
$files = [
 'app/Services/DiarConnectionResolver.php',
 'app/Repositories/DiarExecutiveOverviewRepository.php',
 'app/Controllers/DiarExecutiveOverviewController.php',
 'app/Views/diar_command_center/executive_overview.php',
 'public/assets/css/diar-executive-overview.css',
];
foreach ($files as $f) { $require(is_file($root . '/' . $f), 'FILE_' . strtoupper(str_replace(['/', '.', '-'], '_', $f))); }
$repo = (string)@file_get_contents($root . '/app/Repositories/DiarExecutiveOverviewRepository.php');
$router = (string)@file_get_contents($root . '/public/index.php');
$tabs = (string)@file_get_contents($root . '/app/Views/diar_command_center/_tabs.php');
$controller = (string)@file_get_contents($root . '/app/Controllers/DiarExecutiveOverviewController.php');
$view = (string)@file_get_contents($root . '/app/Views/diar_command_center/executive_overview.php');

$require(substr_count($router, 'DIAR_6B_1_COMPAT_CONTROLLER_USE_R1_1') === 1, 'ROUTER_IMPORT_MARKER');
$require(substr_count($router, 'DIAR_6B_1_COMPAT_ROUTER_R1_1') === 1, 'ROUTER_CASE_MARKER');
$require(substr_count($tabs, 'DIAR_6B_1_COMPAT_TAB_R1_1') === 1, 'TAB_MARKER');
$require(strpos($controller, "AccessControl::allows('diar.command.view')") !== false, 'REUSE_EXISTING_PERMISSION');
$require(strpos($controller, 'diar.executive_overview.view') === false, 'NO_NEW_ACL_DEPENDENCY');
$require(strpos($view, 'diar-executive-overview.css?v=diar-6b-1-r1-1-compat') !== false, 'VIEW_SELF_ASSET');
$require(strpos($repo, 'Database::execute(') === false, 'READ_ONLY_NO_EXECUTE');
$require(strpos($repo, 'Database::executeTransaction(') === false, 'READ_ONLY_NO_TRANSACTION');
$require(!preg_match('/\b(INSERT|UPDATE|DELETE|MERGE|TRUNCATE)\s+(INTO|FROM|TABLE|DASH_)/i', $repo), 'READ_ONLY_NO_DML_SQL');
$require(strpos($repo, 'DASH_DIAR_MONTH_SNAPSHOTS') !== false, 'MONTH_SNAPSHOT_SOURCE');
$require(strpos($repo, 'DASH_DIAR_MONTH_CASES') !== false, 'MONTH_CASE_SOURCE');
$require(strpos($repo, 'DASH_DIAR_MONTH_DAYS') !== false, 'MONTH_DAY_SOURCE');
$require(strpos($repo, 'STORE_CODE IN (56,57)') !== false, 'BAD_STORE_GUARD');
$require(strpos($repo, "'database_link' => false") !== false, 'NO_DB_LINK_CONTRACT');
$require(strpos($repo, "'primary_baseline_days' => 365") !== false, 'BASELINE_365');
$require(strpos($repo, "'daily_fact_retention_days' => 400") !== false, 'RETENTION_400');
$require(strpos($repo, "'late_refresh_days' => 7") !== false, 'LATE_7');
$require(strpos($view, 'ليست خسارة') !== false || strpos($view, 'not confirmed financial loss') !== false, 'LOSS_BOUNDARY');
if ($failures !== []) {
    fwrite(STDERR, 'DIAR_6B_COMPAT_STATIC_QA|FAIL|CHECKS=' . count($checks) . '|FAILED=' . implode(',', $failures) . PHP_EOL);
    exit(1);
}
echo 'DIAR_6B_COMPAT_STATIC_QA|PASS|CHECKS=' . count($checks) . '|READ_ONLY=PASS|SEMANTIC_MERGE=PASS' . PHP_EOL;
