# Reproduce every number in the story from the two CSVs in this folder.
#   python3 compute.py
import csv, os

HERE = os.path.dirname(os.path.abspath(__file__))
usd = {}
for r in csv.DictReader(open(os.path.join(HERE, "pce-lines.csv"))):
    usd[int(r["line"]), int(r["year"])] = float(r["million_usd"]) * 1e6
pop = int(next(csv.DictReader(open(os.path.join(HERE, "us-population.csv"))))["population"])
OUT = (214, 215, 216, 217)  # cinema, live entertainment excl. sports, spectator sports, museums and libraries
out = lambda y: sum(usd[l, y] for l in OUT)
share = lambda v, y: 100 * v / usd[1, y]

y = 2025
print(f"2025 gambling ${usd[226, y] / 1e9:.1f}bn, ${usd[226, y] / pop:.0f} a head")
print(f"2025 going out ${out(y) / 1e9:.1f}bn, ${out(y) / pop:.0f} a head; ratio {usd[226, y] / out(y):.2f}")
print(f"2025 alcohol ${(usd[99, y] + usd[245, y]) / pop:.0f} a head; casinos' share of gambling {usd[227, y] / usd[226, y]:.0%}")
for line, name in [(127, "games, toys, hobbies"), (141, "tobacco"), (212, "amusement parks"), (211, "gyms"), (219, "cable TV"), (231, "pet services"), (224, "video streaming"), (60, "books"), (215, "live shows"), (216, "spectator sport"), (217, "museums"), (214, "cinema")]:
    print(f"2025 {name} ${usd[line, y] / pop:.0f} a head")
years = sorted({yr for (_, yr) in usd})
cross = next(yr for yr in years if usd[226, yr] > out(yr))
assert all(usd[226, yr] > out(yr) for yr in years if yr >= cross)
print(f"gambling ahead of going out every year from {cross}")
for yr in (1959, 1980, 1981, 2000, 2006, 2018, 2019, 2020, 2025):
    print(f"{yr} share: gambling {share(usd[226, yr], yr):.2f}%, going out {share(out(yr), yr):.2f}%")
print(f"gambling 2018 ${usd[226, 2018] / 1e9:.1f}bn; growth to 2025 {usd[226, 2025] / usd[226, 2018] - 1:.0%}; all spending {usd[1, 2025] / usd[1, 2018] - 1:.0%}")
