"""hamlet1514.py — build the poidh #1514 claim site from saved Hamlet search results.

  .venv-ausca/bin/python -I tools/hamlet1514.py
Reads research/hamlet/searches_run35_{a,b}.json (search cards, as the page showed them) and meetings_run35.json (each
meeting page's own summary). Every quote is copied from the saved card text, never retyped. Writes
drafts/poidh1514/site/: moments.csv, moments.json, timeline.svg, timeline.png.
"""
import csv, datetime, json, os, re, subprocess

VAULT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
R = os.path.join(VAULT, "research", "hamlet"); OUT = os.path.join(VAULT, "drafts", "poidh1514", "site")
M = "https://www.myhamlet.com/meetings/"

def load(f):
    d = json.loads(open(os.path.join(R, f)).read())
    return json.loads(d) if isinstance(d, str) else d

snip, head = {}, {}
for f in ("searches_run35_a.json", "searches_run35_b.json", "searches_run35_c.json"):
    for q, v in load(f).items():
        for c in v["cards"]:
            for href, s in c["moments"]:
                k = href.replace(M, "")
                snip.setdefault(k, s); head.setdefault(k, c["head"])
meet = load("meetings_run35.json")

# group, key (meeting?t=seconds), date, place, state, body, how the data-center link is shown, note
ROWS = [
 ("who", "104379?t=692",   "2025-07-14", "Hammond", "IN", "City Council", "snippet", ""),
 ("cant", "50461?t=2531",  "2025-07-16", "Hancock County", "IN", "County Commissioners", "snippet", ""),
 ("why", "38788?t=10696",  "2025-07-15", "Pima County", "AZ", "Board of Supervisors", "snippet", "Pima County is home of the Project Blue data center fight"),
 ("cant", "54224?t=18229", "2025-10-07", "King George County", "VA", "Board of Supervisors", "result of a data-center end-user query", ""),
 ("who", "54083?t=7416",   "2025-11-19", "San Marcos", "TX", "Neighborhood Commission", "snippet", "the applicant is the landowner, not the operator"),
 ("cant", "41601?t=3724",  "2025-12-16", "DeKalb County", "GA", "Board of Commissioners", "meeting summary", "public comment"),
 ("cant", "54092?t=2688",  "2026-01-13", "San Marcos", "TX", "Planning & Zoning Commission", "meeting summary", ""),
 ("cant", "10786?t=850",   "2026-01-14", "Page", "AZ", "Council", "same meeting (data center water use, t=1275)", "public comment"),
 ("cant", "9747?t=2310",   "2026-01-21", "Phoenix", "AZ", "City Council", "meeting summary", ""),
 ("who", "44315?t=591",    "2026-02-25", "Bastrop", "LA", "City Council", "snippet", "project names vs company name"),
 ("who", "35415?t=4973",   "2026-03-05", "Joliet", "IL", "Plan Commission", "meeting summary", ""),
 ("who", "35415?t=13897",  "2026-03-05", "Joliet", "IL", "Plan Commission", "meeting summary", ""),
 ("not", "68463?t=13874",  "2026-05-04", "Cottage Grove", "WI", "Village Board", "snippet (cut at 'with any da')", ""),
 ("not", "94958?t=6821",   "2026-07-27", "Hancock County", "IN", "Plan Commission (Data Center Advisory Committee)", "meeting summary", ""),
 ("not", "95073?t=9023",   "2026-08-06", "Nye County", "NV", "Water District Governing Board", "meeting summary", ""),
 ("why", "107062?t=8850",  "2026-08-18", "Chesapeake", "VA", "City Council", "meeting summary", ""),
 ("cant", "101797?t=248",  "2026-08-26", "Schuylkill County", "PA", "Commissioners", "meeting summary", "public comment, citing Spotlight PA"),
 ("why", "103349?t=13169", "2026-09-08", "North Port", "FL", "City Commission", "meeting summary", ""),
 ("not", "103349?t=13383", "2026-09-08", "North Port", "FL", "City Commission", "meeting summary", "commission directed staff to draft an ordinance eliminating NDAs (Hamlet summary)"),
 ("not", "110216?t=1569",  "2026-09-22", "Bow", "NH", "Board of Selectmen", "meeting summary", ""),
 ("not", "118478?t=7608",  "2026-10-01", "Bow", "NH", "Planning Board", "same town, 9 days after the AI data center hearing", "the same clip is also filed under meeting 118488"),
 ("not", "114242?t=521",   "2026-10-06", "Washington County", "OR", "Board of County Commissioners", "meeting summary", ""),
 ("not", "119207?t=13242", "2026-10-12", "Salem", "OR", "City Council", "meeting summary", "council banned data-center NDAs on 14 Sep (PolitiFact); Hamlet's summary says the policy was adopted here"),
 ("not", "119207?t=13583", "2026-10-12", "Salem", "OR", "City Council", "meeting summary", ""),
 ("cant", "120379?t=20378","2026-10-20", "Santa Clara", "CA", "City Council", "snippet", ""),
 ("not", "120379?t=21601", "2026-10-20", "Santa Clara", "CA", "City Council", "meeting summary", ""),
]
LABEL = {"who": "Who is it?", "cant": "We can't say", "why": "Why the NDA", "not": "Not here"}

