<?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/DoctorsController.php');
$repo = (string)file_get_contents($root . '/app/Repositories/DoctorsRepository.php');
$view = (string)file_get_contents($root . '/app/Views/doctors/index.php');
$css = (string)file_get_contents($root . '/public/assets/css/doctors-executive-parity.css');
$js = (string)file_get_contents($root . '/public/assets/js/doctors-specialty-filter.js');
$layout = (string)file_get_contents($root . '/app/Views/layouts/main.php');
$live = (string)file_get_contents($root . '/tools/doctors-r45-p13-specialty-filter-live-acceptance.php');
$env = (string)file_get_contents($root . '/.env');

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

$checks = [
    'version' => ($version['version'] ?? '') === '11.0.13.0',
    'phase' => ($version['phase'] ?? '') === 'H11.0-P13.0',
    'request_array' => strpos(
        $controller,
        "Request::get('specialty_ids', [])"
    ) !== false,
    'request_normalization' => strpos(
        $controller,
        'normalizeSpecialtyIds('
    ) !== false,
    'available_specialties_discovery' => strpos(
        $repo,
        'public function availableSpecialties('
    ) !== false,
    'verified_id_source' => strpos(
        $repo,
        'TO_CHAR(SPECIAL_SPEC_ID)'
    ) !== false,
    'filter_contract' => strpos(
        $repo,
        'private function specialtyFilterContract('
    ) !== false,
    'bound_filter_parameters' => strpos(
        $repo,
        "'doctor_specialty_' . \$index"
    ) !== false,
    'summary_filtered' => strpos(
        $controller,
        'summary($year, $compareYear, $startMonth, $endMonth, $selectedSpecialtyIds)'
    ) !== false,
    'current_ranking_filtered' => strpos(
        $controller,
        'topDoctors($year, $metric, 10, $startMonth, $endMonth, $selectedSpecialtyIds)'
    ) !== false,
    'compare_ranking_filtered' => strpos(
        $controller,
        'topDoctors($compareYear, $metric, 10, $startMonth, $endMonth, $selectedSpecialtyIds)'
    ) !== false,
    'specialty_chart_filtered' => strpos(
        $controller,
        'specialties($year, $metric, 10, $startMonth, $endMonth, $selectedSpecialtyIds)'
    ) !== false,
    'revenue_mix_filtered' => strpos(
        $controller,
        'revenueMix($year, $startMonth, $endMonth, $selectedSpecialtyIds)'
    ) !== false,
    'checkbox_array_binding' => strpos(
        $view,
        'name="specialty_ids[]"'
    ) !== false,
    'searchable_multiselect' => strpos(
        $view,
        'data-doctors-specialty-search'
    ) !== false
        && strpos($js, 'applySearch') !== false,
    'clear_selection' => strpos(
        $view,
        'data-doctors-specialty-clear'
    ) !== false
        && strpos($js, 'box.checked = false') !== false,
    'selected_chips' => strpos(
        $view,
        'data-doctors-specialty-chips'
    ) !== false,
    'all_specialties_default' => strpos(
        $js,
        "summary.textContent = allLabel"
    ) !== false,
    'browser_gate' => strpos(
        $js,
        'DDB_DOCTORS_P13_SPECIALTY_FILTER_GATE'
    ) !== false,
    'responsive_filter_css' => strpos(
        $css,
        '.doctors130-specialty-filter'
    ) !== false
        && strpos($css, '@media(max-width:760px)') !== false,
    'flow_safe_doctors_topbar' => strpos(
        $layout,
        "'specialty-finance', 'doctors'"
    ) !== false,
    'asset_cache_bust' => strpos(
        $layout,
        '11.0.13.0-p13-doctors-specialty-filter'
    ) !== false,
    'live_acceptance' => strpos(
        $live,
        'DDB_DOCTORS_SPECIALTY_FILTER_PARITY_V1'
    ) !== false,
    'live_multi_select_gate' => strpos(
        $live,
        "'multi_select_exercised'"
    ) !== false,
    'live_summary_exact_gate' => strpos(
        $live,
        "'filtered_summary_exact'"
    ) !== false,
    'live_ranking_exact_gates' => strpos(
        $live,
        "'current_ranking_exact'"
    ) !== false
        && strpos($live, "'comparison_ranking_exact'") !== 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']['oracle_writes'] ?? ''
    ) === 'NONE',
];

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

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