<?php
declare(strict_types=1);

$root = dirname(__DIR__);
$version = json_decode((string)file_get_contents($root . '/DDB_VERSION.json'), true);
$repo = (string)file_get_contents($root . '/app/Repositories/DoctorsRepository.php');
$controller = (string)file_get_contents($root . '/app/Controllers/DoctorsController.php');
$view = (string)file_get_contents($root . '/app/Views/doctors/index.php');
$js = (string)file_get_contents($root . '/public/assets/js/doctors-specialty-filter.js');
$layout = (string)file_get_contents($root . '/app/Views/layouts/main.php');
$discovery = (string)file_get_contents($root . '/tools/doctors-r45-p13-0-1-specialty-discovery-live-acceptance.php');
$env = (string)file_get_contents($root . '/.env');

$runtime = $repo . "\n" . $controller . "\n" . $view . "\n" . $discovery;

$checks = [
    'version' => ($version['version'] ?? '') === '11.0.13.1',
    'phase' => ($version['phase'] ?? '') === 'H11.0-P13.0.1',
    'simple_numeric_specialty_query' => strpos(
        $repo,
        'GROUP BY SPECIAL_SPEC_ID'
    ) !== false
        && strpos($repo, 'COUNT(DISTINCT {$doctorExpr})') !== false,
    'fragile_inline_view_removed' => strpos(
        $repo,
        'GROUP BY specialty_id'
    ) === false,
    'no_trim_is_not_null_predicate' => strpos(
        $repo,
        'TRIM(SPECIAL_SPEC_EN_NAME) IS NOT NULL'
    ) === false,
    'empty_discovery_is_explicit_failure' => strpos(
        $repo,
        'DOCTORS_SPECIALTY_SOURCE_RETURNED_NO_ROWS'
    ) !== false,
    'revenue_mix_filter_params_bound' => strpos(
        $repo,
        "\$specialtyFilter['params']"
    ) !== false
        && strpos(
            $repo,
            '$rows = Database::fetchAll($this->hospitalCode, $sql, $params);'
        ) !== false,
    'page_not_blocked_by_filter_failure' => strpos(
        $controller,
        'throw $specialtyDiscoveryError'
    ) === false,
    'filter_availability_passed_to_view' => strpos(
        $controller,
        "'specialtyFilterAvailable'"
    ) !== false,
    'graceful_all_specialties_state' => strpos(
        $view,
        'The specialty filter could not be loaded. The report shows all specialties.'
    ) !== false,
    'unavailable_panel_has_no_search' => strpos(
        $view,
        'if ($specialtyFilterAvailable)'
    ) !== false,
    'duplicate_no_results_prevented' => strpos(
        $js,
        'options.length > 0 && visible === 0'
    ) !== false,
    'discovery_live_acceptance' => strpos(
        $discovery,
        'DDB_DOCTORS_SPECIALTY_DISCOVERY_V1_1'
    ) !== false,
    'cache_busted' => strpos(
        $layout,
        '11.0.13.1-p13-0-1-specialty-recovery'
    ) !== 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_p13_0_1']['oracle_writes'] ?? ''
    ) === 'NONE',
];

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

echo json_encode([
    'schema' => 'R45_P13_0_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);
