<?php

declare(strict_types=1);

require_once dirname(__DIR__) . '/app/bootstrap.php';

$tests = [];
$add = static function (string $name, bool $pass, string $detail = '') use (&$tests): void {
    $tests[] = ['name' => $name, 'status' => $pass ? 'PASS' : 'FAIL', 'detail' => $detail];
};

$root = dirname(__DIR__);
$repo = file_get_contents($root . '/app/Repositories/RevenueRepository.php') ?: '';
$controller = file_get_contents($root . '/app/Controllers/RevenueController.php') ?: '';
$profiler = file_get_contents($root . '/app/Services/RevenueConsolidatedProfiler.php') ?: '';
$tool = file_get_contents($root . '/tools/revenue-consolidated-profile.php') ?: '';
$version = json_decode(file_get_contents($root . '/DDB_VERSION.json') ?: '{}', true) ?: [];

$add('version_contract', ($version['release'] ?? '') === '11.0.11.57' && ($version['phase'] ?? '') === 'H11.0-P11.1-R45-P2');
$add('bundle_method_exists', strpos($repo, 'function monthlyRevenueBundle(') !== false);
$add('range_bundle_exists', strpos($repo, 'monthlyRevenueBundleRange(') !== false);
$add('single_source_aggregation', strpos($repo, 'SUM(NVL(CREDIT_VALUE, 0) + NVL(CASH_VALUE, 0)) AS TOTAL_AMOUNT') !== false);
$add('cash_aggregation', strpos($repo, 'SUM(NVL(CASH_VALUE, 0)) AS CASH_AMOUNT') !== false);
$add('insurance_aggregation', strpos($repo, 'SUM(NVL(CREDIT_VALUE, 0)) AS INSURANCE_AMOUNT') !== false);
$add('moh_conditional_aggregation', strpos($repo, 'WHEN CUSTOMER_CODE = :moh_customer_code') !== false);
$add('bundle_split_four_series', strpos($repo, "'total' => []") !== false && strpos($repo, "'cash' => []") !== false && strpos($repo, "'insurance' => []") !== false && strpos($repo, "'moh' => []") !== false);
$add('controller_calls_bundle_once', substr_count($controller, 'monthlyRevenueBundle(') === 1);
$add('controller_no_four_revenue_calls', substr_count($controller, '->monthlyRevenue(') === 0);
$add('profit_loss_preserved', substr_count($controller, 'monthlyProfitLoss(') === 1);
$add('fair_year_dates_preserved', strpos($controller, '$queryFromDate') !== false && strpos($controller, '$queryToDate') !== false);
$policy = file_get_contents($root . '/app/Services/FairYearComparisonPolicy.php') ?: '';
$view = file_get_contents($root . '/app/Views/revenue/index.php') ?: '';
$add('fair_year_policy_preserved', strpos($controller, 'FairYearComparisonPolicy') !== false && strpos($policy, 'last_complete_month') !== false);
$add('year_selectors_preserved', strpos($view, 'name="year"') !== false && strpos($view, 'name="compare_year"') !== false);
$add('current_month_exclusion_notice_preserved', strpos($view, 'incomplete current month') !== false);
$add('range_mode_preserved', strpos($controller, "\$mode === 'range'") !== false);
$add('legacy_api_preserved_for_parity', strpos($repo, 'public function monthlyRevenue(') !== false);
$add('live_profiler_exists', strpos($profiler, 'legacy_four_query_path') !== false && strpos($profiler, 'consolidated_one_query_path') !== false);
$add('numeric_parity_gate', strpos($profiler, "'tolerance' => 0.01") !== false && strpos($profiler, 'compareBundles') !== false);
$add('performance_improvement_calculated', strpos($profiler, 'average_improvement_percent') !== false);
$add('query_count_reduction_contract', strpos($profiler, "'total_primary_calls_before' => 5") !== false && strpos($profiler, "'total_primary_calls_after' => 2") !== false);
$add('cli_evidence_outputs', strpos($tool, 'REVENUE_CONSOLIDATED_PERFORMANCE.json') !== false && strpos($tool, 'REVENUE_CONSOLIDATED_PERFORMANCE.csv') !== false);
$add('no_mv_activation', strpos($controller, 'use_revenue_mv') === false);
$combined = $repo . $controller . $profiler;
$add('no_oracle_writes', stripos($combined, 'INSERT INTO') === false && stripos($combined, 'UPDATE ') === false && stripos($combined, 'DELETE FROM') === false && stripos($combined, 'MERGE INTO') === false);

$passed = count(array_filter($tests, static fn(array $test): bool => $test['status'] === 'PASS'));
$failed = count($tests) - $passed;
$result = ['phase' => 'H11.0-P11.1-R45-P2', 'release' => '11.0.11.57', 'passed' => $passed, 'failed' => $failed, 'tests' => $tests];
echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL;
exit($failed === 0 ? 0 : 1);
