#!/usr/bin/env python3
"""Build the Research & Validation evidence charts from data/rv-evidence.csv.

Reads the verified-figure CSV and writes self-contained SVG charts (no scripts, no external assets)
to assets/rv-charts/, a wide variant (<id>.svg) and a narrow mobile variant (<id>-m.svg) per chart,
plus assets/rv-charts/summary.json. Output is deterministic: rows are processed in CSV order,
numbers are formatted from the CSV text, and nothing time-dependent is written.

Usage: python3 scripts/build-rv-charts.py   (run from the repository root)
"""
import csv
import html
import json
import math
import os
import sys

ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
CSV_PATH = os.path.join(ROOT, "data", "rv-evidence.csv")
OUT = os.path.join(ROOT, "assets", "rv-charts")

# Page theme tokens (research-validation.html :root) and the Okabe-Ito colorblind-safe palette.
BG = "#0a1628"
TEXT = "#ffffff"
MUTED = "#ffffffb3"
DIM = "#ffffff8c"
GRID = "#ffffff1f"
PALETTE = ["#56B4E9", "#E69F00", "#009E73", "#F0E442", "#CC79A7", "#D55E00", "#0072B2"]
FONT = "Chakra Petch, monospace"

LEVELS = ["meta-analysis", "RCT", "cohort/case-control", "case series/clinical database", "book/expert opinion"]


def esc(s):
    return html.escape(str(s), quote=True)


def num(s):
    s = (s or "").strip()
    return float(s) if s not in ("", "NA") else None


def load_rows():
    with open(CSV_PATH, newline="", encoding="utf-8") as f:
        return list(csv.DictReader(f))


def svg_open(w, h, title):
    return [
        f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 {w} {h}" width="{w}" height="{h}" role="img" aria-label="{esc(title)}" font-family="{FONT}">',
        f"<title>{esc(title)}</title>",
        f'<rect x="0" y="0" width="{w}" height="{h}" fill="{BG}"/>',
    ]


def axis_title(out, chart, narrow, y=18):
    """Draw the (wrapped) axis title; return the extra height used beyond one line."""
    lines = wrap(chart["axis"], 56 if narrow else 110)
    for i, ln in enumerate(lines):
        out.append(text(8, y + i * 13, ln, 10 if narrow else 11, MUTED))
    return (len(lines) - 1) * 13


def text(x, y, s, size=12, fill=TEXT, anchor="start", weight="400"):
    return f'<text x="{x:.1f}" y="{y:.1f}" font-size="{size}" fill="{fill}" text-anchor="{anchor}" font-weight="{weight}">{esc(s)}</text>'


def wrap(s, width_chars):
    words, lines, cur = str(s).split(), [], ""
    for w in words:
        if cur and len(cur) + 1 + len(w) > width_chars:
            lines.append(cur)
            cur = w
        else:
            cur = (cur + " " + w).strip()
    if cur:
        lines.append(cur)
    return lines


def nice_ticks(lo, hi, n=5):
    span = hi - lo
    if span <= 0:
        return [lo]
    raw = span / n
    mag = 10 ** math.floor(math.log10(raw))
    step = min(m * mag for m in (1, 2, 2.5, 5, 10) if m * mag >= raw)
    k = math.ceil(lo / step - 1e-9)
    ticks = []
    while k * step <= hi + step * 1e-9:
        ticks.append(round(k * step, 10))
        k += 1
    return ticks


def fmt_tick(v):
    return f"{v:g}"


