"""Original offline token-bucket lesson. Fictional inputs, no Kalshi calls.
Rejected attempts consume no tokens in this model; not a live service claim.
"""
from fractions import Fraction as F


def run():
    capacity, refill = F(180), F(60)
    balance, previous = capacity, F(0)
    ledger = []
    for timestamp, cost in [(F(0), F(150)), (F(1, 4), F(50)),
                            (F(1, 2), F(50)), (F(4), F(200))]:
        balance = min(capacity, balance + refill * (timestamp - previous))
        previous = timestamp
        before = balance
        if cost > capacity:
            decision = 'cannot fit'
        elif cost > balance:
            decision = 'reject'
        else:
            decision = 'admit'
            balance -= cost
        ledger.append((timestamp, before, cost, decision, balance))
    for row in ledger:
        print(' | '.join(map(str, row)))
    print('Missing5 tokens at60/s:', F(5, 60), 'seconds')
    print('From45 until180:', F(180 - 45, 60), 'seconds')
    print('Sustained at cost10:', refill / 10, 'operations/s')
    print('Sustained at cost20:', refill / 20, 'operations/s')
    print('Smooth depletion180/(90-60):', F(180, 90 - 60), 'seconds')
    return ledger


if __name__ == '__main__':
    run()
