<?php
declare(strict_types=1);

$root = dirname(__DIR__);

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

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

$checks = [
    'version' => ($version['version'] ?? '') === '11.0.14.2',
    'phase' => ($version['phase'] ?? '') === 'H11.0-P14.2',
    'hidden_state_fixed' =>
        strpos(
            $css,
            '[hidden]{' . "\n" . '    display:none !important;'
        ) !== false,
    'three_tabs_users_roles_audit' =>
        substr_count($view, 'data-acl142-tab=') === 3
        && strpos($view, 'data-acl142-tab="roles"') !== false,
    'auto_select_user' =>
        strpos($js, 'var preferred = buttons.filter') !== false,
    'role_directory' =>
        strpos($view, 'data-acl142-role') !== false
        && strpos($view, 'data-acl142-new-role') !== false,
    'role_create_controller' =>
        strpos($controller, "if (\$action === 'save_role')") !== false
        && strpos($controller, 'generateRoleCode(') !== false,
    'role_update_repository' =>
        strpos($repository, 'public function saveRole(') !== false
        && strpos($repository, "'ROLE_CREATED'") !== false
        && strpos($repository, "'ROLE_UPDATED'") !== false,
    'site_admin_immutable' =>
        strpos($repository, 'SITE_ADMIN_POLICY_IS_IMMUTABLE') !== false
        && strpos($js, "roleCode === 'SITE_ADMIN'") !== false,
    'general_manager_editable' =>
        strpos($repository, "\$roleCode === 'GENERAL_MANAGER'") !== false
        && strpos($js, 'generalManager') !== false,
    'custom_role_policy_templates' =>
        strpos($view, 'value="EXPLICIT"') !== false
        && strpos($view, 'value="ALL_BUSINESS"') !== false,
    'role_descriptions' =>
        strpos($repository, 'ROLE_DESCRIPTION_EN') !== false
        && strpos($view, 'role_description_ar') !== false,
    'role_active_guard' =>
        strpos($repository, 'ROLE_HAS_ASSIGNED_USERS') !== false,
    'one_role_unique_index' =>
        strpos($upgrade, 'UX_DASH_ACL_USER_ONE_ROLE') !== false,
    'any_active_role_assignable' =>
        strpos($controller, "Request::post('role_code'") !== false
        && strpos($repository, 'ROLE_NOT_AVAILABLE') !== false,
    'wildcard_branch_scope' =>
        strpos($controller, "\$selectedBranches = ['*'];") !== false
        && strpos($access, "in_array('*', \$explicitBranches, true)") !== false,
    'strict_acl_branch_denial' =>
        strpos(
            $access,
            "if ((\$context['source'] ?? '') === 'ACL')"
        ) !== false,
    'schema_upgrade' =>
        strpos($upgrade, 'ROLE_DESCRIPTION_EN') !== false
        && strpos($upgrade, 'UX_DASH_ACL_USER_ONE_ROLE') !== false,
    'binary_permission_editor' =>
        strpos($view, 'data-acl142-permission-switch') !== false
        && strpos($js, 'effectiveAllowed(') !== false,
    'compact_layout' =>
        strpos($css, 'min-height:510px') !== false
        && strpos($css, 'min-height:590px') === false,
    'browser_gate' =>
        strpos(
            $js,
            'DDB_ACCESS_CONTROL_P14_2_BROWSER_GATE'
        ) !== false,
    'cache_busted' =>
        strpos(
            $layout,
            '11.0.14.2-p14-2-role-management'
        ) !== false,
    'live_acceptance' =>
        strpos(
            $live,
            'DDB_ACCESS_CONTROL_P14_2_LIVE_ACCEPTANCE'
        ) !== 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,
    'patch_oracle_writes' =>
        ($version['r45_p14_2']['patch_oracle_writes'] ?? '')
        === 'NONE',
];

$failed = [];

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

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

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