"""Print every number the lesson quotes. Run it from this folder: python3 show_numbers.py"""
from decimal import Decimal

from rent_vs_own import (DAYS, GPU_HOUR, HEADROOM, HOURS_PER_MONTH, OPENING, PEOPLE, PRICE_IN, PRICE_OUT, REPLIES_PER_GPU_SECOND, SECONDS_PER_MONTH, SPARE,
                         TOKENS_IN, TOKENS_OUT, TOKENS_PER_REPLY, break_even, case, commas, describe, dollars, percent, rates, tokens_words)

T = 300 * 10**9

print("per reply", TOKENS_IN, TOKENS_OUT, TOKENS_PER_REPLY)
print("replies per GPU second at the limit", round(REPLIES_PER_GPU_SECOND, 4), "headroom", HEADROOM)
per_reply, gpu_month, carry = rates(24)
print(f"one planned GPU carries {commas(round(carry))} replies = {tokens_words(carry * TOKENS_PER_REPLY)} tokens a month at 24 hours a day")
print(f"rent price of that: ${per_reply * carry:,.0f}; a GPU costs ${gpu_month:,.0f} a month; ratio {gpu_month / (per_reply * carry):.3f}")
print(f"owning can only win if each GPU is busy more than {gpu_month / (per_reply * carry / HEADROOM):.1%}")
print(f"hours a day below which a planned GPU rents for less than it costs: {24 * gpu_month / (per_reply * carry):.2f}")
print(f"rent per million tokens: ${float(PRICE_IN * TOKENS_IN + PRICE_OUT * TOKENS_OUT) / TOKENS_PER_REPLY:.4f}")

print("\nthe opening case by hours a day")
for h in (24, 18, 16, 12, 8, 6, 4):
    c = case(T, h)
    x = break_even(h)
    print(f"{h:>2} h  replies {commas(c['replies'])}  rent ${commas(dollars(c['rent']))}  gpus {c['gpus']}  own ${commas(dollars(c['own']))}  busy {c['busy']:.4f} {percent(c['busy'])}  "
          f"own/rent {float(c['own'] / c['rent']):.2f}  per M own {float(c['own_per_million']):.4f} rent {float(c['rent_per_million']):.4f}  "
          f"cross {tokens_words(x['tokens']) + ' gpus ' + str(x['gpus']) + ' own $' + commas(dollars(x['own'])) if x else None}")

print("\nbusy share at the crossing, 24 h")
x = break_even(24)
from rent_vs_own import busy_share
print(x, busy_share(x["replies"], x["gpus"]), percent(busy_share(x["replies"], x["gpus"])))
c250 = case(250 * 10**9, 24)
c400 = case(400 * 10**9, 24)
for c in (c250, c400):
    print(commas(c["tokens"]), "rent", dollars(c["rent"]), "gpus", c["gpus"], "own", dollars(c["own"]), "busy", percent(c["busy"]))

print("\npeople cost, 24 h")
for p in (0, 5_000, 10_000, 20_000, 30_000):
    x = break_even(24, people=p)
    print(p, tokens_words(x["tokens"]), x["gpus"], dollars(x["own"]), percent(busy_share(x["replies"], x["gpus"])))

print("\nspare, 24 h")
for s in (0, 1, 2):
    x = break_even(24, spare=s)
    print("spare", s, tokens_words(x["tokens"]), x["gpus"])

print("\nprices")
for pi, po in ((Decimal("0.10"), Decimal("0.50")), (Decimal(2), Decimal(10))):
    x = break_even(24, pi, po)
    c = case(T, 24, price_in=pi, price_out=po)
    print(pi, po, tokens_words(x["tokens"]), x["gpus"], "at 300B rent", dollars(c["rent"]), "own", dollars(c["own"]))
for gh in (Decimal("2.0"), Decimal("4.0")):
    x = break_even(24, gpu_hour=gh)
    print("gpu hour", gh, tokens_words(x["tokens"]) if x else None, x["gpus"] if x else None)

print("\nthe opening widget state")
d = describe(**OPENING)
print(d["headline"]); print(d["verdict"]); print(d["detail"]); print(d["chart"]["caption"])
print("\n4 h widget state")
d = describe(**{**OPENING, "hours": 4})
print(d["headline"]); print(d["verdict"]); print(d["detail"])
print(DAYS, HOURS_PER_MONTH, SECONDS_PER_MONTH, GPU_HOUR, PEOPLE, SPARE)
