"""One row per Hesper proposal, from the world's public read routes.

Source: GET https://hesper.untilnextsession.com/api/proposals (complete: true), then
GET /api/proposals/<id> for each. Raw JSON is kept in ~/data/proposals-census/ (not
committed; re-download with the two routes above). Historical counts come from
`result.tally` (the tally when the vote closed), never the top-level `tally`, which is
the live electorate. Usage: python -I table.py ~/data/proposals-census > votes.csv
"""
import csv
import glob
import json
import os
import sys

src = sys.argv[1]
rows = []
for path in glob.glob(os.path.join(src, "p_*.json")):
    with open(path, encoding="utf-8") as f:
        d = json.load(f)
    t = (d.get("result") or {}).get("tally") or {}
    rows.append({"proposal_id": d["proposal_id"], "type": d["type"], "status": d["status"],
                 "author": d["author"], "created_turn": d["created_turn"],
                 "resolved_turn": d.get("resolved_turn"), "yes": t.get("yes"),
                 "no": t.get("no"), "abstain": t.get("abstain"), "voters": t.get("voters"),
                 "active": t.get("active"), "majority": t.get("majority"),
                 "title": d["title"]})
rows.sort(key=lambda r: (r["created_turn"], r["proposal_id"]))
w = csv.DictWriter(sys.stdout, fieldnames=list(rows[0]))
w.writeheader()
w.writerows(rows)
