<?php

declare(strict_types=1);

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

use App\Services\FairYearComparisonPolicy;

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

$policy = new FairYearComparisonPolicy(new DateTimeImmutable('2026-07-16'));
$current = $policy->resolve(2026, 2025);
$reverse = $policy->resolve(2025, 2026);
$past = $policy->resolve(2025, 2024);
$january = (new FairYearComparisonPolicy(new DateTimeImmutable('2026-01-15')))->resolve(2026, 2025);

$add('current_year_last_complete_month_is_june', (int)$current['last_complete_month'] === 6);
$add('current_year_current_month_excluded', (int)$current['current_month_excluded'] === 7);
$add('current_year_effective_range', $current['from_date'] === '2026-01-01' && $current['to_date'] === '2026-06-30');
$add('reverse_selection_applies_same_rule', $reverse['from_date'] === '2025-01-01' && $reverse['to_date'] === '2025-06-30');
$add('past_years_use_full_year', (int)$past['last_complete_month'] === 12 && $past['from_date'] === null && $past['to_date'] === null);
$add('january_has_no_completed_months', (int)$january['last_complete_month'] === 0 && $january['from_date'] === null);
$years = $policy->availableYears(2014);
$add('year_options_include_2026_and_2014', $years[0] === 2026 && $years[count($years) - 1] === 2014);

$controller = file_get_contents(dirname(__DIR__) . '/app/Controllers/RevenueController.php') ?: '';
$view = file_get_contents(dirname(__DIR__) . '/app/Views/revenue/index.php') ?: '';
$css = file_get_contents(dirname(__DIR__) . '/public/assets/css/executive-ui-core.css') ?: '';
$version = json_decode(file_get_contents(dirname(__DIR__) . '/DDB_VERSION.json') ?: '{}', true) ?: [];

$add('version_contract', ($version['release'] ?? '') === '11.0.11.56' && ($version['phase'] ?? '') === 'H11.0-P11.1-R45-P1');
$add('controller_uses_shared_policy', strpos($controller, 'FairYearComparisonPolicy') !== false);
$add('controller_applies_effective_dates', strpos($controller, '$queryFromDate') !== false && strpos($controller, '$queryToDate') !== false);
$add('controller_filters_visible_months', strpos($controller, 'filterRowsByMonth') !== false);
$add('visible_year_selectors', strpos($view, "name=\"year\"") !== false && strpos($view, "name=\"compare_year\"") !== false);
$add('fair_comparison_notice_visible', strpos($view, 'Fair current-year comparison') !== false);
$add('current_month_exclusion_explained', strpos($view, 'incomplete current month') !== false);
$add('past_year_full_year_explained', strpos($view, 'Full-year comparison') !== false);
$add('controls_not_hidden_in_details', strpos($view, '<details class="surface-card eri-v2-controls') === false);
$add('responsive_controls_css', strpos($css, '.eri-r45p1-filter-grid') !== false);
$newService = file_get_contents(dirname(__DIR__) . '/app/Services/FairYearComparisonPolicy.php') ?: '';
$add('no_oracle_writes', stripos($controller . $newService, 'INSERT INTO') === false && stripos($controller . $newService, 'DELETE FROM') === false && stripos($controller . $newService, 'MERGE INTO') === false);

$passed = count(array_filter($tests, static fn(array $t): bool => $t['status'] === 'PASS'));
$failed = count($tests) - $passed;
$result = ['phase'=>'H11.0-P11.1-R45-P1','release'=>'11.0.11.56','passed'=>$passed,'failed'=>$failed,'tests'=>$tests];

$out = json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL;
echo $out;
exit($failed === 0 ? 0 : 1);