rows = []
for g, k, date, place, st, body, link, note in ROWS:
    s = snip[k]; mid = k.split("?")[0]
    assert date == datetime.datetime.strptime(re.search(r"([A-Z][a-z]+ \d{1,2}, \d{4})", meet[mid]["title"]).group(1), "%B %d, %Y").date().isoformat(), (k, meet[mid]["title"])
    rows.append(dict(group=LABEL[g], date=date, place=place, state=st, body=body, quote=s, url=M + k,
                     datacenter_link=link, hamlet_summary=meet[mid].get("summary", ""), note=note))
json.dump(rows, open(os.path.join(OUT, "moments.json"), "w"), indent=1, ensure_ascii=False)
with open(os.path.join(OUT, "moments.csv"), "w", newline="") as f:
    w = csv.DictWriter(f, fieldnames=list(rows[0])); w.writeheader(); w.writerows(rows)

# ---- timeline: one lane per group, one dot per place-meeting, Jul 2025 .. Oct 2026; labels stacked so none overlap
W, H = 1600, 1180; X0, X1 = 150, 1540
t0, t1 = datetime.date(2025, 7, 1), datetime.date(2026, 11, 1)
def x(d): return X0 + (X1 - X0) * (datetime.date.fromisoformat(d) - t0).days / (t1 - t0).days
lanes = [("who", "WHO IS IT?", "residents and officials ask who the buyer is"),
         ("cant", "WE CAN'T SAY", "an NDA keeps the name or the deal quiet"),
         ("why", "WHY THE NDA", "site selectors want out of public-records law"),
         ("not", "NOT HERE", "boards refuse, ban or limit data-center NDAs")]
GAP = 215; Y = {g: 330 + i * GAP for i, (g, _, _) in enumerate(lanes)}
INK, RED, MID, GRID = "#111111", "#c8102e", "#7a7a7a", "#dcd8cf"
def esc(t): return t.replace("&", "&amp;").replace("<", "&lt;")
o = [f'<svg xmlns="http://www.w3.org/2000/svg" width="{W}" height="{H}" viewBox="0 0 {W} {H}" font-family="Helvetica Neue, Helvetica, Arial, sans-serif">',
     f'<rect width="{W}" height="{H}" fill="#f6f4ef"/>',
     f'<text x="60" y="92" font-size="56" font-weight="700" fill="{INK}">&#8220;This is a public body.&#8221;</text>',
     f'<text x="60" y="140" font-size="27" fill="{INK}">How the data-center NDA went from &#8220;standard practice&#8221; to banned, meeting by meeting</text>',
     f'<text x="60" y="178" font-size="21" fill="{MID}">{len(rows)} moments from {len({r["url"].split("?")[0] for r in rows})} local meetings in {len({r["place"] for r in rows})} places and {len({r["state"] for r in rows})} states, found with Hamlet (myhamlet.com) search, each linked to the second it was said.</text>']
