<?php
declare(strict_types=1);

$options = getopt('', ['target::']);
$target = rtrim((string)($options['target'] ?? dirname(__DIR__)), "\\/");

function pathJoin(string ...$parts): string {
    return preg_replace('~[\\\\/]+~', DIRECTORY_SEPARATOR, implode(DIRECTORY_SEPARATOR, $parts)) ?? '';
}
function readFileStrict(string $root, string $relative): string {
    $path = pathJoin($root, $relative);
    $value = @file_get_contents($path);
    if ($value === false) {
        throw new RuntimeException('READ_FAILED|' . $relative);
    }
    return $value;
}

$checks = [];
$failures = [];
$check = static function (bool $condition, string $code) use (&$checks, &$failures): void {
    $checks[] = $code;
    if (!$condition) {
        $failures[] = $code;
    }
};

try {
    $service = readFileStrict($target, 'app/Services/DiarWorkspaceNavigation.php');
    $controller = readFileStrict($target, 'app/Controllers/DiarInvestigationController.php');
    $queue = readFileStrict($target, 'app/Views/diar_investigations/index.php');
    $detail = readFileStrict($target, 'app/Views/diar_investigations/detail.php');
    $module = readFileStrict($target, 'app/Views/diar_workspace/module.php');
    $executive = readFileStrict($target, 'app/Views/diar_command_center/executive_overview.php');
    $layout = readFileStrict($target, 'app/Views/layouts/main.php');
    $css = readFileStrict($target, 'public/assets/css/diar-navigation-closure.css');

    $check(strpos($service, "'route' => 'diar-investigations'") !== false, 'INVESTIGATION_CANONICAL_ROUTE');
    $check(strpos($service, "'sidebar_label' => \$tr('Investigations'") !== false, 'SHORT_SIDEBAR_LABEL');
    $check(strpos($service, 'contextTabs(string $activePage)') !== false, 'CONTEXT_TAB_SERVICE');
    $check(strpos($service, "'diar-investigation-workspace'") !== false, 'OVERVIEW_ROUTE_PRESERVED');
    $check(strpos($service, "'diar-investigations', 'diar-investigation'") !== false, 'QUEUE_DETAIL_ACTIVE_GROUP');
    $check(strpos($layout, 'DIAR_6B_2_R3_1_3_CONTEXT_NAV') !== false, 'LAYOUT_CONTEXT_NAV');
    $check(strpos($layout, "DiarWorkspaceNavigation::contextTabs") !== false, 'LAYOUT_CONTEXT_SERVICE');
    $check(strpos($layout, "'diar-investigation' =>") !== false, 'DETAIL_PAGE_TITLE');
    $check(strpos($layout, "['diar-investigations','diar-investigation']") !== false, 'DETAIL_ASSET_LOADING');
    $check(strpos($layout, 'diar-navigation-closure.css') !== false, 'NAV_CSS_LOADED');
    $check(strpos($executive, "url_to('diar-investigations')") !== false, 'EXECUTIVE_TO_QUEUE');
    $check(strpos($executive, "url_to('diar-investigation-workspace')") === false, 'NO_EXECUTIVE_TO_OVERVIEW');
    $check(strpos($module, 'Open cases & events') !== false, 'OVERVIEW_TO_QUEUE_LABEL');
    $check(strpos($queue, "'return'=>\$returnToQueue") !== false, 'QUEUE_PRESERVES_RETURN');
    $check(strpos($queue, "_tabs.php") === false, 'NO_LEGACY_GLOBAL_TABS_QUEUE');
    $check(strpos($detail, "_tabs.php") === false, 'NO_LEGACY_GLOBAL_TABS_DETAIL');
    $check(substr_count($detail, '<div class="case-layout">') === 1, 'DETAIL_LAYOUT_SINGLE_WRAPPER');
    $check(strpos($detail, 'return_to') !== false, 'DETAIL_POST_PRESERVES_RETURN');
    $check(strpos($detail, 'Back to cases & events') !== false, 'DETAIL_BACK_TO_QUEUE');
    $check(strpos($controller, "'activePage' => 'diar-investigation'") !== false, 'DETAIL_ACTIVE_ROUTE');
    $check(strpos($controller, 'safeReturnUrl') !== false, 'RETURN_URL_VALIDATION');
    $check(strpos($controller, "!== 'diar-investigations'") !== false, 'RETURN_ROUTE_ALLOWLIST');
    $check(strpos($css, '.diar-context-nav') !== false, 'CONTEXT_NAV_STYLE');
    $check(strpos($css, 'grid-template-columns: repeat(4,minmax(0,1fr))') !== false, 'INVESTIGATION_FILTER_GRID');
    $check(strpos($css, 'overflow-x: auto') !== false, 'LOCAL_TABLE_OVERFLOW');
    $check(strpos($css, 'position: fixed') === false, 'CONTEXT_NAV_NOT_FIXED');
    $check(strpos($service, 'INSERT ') === false && strpos($service, 'UPDATE ') === false && strpos($service, 'DELETE ') === false, 'NO_DATA_WRITES');
} catch (Throwable $e) {
    $failures[] = $e->getMessage();
}

if ($failures !== []) {
    fwrite(STDERR, 'DIAR_6B_2_R3_1_3_1_STATIC_QA|FAIL|CHECKS=' . count($checks) . '|FAILED=' . implode(',', $failures) . PHP_EOL);
    exit(1);
}

echo 'DIAR_6B_2_R3_1_3_1_STATIC_QA|PASS|CHECKS=' . count($checks)
    . '|CANONICAL_ROUTES=PASS|CONTEXT_TABS=PASS|RETURN_NAV=PASS|INVESTIGATION_LAYOUT=PASS|READ_ONLY=PASS' . PHP_EOL;
