"""Build the post's SVG charts from data/*.csv. Standard library only.

    python3 blog/postal-codes-are-the-easy-part/src/make_charts.py

The world map uses Natural Earth 1:110m admin-0 countries (public domain),
downloaded once into src/.cache/ (git-ignored) from a pinned release.
"""

import csv
import hashlib
import json
import urllib.request
from collections import Counter
from html import escape
from pathlib import Path

HERE = Path(__file__).resolve().parent
POST = HERE.parent
DATA = POST / "data"
CACHE = HERE / ".cache"

NE_VERSION = "v5.1.2"
NE_URL = (f"https://raw.githubusercontent.com/nvkelso/natural-earth-vector/{NE_VERSION}"
          "/geojson/ne_110m_admin_0_countries.geojson")

FONT = "font-family='Inter, -apple-system, Segoe UI, Helvetica, Arial, sans-serif'"
INK, MUTED, GRID = "#1f2937", "#6b7280", "#e5e7eb"

STATUS = [  # key, label, colour
    ("postal_codes_published", "Postal codes on DataHub", "#1d4ed8"),
    ("open_source_not_yet_added", "Open source exists, not yet added", "#60a5fa"),
    ("closed_or_paid_source_only", "Codes exist, only closed or paid sources", "#f59e0b"),
    ("no_postcode_system", "No postal-code system", "#cbd5e1"),
]
COLOUR = {k: c for k, _, c in STATUS}


def read_csv(name):
    with open(DATA / name, encoding="utf-8") as f:
        return list(csv.DictReader(f))


def svg(width, height, body, title):
    return (f"<svg xmlns='http://www.w3.org/2000/svg' width='{width}' height='{height}' viewBox='0 0 {width} {height}' "
            f"role='img' aria-label='{escape(title, quote=True)}' {FONT}>"
            f"<title>{escape(title)}</title><rect width='100%' height='100%' fill='#ffffff'/>"
            f"{body}</svg>\n")


def text(x, y, s, size=13, fill=INK, anchor="start", weight="normal"):
    return (f"<text x='{x:.1f}' y='{y:.1f}' font-size='{size}' fill='{fill}' "
            f"text-anchor='{anchor}' font-weight='{weight}'>{escape(str(s))}</text>")


