"""Reproduce an explicitly synthetic example. No account or client data."""
import csv
import json
from datetime import datetime, timedelta, timezone
from pathlib import Path

HERE = Path(__file__).resolve().parent
CUTOFF = datetime(2026, 10, 22, tzinfo=timezone.utc)
WINDOW = timedelta(days=14)
rows = []
for cohort, started in [("A", datetime(2026, 10, 1, tzinfo=timezone.utc)),
                        ("B", datetime(2026, 10, 20, tzinfo=timezone.utc))]:
    for i in range(1, 101):
        paid = None
        if i <= 40:
            paid = started + timedelta(days=2 if cohort == "B" and i <= 10 else 14)
        rows.append({"synthetic_id": f"{cohort}{i:03d}", "cohort": cohort,
                     "trial_started_at_utc": started.isoformat(),
                     "first_paid_at_utc": paid.isoformat() if paid else "",
                     "data_type": "synthetic"})

assert len({r["synthetic_id"] for r in rows}) == len(rows) == 200
with (HERE / "subscription-cohort-example.csv").open("w", newline="", encoding="utf-8") as f:
    writer = csv.DictWriter(f, fieldnames=list(rows[0]))
    writer.writeheader()
    writer.writerows(rows)

def summarize(cutoff):
    started, observed, eligible, accepted = 0, 0, 0, 0
    for row in rows:
        start = datetime.fromisoformat(row["trial_started_at_utc"])
        paid = datetime.fromisoformat(row["first_paid_at_utc"]) if row["first_paid_at_utc"] else None
        if start > cutoff:
            continue
        started += 1
        observed += bool(paid and paid <= cutoff)
        if start + WINDOW <= cutoff:
            eligible += 1
            accepted += bool(paid and start <= paid <= start + WINDOW and paid <= cutoff)
    return {"cutoff_utc": cutoff.isoformat(), "observation_window_days": 14,
            "trials_started": started, "paid_observed_by_cutoff": observed,
            "all_trials_conversion_to_date": observed / started if started else None,
            "fully_observed_trials": eligible,
            "paid_within_14_days_among_fully_observed_trials": accepted,
            "fully_observed_14_day_conversion": accepted / eligible if eligible else None,
            "immature_trials": started - eligible}

current = summarize(CUTOFF)
later = summarize(datetime(2026, 11, 3, tzinfo=timezone.utc))
assert current["paid_observed_by_cutoff"] == 50
assert current["fully_observed_trials"] == 100
assert current["fully_observed_14_day_conversion"] == 0.4
assert later["fully_observed_trials"] == 200
assert later["fully_observed_14_day_conversion"] == 0.4
assert summarize(datetime(2026, 10, 2, tzinfo=timezone.utc))["fully_observed_14_day_conversion"] is None
assert summarize(datetime(2026, 10, 2, tzinfo=timezone.utc))["trials_started"] == 100
assert summarize(datetime(2026, 9, 30, tzinfo=timezone.utc))["all_trials_conversion_to_date"] is None
result = {"data_type": "entirely synthetic, not measured research or a forecast",
          "current": current, "later_synthetic_fixture": later}
(HERE / "subscription-cohort-results.json").write_text(json.dumps(result, indent=2) + "\n", encoding="utf-8")
print(json.dumps(result, indent=2))
