<?php
declare(strict_types=1);

$root = dirname(__DIR__);
$version = json_decode((string)file_get_contents($root . '/DDB_VERSION.json'), true);
$current = (string)file_get_contents($root . '/app/Repositories/SpecialtyFinanceRepository.php');
$reference = (string)file_get_contents($root . '/app/Repositories/LegacySpecialtyActivityRepository.php');
$service = (string)file_get_contents($root . '/app/Services/SpecialtyFinanceActivityParityService.php');
$live = (string)file_get_contents($root . '/tools/specialty-finance-r45-p12-0-live-acceptance.php');
$view = (string)file_get_contents($root . '/app/Views/specialty_finance/index.php');
$env = (string)file_get_contents($root . '/.env');

$runtime = $current . "\n" . $reference . "\n" . $service . "\n" . $live;

$checks = [
    'version' => ($version['version'] ?? '') === '11.0.12.0',
    'phase' => ($version['phase'] ?? '') === 'H11.0-P12.0',
    'source_contract_method' => strpos($current, 'public function activitySourceContract()') !== false,
    'period_activity_method' => strpos($current, 'public function specialtyActivityComparison(') !== false,
    'monthly_activity_method' => strpos($current, 'public function specialtyActivityMonthlyComparison(') !== false,
    'doctor_total_reconciliation_method' => strpos($current, 'public function activityDoctorTotals(') !== false,
    'required_outpatient_column' => strpos($current, "'TOTAL_OP_VISITS'") !== false,
    'required_admissions_column' => strpos($current, "'TOTAL_INPAT_VISITS'") !== false,
    'specialty_doctor_dedup' => strpos($current, 'per_doctor_specialty AS') !== false
        && strpos($current, 'GROUP BY YEARID, {$specialtyKeyExpr}, {$doctorExpr}') !== false,
    'monthly_specialty_doctor_dedup' => strpos($current, 'per_doctor_specialty_month AS') !== false
        && strpos($current, 'GROUP BY YEARID, MONTHID, {$specialtyKeyExpr}, {$doctorExpr}') !== false,
    'no_silent_missing_column_zero' => strpos($current, 'assertActivitySourceContract') !== false
        && strpos($current, 'SPECIALTY_ACTIVITY_SOURCE_CONTRACT_UNAVAILABLE') !== false,
    'independent_reference_repository' => strpos($reference, 'final class LegacySpecialtyActivityRepository') !== false
        && strpos($reference, 'new SpecialtyFinanceRepository') === false,
    'reference_period_path' => strpos($reference, 'specialtyActivityForYear(') !== false,
    'reference_monthly_path' => strpos($reference, 'specialtyActivityMonthlyForYear(') !== false,
    'parity_service' => strpos($service, 'final class SpecialtyFinanceActivityParityService') !== false,
    'period_exact_gates' => strpos($service, "'current_period_exact'") !== false
        && strpos($service, "'compare_period_exact'") !== false,
    'monthly_exact_gates' => strpos($service, "'current_monthly_exact'") !== false
        && strpos($service, "'compare_monthly_exact'") !== false,
    'doctor_total_reconciliation_gates' => strpos($service, 'current_specialty_total_reconciles_to_doctor_total') !== false
        && strpos($service, 'compare_specialty_total_reconciles_to_doctor_total') !== false,
    'both_metrics_activity_gate' => strpos($service, "'both_metrics_have_activity'") !== false,
    'live_acceptance_tool' => strpos($live, 'SpecialtyFinanceActivityParityService') !== false,
    'evidence_output' => strpos($live, 'SPECIALTY_FINANCE_R45_P12_0_ACTIVITY_LIVE_ACCEPTANCE_') !== false,
    'view_unchanged_until_live_proof' => hash('sha256', $view) === 'ba42e9ac6a5ce61d6384d439674d16adcd3f3a898f0cb884ee39af1d690ded86',
    '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,
    'ui_integration_deferred' => ($version['r45_p12_0']['ui_integration'] ?? '') === 'DEFERRED_UNTIL_LIVE_PASS',
    'oracle_contract_read_only' => ($version['r45_p12_0']['oracle_writes'] ?? '') === 'NONE',
];

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

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