"""Independent date-based replay of the published synthetic interval examples."""
from datetime import date
from pathlib import Path
import itertools
import json

here = Path(__file__).parent
fixture = json.loads((here / 'evidence-history-fixtures.json').read_text())
assert fixture['evidence_type'] == 'synthetic'
assert fixture['is_real_provider_record'] is False

def day(value):
    result = date.fromisoformat(value)
    assert result.isoformat() == value and 2000 <= result.year <= 2099
    return result.toordinal()

def interval(row, kind):
    first = day(row[kind + '_from'])
    last = float('inf') if row[kind + '_until'] is None else day(row[kind + '_until'])
    assert first < last
    return first, last

rows = fixture['records']
assert len({r['id'] for r in rows}) == len(rows)
for left, right in itertools.combinations(rows, 2):
    overlaps = []
    for dimension in ('effective', 'known'):
        a, b = interval(left, dimension), interval(right, dimension)
        overlaps.append(max(a[0], b[0]) < min(a[1], b[1]))
    assert not all(overlaps), 'Conflicting two-dimensional intervals'

cases = fixture['examples'] + [
    {'label': 'Exact first knowledge start', 'effectiveOn': '2027-01-15', 'knownOn': '2027-01-02', 'expected_record': 'initial-open', 'expected_limit': 1000},
    {'label': 'Exact future-notice knowledge boundary for January', 'effectiveOn': '2027-01-15', 'knownOn': '2027-01-20', 'expected_record': 'initial-bounded', 'expected_limit': 1000},
    {'label': 'Exact February effective start', 'effectiveOn': '2027-02-01', 'knownOn': '2027-01-20', 'expected_record': 'future-notice', 'expected_limit': 500},
    {'label': 'Exact correction knowledge start', 'effectiveOn': '2027-01-15', 'knownOn': '2027-02-10', 'expected_record': 'historical-correction', 'expected_limit': 800},
    {'label': 'Before the effective history begins', 'effectiveOn': '2026-12-31', 'knownOn': '2027-01-10', 'expected_record': None, 'expected_limit': None}
]
results = []
for case in cases:
    e, k = day(case['effectiveOn']), day(case['knownOn'])
    matches = []
    for row in rows:
        ef, eu = interval(row, 'effective')
        kf, ku = interval(row, 'known')
        if ef <= e < eu and kf <= k < ku:
            matches.append(row)
    assert len(matches) <= 1
    row = matches[0] if matches else None
    actual_id = None if row is None else row['id']
    actual_limit = None if row is None else row['daily_limit']
    assert actual_id == case['expected_record'], case['label']
    assert actual_limit == case['expected_limit'], case['label']
    results.append({'case': case['label'], 'effectiveOn': case['effectiveOn'], 'knownOn': case['knownOn'], 'selected_record': actual_id, 'daily_limit': actual_limit, 'is_projection': e > k})
assert 800 - 1000 == -200
output = {'evidence_type': 'synthetic', 'method': 'Independent Python date-based interval replay', 'cases_passed': len(results), 'provider_tests': 0, 'network_requests': 0, 'cases': results}
(here / 'evidence-history-verification.json').write_text(json.dumps(output, indent=2) + '\n')
print(json.dumps({'cases_passed': len(results), 'provider_tests': 0, 'network_requests': 0}))
