"""Explore event-time and availability-time leakage with Python 3."""

from datetime import datetime


def utc(value):
    return datetime.fromisoformat(value.replace("Z", "+00:00"))


# Each event has a business event time and a time at which the feature pipeline
# could first have known about it. Amounts and dates are fictional.
events = [
    ("A", utc("2026-01-01T08:00:00Z"), utc("2026-01-01T08:05:00Z"), 10),
    ("A", utc("2026-01-01T09:00:00Z"), utc("2026-01-01T11:00:00Z"), 20),
    ("A", utc("2026-01-01T12:00:00Z"), utc("2026-01-01T12:05:00Z"), 30),
    ("B", utc("2026-01-01T09:30:00Z"), utc("2026-01-01T09:35:00Z"), 7),
]
decisions = [("A", utc("2026-01-01T10:00:00Z")), ("B", utc("2026-01-01T10:00:00Z"))]


def total(customer, cutoff, mode):
    """Sum amounts using a historical, event-only, or current-snapshot join."""
    eligible = []
    for entity, happened, available, amount in events:
        if entity != customer:
            continue
        if mode == "historical" and (happened > cutoff or available > cutoff):
            continue
        if mode == "event_only" and happened > cutoff:
            continue
        eligible.append(amount)
    return sum(eligible)


def main():
    print("customer | historical | event only | current snapshot")
    for customer, cutoff in decisions:
        values = [total(customer, cutoff, mode) for mode in ("historical", "event_only", "current")]
        print(f"{customer:8} | {values[0]:10} | {values[1]:10} | {values[2]:16}")
    assert total("A", decisions[0][1], "historical") == 10
    assert total("A", decisions[0][1], "event_only") == 30
    assert total("A", decisions[0][1], "current") == 60
    assert total("B", decisions[1][1], "historical") == 7


if __name__ == "__main__":
    main()