def forest(chart, rows, narrow):
    """Effect sizes with CIs, grouped by the chart's group field, null line marked.
    Narrow variant: each label sits on its own line above its interval."""
    w = 360 if narrow else 720
    label_w = 0 if narrow else 300
    row_h = 40 if narrow else 26
    groups = []
    for r in rows:
        g = r.get(chart.get("group_by", ""), "") if chart.get("group_by") else ""
        if not groups or groups[-1][0] != g:
            groups.append((g, []))
        groups[-1][1].append(r)
    out_head = []
    extra = axis_title(out_head, chart, narrow)
    top, bottom = 34 + extra, 46
    n_rows = sum(len(g[1]) for g in groups)
    n_groups = sum(1 for g in groups if g[0])
    h = top + n_rows * row_h + n_groups * 24 + bottom
    vals = [num(r["value"]) for r in rows] + [num(r["ci_low"]) for r in rows if num(r["ci_low"]) is not None] + [num(r["ci_high"]) for r in rows if num(r["ci_high"]) is not None]
    null = chart["null"]
    lo, hi = min(vals + [null]), max(vals + [null])
    pad = (hi - lo) * 0.08 or 0.1
    lo, hi = lo - pad, hi + pad
    x0, x1 = (12 if narrow else label_w + 10), w - 16
    sx = lambda v: x0 + (v - lo) / (hi - lo) * (x1 - x0)
    out = svg_open(w, h, chart["title"]) + out_head
    for tk in nice_ticks(lo, hi, 4 if narrow else 6):
        out.append(f'<line x1="{sx(tk):.1f}" y1="{top - 6}" x2="{sx(tk):.1f}" y2="{h - bottom + 4}" stroke="{GRID}"/>')
        out.append(text(sx(tk), h - bottom + 18, fmt_tick(tk), 10, DIM, "middle"))
    out.append(f'<line x1="{sx(null):.1f}" y1="{top - 8}" x2="{sx(null):.1f}" y2="{h - bottom + 4}" stroke="{MUTED}" stroke-dasharray="4 3"/>')
    out.append(text(sx(null), h - bottom + 34, f"no effect ({fmt_tick(null)})", 10, MUTED, "middle"))
    y = top
    for g, grows in groups:
        if g:
            out.append(text(8, y + 15, g, 11, PALETTE[1], weight="600"))
            y += 24
        for r in grows:
            v, lo_ci, hi_ci = num(r["value"]), num(r["ci_low"]), num(r["ci_high"])
            color = PALETTE[0] if r.get("significant", "") != "no" else DIM
            val = r["value"] + (f" [{r['ci_low']}, {r['ci_high']}]" if lo_ci is not None else "")
            if narrow:
                out.append(text(8, y + 12, r["label"][:62], 10, TEXT))
                cy = y + 27
            else:
                lines = wrap(r["label"], 44)[:2]
                for li, ln in enumerate(lines):
                    out.append(text(8, y + row_h * 0.5 + (li - (len(lines) - 1) / 2) * 11 + 4, ln, 11, TEXT))
                cy = y + row_h / 2
            if lo_ci is not None and hi_ci is not None:
                out.append(f'<line x1="{sx(lo_ci):.1f}" y1="{cy:.1f}" x2="{sx(hi_ci):.1f}" y2="{cy:.1f}" stroke="{color}" stroke-width="2"/>')
            out.append(f'<rect x="{sx(v) - 4:.1f}" y="{cy - 4:.1f}" width="8" height="8" fill="{color}"/>')
            right = sx(hi_ci if hi_ci is not None else v) + 6
            est = len(val) * (5.2 if narrow else 5.8)
            if right + est <= w - 4:
                out.append(text(right, cy + 3.5, val, 9 if narrow else 10, MUTED))
            elif narrow:
                out.append(text(w - 8, y + 12, val, 9, MUTED, "end"))
            else:
                out.append(text(sx(lo_ci if lo_ci is not None else v) - 6, cy + 3.5, val, 10, MUTED, "end"))
            y += row_h
    out.append("</svg>")
    return "\n".join(out)


