<?php
declare(strict_types=1);

$root = dirname(__DIR__);
$version = json_decode(
    (string)file_get_contents($root . '/DDB_VERSION.json'),
    true
);
$controller = (string)file_get_contents(
    $root . '/app/Controllers/AccessControlController.php'
);
$repository = (string)file_get_contents(
    $root . '/app/Repositories/AccessControlRepository.php'
);
$view = (string)file_get_contents(
    $root . '/app/Views/access_control/index.php'
);
$css = (string)file_get_contents(
    $root . '/public/assets/css/access-control.css'
);
$js = (string)file_get_contents(
    $root . '/public/assets/js/access-control.js'
);
$layout = (string)file_get_contents(
    $root . '/app/Views/layouts/main.php'
);
$live = (string)file_get_contents(
    $root . '/tools/access-control-r45-p14-1-live-acceptance.php'
);
$env = (string)file_get_contents($root . '/.env');

$runtime = $controller
    . "\n" . $repository
    . "\n" . $view
    . "\n" . $layout
    . "\n" . $live;

$checks = [
    'version' => ($version['version'] ?? '') === '11.0.14.1',
    'phase' => ($version['phase'] ?? '') === 'H11.0-P14.1',
    'three_sections' =>
        substr_count($view, 'data-acl141-tab=') === 3
        && substr_count($view, 'data-acl141-section=') === 3,
    'searchable_user_directory' =>
        strpos($view, 'data-acl141-user-search') !== false
        && strpos($js, 'data-acl141-user-search') !== false,
    'one_role_radio_contract' =>
        substr_count($view, 'data-acl141-role') === 3
        && strpos($repository, 'ONE_PROTECTED_ROLE_ONLY') !== false,
    'clear_protected_role_supported' =>
        strpos($view, "value=\"\"") !== false
        && strpos(
            $repository,
            'USER_PROTECTED_ACCESS_CLEARED'
        ) !== false,
    'site_admin_protection_preserved' =>
        strpos(
            $repository,
            'AT_LEAST_ONE_SITE_ADMIN_REQUIRED'
        ) !== false,
    'branch_mode_contract' =>
        strpos($view, 'name="branch_mode"') !== false
        && strpos($controller, "'SELECTED'") !== false,
    'legacy_assignment_chips_removed' =>
        strpos($view, 'acl140-assignments') === false,
    'legacy_permission_matrix_removed' =>
        strpos($view, 'acl140-matrix') === false,
    'binary_permission_switches' =>
        strpos($view, 'gm_allowed[') !== false
        && strpos($view, 'data-acl141-permission-switch') !== false,
    'automatic_policy_preserved' =>
        strpos($controller, '$desiredAllowed !== $baseAllowed') !== false,
    'recommended_policy_reset' =>
        strpos($controller, 'reset_general_manager') !== false
        && strpos($view, 'reset_general_manager') !== false,
    'permission_codes_hidden_from_view' =>
        strpos($view, "<?= e(\$code) ?>") === false,
    'friendly_audit_labels' =>
        strpos($view, '$actionLabels') !== false,
    'access_counts' =>
        strpos($repository, 'public function accessCounts') !== false,
    'browser_gate' =>
        strpos(
            $js,
            'DDB_ACCESS_CONTROL_P14_1_BROWSER_GATE'
        ) !== false,
    'responsive_layout' =>
        strpos($css, '.acl141-users-layout') !== false
        && strpos($css, '@media(max-width:620px)') !== false,
    'cache_busted' =>
        strpos(
            $layout,
            '11.0.14.1-p14-1-practical-access'
        ) !== false,
    'live_acceptance' =>
        strpos(
            $live,
            'DDB_ACCESS_CONTROL_P14_1_LIVE_ACCEPTANCE'
        ) !== false,
    'one_role_live_gate' =>
        strpos(
            $live,
            "'one_protected_role_per_user'"
        ) !== false,
    'php7_match_absent' =>
        preg_match('/\bmatch\s*\(/', $runtime) !== 1,
    'php7_nullsafe_absent' => strpos($runtime, '?->') === false,
    'php7_attributes_absent' =>
        preg_match('/#\s*\[/', $runtime) !== 1,
    'php7_arrow_functions_absent' =>
        preg_match('/\bfn\s*\(/', $runtime) !== 1,
    'revenue_summary_enabled' =>
        preg_match('/^USE_REVENUE_SUMMARY=1$/m', $env) === 1,
    'revenue_summary_age_30' =>
        preg_match(
            '/^REVENUE_SUMMARY_MAX_AGE_HOURS=30$/m',
            $env
        ) === 1,
    'oracle_writes_none' =>
        ($version['r45_p14_1']['oracle_writes'] ?? '') === 'NONE',
];

$failed = [];
foreach ($checks as $name => $passed) {
    if (!$passed) {
        $failed[] = $name;
    }
}

echo json_encode([
    'schema' => 'R45_P14_1_STATIC_QA',
    'checks' => $checks,
    'passed' => count($checks) - count($failed),
    'failed' => $failed,
    'oracle_writes' => 'NONE',
], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES), PHP_EOL;

exit($failed === [] ? 0 : 1);
