#!/usr/bin/env python3
"""Fictional export-window exercise. Python 3.10+, no network or vendor data."""
import calendar
import json
from datetime import datetime, timedelta, timezone

RETENTION_DAYS = 30

def inspect(name, successes):
    intervals = []
    for previous, current in zip(successes, successes[1:]):
        days = (current - previous).days
        intervals.append({
            "previous_success": previous.isoformat(),
            "next_success": current.isoformat(),
            "gap_days": days,
            "unrecoverable_days": max(0, days - RETENTION_DAYS),
            "remaining_margin_days": RETENTION_DAYS - days,
        })
    return {
        "name": name,
        "max_gap_days": max(x["gap_days"] for x in intervals),
        "unrecoverable_days": sum(x["unrecoverable_days"] for x in intervals),
        "intervals": intervals,
    }

def results():
    month_ends = [datetime(2026, m, calendar.monthrange(2026, m)[1], tzinfo=timezone.utc)
                  for m in range(1, 13)]
    start = datetime(2026, 1, 1, tzinfo=timezone.utc)
    rows = [inspect("2026 month ends", month_ends),
            inspect("2026 month ends, April export fails", [x for x in month_ends if x.month != 4])]
    for cadence in (7, 14, 15, 16):
        # Successful exports at scheduled runs 0, 1 and 3; run 2 fails completely.
        successes = [start + timedelta(days=cadence * i) for i in (0, 1, 3)]
        rows.append(inspect(f"every {cadence} days, one export fails", successes))
    assert [r["max_gap_days"] for r in rows] == [31, 61, 14, 28, 30, 32]
    assert [r["unrecoverable_days"] for r in rows] == [6, 36, 0, 0, 0, 2]
    return {
        "method": "Half-open full-history export window [success-30days, success); instant download; UTC days.",
        "retention_days": RETENTION_DAYS,
        "scope": "Gaps between listed successful exports only; excludes initial backfill and final tail.",
        "limitations": ["Fictional 30-day retention, not a vendor claim", "All included events export completely",
                        "No late events, row caps, provider boundary semantics or network delays"],
        "policies": rows,
    }

if __name__ == "__main__":
    print(json.dumps(results(), indent=2))