def hbar(chart, rows, narrow):
    """Horizontal bars for percentages or counts, direct-labelled."""
    w = 360 if narrow else 720
    label_w = 0 if narrow else 280
    row_h = 44 if narrow else 30
    head = []
    extra = axis_title(head, chart, narrow)
    top, bottom = 30 + extra, 36
    h = top + len(rows) * row_h + bottom
    vmax = chart.get("max") or max(num(r["value"]) for r in rows)
    x0, x1 = (8 if narrow else label_w + 10), w - (110 if narrow else 130)
    sx = lambda v: x0 + v / vmax * (x1 - x0)
    out = svg_open(w, h, chart["title"]) + head
    for t in nice_ticks(0, vmax, 4):
        out.append(f'<line x1="{sx(t):.1f}" y1="{top - 4}" x2="{sx(t):.1f}" y2="{h - bottom + 2}" stroke="{GRID}"/>')
        out.append(text(sx(t), h - bottom + 16, fmt_tick(t), 10, DIM, "middle"))
    for i, r in enumerate(rows):
        y = top + i * row_h
        v = num(r["value"])
        color = PALETTE[0] if r.get("significant", "") != "no" else DIM
        if narrow:
            out.append(text(8, y + 12, r["label"][:48], 10, TEXT))
            by, bh = y + 17, 14
        else:
            for li, ln in enumerate(wrap(r["label"], 40)[:2]):
                out.append(text(8, y + row_h / 2 + 4 + (li - (min(2, len(wrap(r['label'], 40))) - 1) / 2) * 11, ln, 11, TEXT))
            by, bh = y + 7, row_h - 14
        out.append(f'<rect x="{x0}" y="{by:.1f}" width="{max(1.0, sx(v) - x0):.1f}" height="{bh}" fill="{color}"/>')
        suffix = "%" if r["metric_type"] == "%" else ""
        ci = f" [{r['ci_low']}–{r['ci_high']}]" if num(r["ci_low"]) is not None else ""
        out.append(text(sx(v) + 5, by + bh - 3, f"{r['value_display'] or r['value']}{suffix}{ci}", 10, TEXT))
    out.append("</svg>")
    return "\n".join(out)


def yearbars(chart, rows, narrow):
    """One bar per year, direct-labelled (multi-year series)."""
    w = 360 if narrow else 720
    head = []
    extra = axis_title(head, chart, narrow)
    top, bottom, left = 40 + extra, 40, 14
    h = (240 if narrow else 260) + extra
    vmax = max(num(r["value"]) for r in rows)
    n = len(rows)
    slot = (w - left * 2) / n
    bw = slot * 0.6
    sy = lambda v: (h - bottom) - v / vmax * (h - bottom - top)
    out = svg_open(w, h, chart["title"]) + head
    out.append(f'<line x1="{left}" y1="{h - bottom}" x2="{w - left}" y2="{h - bottom}" stroke="{GRID}"/>')
    for i, r in enumerate(rows):
        v = num(r["value"])
        x = left + i * slot + (slot - bw) / 2
        out.append(f'<rect x="{x:.1f}" y="{sy(v):.1f}" width="{bw:.1f}" height="{(h - bottom) - sy(v):.1f}" fill="{PALETTE[0]}"/>')
        suffix = "%" if r["metric_type"] == "%" else ""
        out.append(text(x + bw / 2, sy(v) - 5, f"{r['value_display'] or r['value']}{suffix}", 9 if narrow else 11, TEXT, "middle"))
        out.append(text(x + bw / 2, h - bottom + 16, r["year"], 10, MUTED, "middle"))
    out.append("</svg>")
    return "\n".join(out)