def legend(counts, y, fmt):
    """Two-row, two-column legend for the four statuses."""
    out = ""
    for i, (key, label, col) in enumerate(STATUS):
        x = 16 + (i % 2) * 470
        yy = y + (i // 2) * 22
        out += f"<rect x='{x}' y='{yy - 11}' width='12' height='12' rx='2' fill='{col}'/>"
        out += text(x + 18, yy, label + fmt.format(n=counts[key]), 13)
    return out


# --- 1. World map of postal-code coverage ---------------------------------

def natural_earth():
    CACHE.mkdir(exist_ok=True)
    path = CACHE / f"ne_110m_admin_0_countries_{NE_VERSION}.geojson"
    if not path.exists():
        req = urllib.request.Request(NE_URL, headers={"User-Agent": "datahub-blog-charts/0.1"})
        with urllib.request.urlopen(req, timeout=120) as r:
            path.write_bytes(r.read())
    print("natural earth sha256", hashlib.sha256(path.read_bytes()).hexdigest())
    return json.loads(path.read_text())


def ring_path(ring, w, h):
    pts = []
    for lon, lat in ring:
        x = (lon + 180) / 360 * w
        y = (90 - lat) / 180 * h
        pts.append(f"{x:.1f},{y:.1f}")
    return "M" + "L".join(pts) + "Z"


def world_map(countries):
    status = {r["country_code"]: r["postal_status"] for r in countries}
    geo = natural_earth()
    W, H, TOP = 960, 430, 20  # equirectangular, cropped below -58° latitude
    map_h = 480
    shapes = []
    for feat in geo["features"]:
        p = feat["properties"]
        iso = p.get("ISO_A2_EH") or p.get("ISO_A2")
        if p.get("ADMIN") == "Antarctica":
            continue
        fill = COLOUR.get(status.get(iso, ""), "#f3f4f6")
        g = feat["geometry"]
        polys = g["coordinates"] if g["type"] == "MultiPolygon" else [g["coordinates"]]
        d = "".join(ring_path(ring, W, map_h) for poly in polys for ring in poly)
        shapes.append(f"<path d='{d}' fill='{fill}' stroke='#ffffff' stroke-width='0.5'>"
                      f"<title>{escape(p.get('NAME', ''))}</title></path>")
    body = f"<g transform='translate(0,{TOP})'>{''.join(shapes)}</g>"
    # legend
    counts = Counter(r["postal_status"] for r in countries)
    body += legend(counts, H + 2, " ({n})")
    title = ("World map of postal-code coverage for the 249 ISO 3166 countries and territories: "
             f"{counts['postal_codes_published']} have postal codes on DataHub, "
             f"{counts['open_source_not_yet_added']} have an open source not yet added, "
             f"{counts['closed_or_paid_source_only']} have codes only from closed or paid sources, "
             f"{counts['no_postcode_system']} have no postal-code system.")
    return svg(W, H + 40, body, title)


# --- 2. Status bar ---------------------------------------------------------

def status_bar(countries):
    counts = Counter(r["postal_status"] for r in countries)
    total = sum(counts.values())
    W, H, X0, BW = 960, 150, 16, 928
    body = text(X0, 24, f"The {total} ISO 3166-1 countries and territories, by postal-code status",
                15, INK, weight="600")
    x = X0
    for key, label, col in STATUS:
        n = counts[key]
        w = BW * n / total
        body += f"<rect x='{x:.1f}' y='40' width='{w:.1f}' height='44' fill='{col}'/>"
        tc = "#ffffff" if key in ("postal_codes_published",) else INK
        if w > 40:
            body += text(x + w / 2, 68, n, 16, tc, "middle", "700")
        x += w
    body += legend(counts, 108, ": {n}")
    title = ", ".join(f"{label}: {counts[key]}" for key, label, _ in STATUS)
    return svg(W, H, body, f"Postal-code status of {total} countries. {title}.")


# --- 3. Level-1 divisions per country --------------------------------------

def level1_histogram(countries):
    bins = [(0, 0, "none"), (1, 9, "1–9"), (10, 19, "10–19"), (20, 49, "20–49"),
            (50, 99, "50–99"), (100, 10**6, "100+")]
    counts = []
    for lo, hi, lab in bins:
        counts.append(sum(1 for r in countries if lo <= int(r["level1_divisions"]) <= hi))
    W, H, X0, Y0, PH = 960, 330, 70, 50, 220
    peak = max(counts)
    bw = (W - X0 - 30) / len(bins)
    body = text(16, 24, "Countries by number of first-level divisions (ISO 3166-2)", 15, weight="600")
    for i in range(0, peak + 1, 20):
        y = Y0 + PH - PH * i / peak
        body += f"<line x1='{X0}' x2='{W - 20}' y1='{y:.1f}' y2='{y:.1f}' stroke='{GRID}'/>"
        body += text(X0 - 8, y + 4, i, 11, MUTED, "end")
    for j, ((lo, hi, lab), n) in enumerate(zip(bins, counts)):
        x = X0 + j * bw + 10
        h = PH * n / peak
        col = "#cbd5e1" if lo == 0 else "#1d4ed8"
        body += f"<rect x='{x:.1f}' y='{Y0 + PH - h:.1f}' width='{bw - 20:.1f}' height='{h:.1f}' fill='{col}' rx='2'/>"
        body += text(x + (bw - 20) / 2, Y0 + PH - h - 6, n, 13, INK, "middle", "700")
        body += text(x + (bw - 20) / 2, Y0 + PH + 18, lab, 12, MUTED, "middle")
    body += text(W / 2, H - 8, "first-level divisions per country", 12, MUTED, "middle")
    title = ("Bar chart of the 249 countries by number of first-level divisions: "
             + ", ".join(f"{lab}: {n}" for (_, _, lab), n in zip(bins, counts)) + ".")
    return svg(W, H, body, title)


# --- 4. Name coverage by language heatmap --------------------------------

def name_heatmap():
    rows = read_csv("name-coverage.csv")
    W, X0, Y0, CW, RH = 960, 330, 70, 200, 30
    levels = ["level1", "level2", "level3"]
    H = Y0 + RH * len(rows) + 50
    body = text(16, 24, "Share of divisions with a name in a second language, in Wikidata", 15, weight="600")
    body += text(16, 44, "joined on each country's official codes; measured 19 November 2026", 12, MUTED)
    for j, lv in enumerate(["Level 1", "Level 2", "Level 3"]):
        body += text(X0 + j * CW + CW / 2, Y0 - 8, lv, 12, MUTED, "middle", "600")

    def colour(v):
        t = v / 100
        r = int(239 + (29 - 239) * t); g = int(246 + (78 - 246) * t); b = int(255 + (216 - 255) * t)
        return f"rgb({r},{g},{b})"

    for i, r in enumerate(rows):
        y = Y0 + i * RH
        foreign = r["kind"] == "not a local language"
        label = f"{r['country']} · {r['language']}"
        body += text(X0 - 12, y + 20, label, 13, MUTED if foreign else INK, "end")
        for j, lv in enumerate(levels):
            v = r[f"{lv}_pct"]
            x = X0 + j * CW
            if v == "":
                body += f"<rect x='{x + 2}' y='{y + 2}' width='{CW - 4}' height='{RH - 4}' fill='#f9fafb' stroke='{GRID}'/>"
                body += text(x + CW / 2, y + 20, "no level in our data", 11, MUTED, "middle")
                continue
            v = float(v)
            body += f"<rect x='{x + 2}' y='{y + 2}' width='{CW - 4}' height='{RH - 4}' fill='{colour(v)}'/>"
            body += text(x + CW / 2, y + 20, f"{v:g}%", 13, "#ffffff" if v >= 55 else INK, "middle", "600")
        if foreign and (i == 0 or rows[i - 1]["kind"] != "not a local language"):
            body += f"<line x1='16' x2='{W - 16}' y1='{y}' y2='{y}' stroke='{MUTED}' stroke-dasharray='4 3'/>"
    body += text(16, H - 14, "Grey rows: a language that is not local to the country, for comparison. Source: Wikidata (CC0).", 11, MUTED)
    title = "Heatmap of the share of divisions with a name in a second language, by country, language and level. " + "; ".join(
        f"{r['country']} {r['language']}: " + ", ".join(f"L{k + 1} {r[f'{lv}_pct'] or 'n/a'}" for k, lv in enumerate(levels))
        for r in rows)
    return svg(W, H, body, title)


def main():
    countries = read_csv("countries.csv")
    out = {
        "coverage-map.svg": world_map(countries),
        "coverage-status.svg": status_bar(countries),
        "level1-divisions.svg": level1_histogram(countries),
        "name-coverage.svg": name_heatmap(),
    }
    for name, content in out.items():
        (POST / name).write_text(content, encoding="utf-8")
        print(name, len(content.encode()), "bytes")


if __name__ == "__main__":
    main()