wv = [r for r in rows if r["group"] == "Not here" and r["date"] >= "2026-07-01"]
WAVE = (len({(r["place"], r["body"]) for r in wv}), len({r["place"] for r in wv}), len({r["state"] for r in wv}))
wave = x("2026-07-01"); top, bot = 225, Y["not"] + 95
o.append(f'<rect x="{wave:.1f}" y="{top}" width="{X1 - wave:.1f}" height="{bot - top}" fill="#f3e3e3"/>')
months = [datetime.date(2025 + (6 + i) // 12, (6 + i) % 12 + 1, 1) for i in range(17)]
for m in months:
    xx = x(m.isoformat())
    o.append(f'<line x1="{xx:.1f}" y1="{top}" x2="{xx:.1f}" y2="{bot}" stroke="{GRID}" stroke-width="1"/>')
    if m.month in (1, 4, 7, 10):
        o.append(f'<text x="{xx + 5:.1f}" y="{top - 8}" font-size="18" fill="{MID}">{m.strftime("%b %Y")}</text>')
o.append(f'<text x="{X1}" y="{bot + 30}" font-size="20" font-weight="700" text-anchor="end" fill="{RED}">Jul&#8211;Oct 2026: {WAVE[0]} boards in {WAVE[1]} places and {WAVE[2]} states move against them</text>')
FS, CW, LV = 15.5, 8.4, 21          # label font size, average char width, level height
for g, name, sub in lanes:
    y = Y[g]; col = RED if g == "not" else INK
    o.append(f'<text x="60" y="{y - 92}" font-size="20" font-weight="700" fill="{col}">{name}</text>')
    o.append(f'<text x="{60 + len(name) * 13.6 + 14:.0f}" y="{y - 92}" font-size="18" fill="{MID}">{sub}</text>')
    o.append(f'<line x1="{X0}" y1="{y}" x2="{X1}" y2="{y}" stroke="{INK}" stroke-width="2"/>')
    pts, seen = [], set()
    for r in sorted((r for r in rows if r["group"] == LABEL[g]), key=lambda r: r["date"]):
        if (r["place"], r["date"]) in seen: continue
        seen.add((r["place"], r["date"])); pts.append(r)
    taken = {}                       # level -> list of (x0, x1) label spans
    order = [1, -1, 2, -2, 3, -3]    # positive = above the line
    for r in pts:
        xx = x(r["date"]); txt = f'{r["place"]}, {r["state"]}'; w = len(txt) * CW
        x0 = min(max(xx - w / 2, X0 - 40), X1 + 40 - w)
        lvl = next(l for l in order if all(x0 > b + 10 or x0 + w < a - 10 for a, b in taken.get(l, [])))
        taken.setdefault(lvl, []).append((x0, x0 + w))
        ly = y - 14 - (lvl - 1) * LV if lvl > 0 else y + 27 + (-lvl - 1) * LV
        if abs(lvl) > 1:
            o.append(f'<line x1="{xx:.1f}" y1="{y}" x2="{xx:.1f}" y2="{ly - 5 if lvl < 0 else ly + 5:.1f}" stroke="{MID}" stroke-width="1"/>')
        halo = "#f3e3e3" if xx >= wave else "#f6f4ef"
        o.append(f'<text x="{x0 + w / 2:.1f}" y="{ly:.1f}" font-size="{FS}" text-anchor="middle" fill="{INK}" stroke="{halo}" stroke-width="5" paint-order="stroke">{esc(txt)}</text>')
    for r in pts:
        o.append(f'<circle cx="{x(r["date"]):.1f}" cy="{y}" r="8.5" fill="{col}" stroke="#f6f4ef" stroke-width="2"/>')
o.append(f'<text x="60" y="{H - 40}" font-size="17" fill="{MID}">Data: Hamlet meeting search, 27 Oct 2026. Quotes are Hamlet&#8217;s own transcript snippets, unedited. Every moment, with its link and how it ties to a data center: moments.csv. By Assay, CC0.</text>')
o.append("</svg>")
open(os.path.join(OUT, "timeline.svg"), "w").write("\n".join(o))
png = os.path.join(OUT, "timeline.png")
subprocess.run(["rsvg-convert", "-w", str(W), "-o", png, os.path.join(OUT, "timeline.svg")], check=True)
subprocess.run(["magick", png, "-dither", "None", "-colors", "48", "-strip", "PNG8:" + png], check=True)   # flat art: 8-bit palette keeps it under 100 KiB
print("wave", WAVE, "png bytes", os.path.getsize(png))
print(len(rows), "rows;", len({(r['place'], r['state']) for r in rows}), "places;", len({r['state'] for r in rows}), "states;",
      len({r['url'].split('?')[0] for r in rows}), "meetings")
# ---- report: fill quotes and links from the saved cards, then render index.html
import html as _h, shutil, markdown
queries = []
for f in ("searches_run35_a.json", "searches_run35_b.json", "searches_run35_c.json"):
    queries += list(load(f))
def ts(k):
    t = int(k.split("t=")[1]); return f"{t // 3600}:{t % 3600 // 60:02d}:{t % 60:02d}" if t >= 3600 else f"{t // 60}:{t % 60:02d}"
def where(k):
    m = re.match(r"(.+?), ([A-Z][a-z]+ \d{1,2}, \d{4}), (.+), ([A-Z]{2})", meet[k.split("?")[0]]["title"].replace(" · Hamlet", ""))
    body, date, place, st = m.groups()
    return f"{place}, {st}, {body}, {datetime.datetime.strptime(date, '%B %d, %Y').strftime('%-d %b %Y')}"
rep = open(os.path.join(VAULT, "drafts", "poidh1514", "report.md")).read()
rep = re.sub(r"\{\{Q:([^}]+)\}\}", lambda m: f"{snip[m.group(1)]} ({where(m.group(1))}, at {ts(m.group(1))}, [video]({M}{m.group(1)}))", rep)
rep = re.sub(r"\{\{L:([^}]+)\}\}", lambda m: f"[video at {ts(m.group(1))}]({M}{m.group(1)})", rep)
rep = rep.replace("{{QUERIES}}", "; ".join(f"`{q}`" for q in queries) + ".").replace("{{NREAD}}", str(len(snip)))
assert "{{" not in rep
open(os.path.join(OUT, "README.md"), "w").write(rep)
shutil.copy(os.path.abspath(__file__), os.path.join(OUT, "hamlet1514.py"))
CSS = ("body{max-width:860px;margin:40px auto;padding:0 20px;font:17px/1.6 Georgia,serif;color:#111;background:#f6f4ef}"
       "h1{font:700 44px/1.1 Helvetica Neue,Helvetica,Arial,sans-serif;margin:.2em 0}h2{font:700 22px Helvetica Neue,Helvetica,Arial,sans-serif;"
       "margin-top:2em;border-top:2px solid #111;padding-top:.5em}img{width:100%;height:auto;border:1px solid #ddd}a{color:#c8102e}"
       "code{font-size:14px;background:#ebe7de;padding:1px 4px}li{margin:.5em 0}")
body = markdown.markdown(rep, extensions=["extra"])
open(os.path.join(OUT, "index.html"), "w").write(f'<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" '
    f'content="width=device-width,initial-scale=1"><title>This is a public body: data-center NDAs in local meetings</title><style>{CSS}</style>'
    f'</head><body>{body}</body></html>')
print("report", len(rep), "chars; queries", len(queries))
for g in LABEL.values():
    print(g, sorted({f"{r['place']} {r['state']}" for r in rows if r['group'] == g}))
