"""Aha demo: same request trace, two algorithms tuned to the same average
rate (5 requests / 5 seconds), different burst behavior.
"""

from rate_limiter import TokenBucket, SlidingWindowLog

CAPACITY = 5
WINDOW = 5.0

# (timestamp, label) — an initial burst of 5, a lone request mid-recovery,
# then a second burst of 5 exactly at the window boundary.
TRACE = (
    [(0.0, "burst #1") for _ in range(5)]
    + [(2.5, "lone request")]
    + [(5.0, "burst #2") for _ in range(5)]
)


def run():
    bucket = TokenBucket(capacity=CAPACITY, refill_rate=CAPACITY / WINDOW)
    window = SlidingWindowLog(max_requests=CAPACITY, window=WINDOW)

    print(f"{'t':>5}  {'event':<14} {'token bucket':<14} {'sliding window':<14}")
    for now, label in TRACE:
        bucket_result = "allow" if bucket.allow(now) else "DENY"
        window_result = "allow" if window.allow(now) else "DENY"
        flag = "  <-- differ!" if bucket_result != window_result else ""
        print(f"{now:>5.1f}  {label:<14} {bucket_result:<14} {window_result:<14}{flag}")


if __name__ == "__main__":
    run()