def evidence_map(chart, rows, narrow):
    """Every source by evidence level (bands) and year (x); each source on its own sub-row.
    Filled circle = peer-reviewed, open square = not peer-reviewed."""
    w = 360 if narrow else 720
    label_w = 96 if narrow else 180
    step = 17 if narrow else 16
    head = []
    extra = axis_title(head, chart, narrow)
    top, bottom, pad = 30 + extra, 52, 10
    bands = [(lvl, sorted([r for r in rows if r["evidence_level"] == lvl], key=lambda r: (int(r["year"]), r["source_short"]))) for lvl in LEVELS]
    h = top + sum(len(b) * step + pad * 2 for _, b in bands) + bottom
    years = [int(r["year"]) for r in rows]
    lo, hi = (min(years) // 10) * 10, (max(years) // 10 + 1) * 10
    x0, x1 = label_w + 8, w - 14
    sx = lambda v: x0 + (v - lo) / (hi - lo) * (x1 - x0)
    out = svg_open(w, h, chart["title"]) + head
    y = top
    for lvl, band in bands:
        bh = len(band) * step + pad * 2
        for li, ln in enumerate(wrap(lvl, 13 if narrow else 22)[:3]):
            out.append(text(8, y + pad + 10 + li * 12, ln, 10 if narrow else 11, PALETTE[1], weight="600"))
        for i, r in enumerate(band):
            cx = sx(int(r["year"]))
            cy = y + pad + i * step + step / 2
            if r["peer_reviewed"] == "yes":
                out.append(f'<circle cx="{cx:.1f}" cy="{cy:.1f}" r="4.5" fill="{PALETTE[0]}"/>')
            else:
                out.append(f'<rect x="{cx - 4.5:.1f}" y="{cy - 4.5:.1f}" width="9" height="9" fill="none" stroke="{PALETTE[3]}" stroke-width="2"/>')
            lab = r["source_short"]
            est = len(lab) * (5.4 if narrow else 6.0)
            if cx + 8 + est <= w - 4:
                out.append(text(cx + 8, cy + 3.5, lab, 9 if narrow else 10, TEXT))
            else:
                out.append(text(cx - 8, cy + 3.5, lab, 9 if narrow else 10, TEXT, "end"))
        y += bh
        out.append(f'<line x1="8" y1="{y:.1f}" x2="{x1}" y2="{y:.1f}" stroke="{GRID}"/>')
    for yr in range(lo, hi + 1, 20 if narrow else 10):
        out.append(f'<line x1="{sx(yr):.1f}" y1="{top}" x2="{sx(yr):.1f}" y2="{y:.1f}" stroke="{GRID}"/>')
        out.append(text(sx(yr), y + 14, str(yr), 10, DIM, "middle"))
    out.append(text((x0 + x1) / 2, y + 28, "Publication year", 10, MUTED, "middle"))
    ly = h - 12
    out.append(f'<circle cx="{x0 + 5}" cy="{ly - 4}" r="4.5" fill="{PALETTE[0]}"/>')
    out.append(text(x0 + 14, ly, "peer-reviewed", 10, MUTED))
    out.append(f'<rect x="{x0 + 110:.1f}" y="{ly - 8.5:.1f}" width="9" height="9" fill="none" stroke="{PALETTE[3]}" stroke-width="2"/>')
    out.append(text(x0 + 124, ly, "not peer-reviewed", 10, MUTED))
    out.append("</svg>")
    return "\n".join(out)


RENDER = {"forest": forest, "hbar": hbar, "yearbars": yearbars, "evidence_map": evidence_map}


def main():
    rows = load_rows()
    with open(os.path.join(ROOT, "data", "rv-charts.json"), encoding="utf-8") as f:
        charts = json.load(f)
    os.makedirs(OUT, exist_ok=True)
    summary = []
    for ch in charts:
        sel = [r for r in rows if r["chart"] == ch["id"]]
        if not sel:
            sys.exit(f"chart {ch['id']}: no rows")
        for narrow in (False, True):
            svg = RENDER[ch["type"]](ch, sel, narrow)
            name = ch["id"] + ("-m" if narrow else "") + ".svg"
            with open(os.path.join(OUT, name), "w", encoding="utf-8", newline="\n") as f:
                f.write(svg + "\n")
        summary.append({"id": ch["id"], "type": ch["type"], "title": ch["title"], "stream": sel[0]["stream"], "rows": len(sel),
                        "sources": sorted({r["source_short"] for r in sel})})
    with open(os.path.join(OUT, "summary.json"), "w", encoding="utf-8", newline="\n") as f:
        json.dump({"charts": summary, "rows_total": len(rows)}, f, indent=2, ensure_ascii=False)
        f.write("\n")
    print(f"{len(summary)} charts, {len(rows)} rows -> {os.path.relpath(OUT, ROOT)}")


if __name__ == "__main__":
    main()
