"""Rebuild the SVG with Python 3. Source values are transcribed from section 4.6."""
import csv
from html import escape
from pathlib import Path

base = Path(__file__).resolve().with_suffix("")
with base.with_suffix(".csv").open(encoding="utf-8", newline="") as handle:
    rows = list(csv.DictReader(handle))

svg = [
    '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 600 352" role="img" aria-labelledby="title desc">',
    '<title id="title">Basketball feedback packages, 14 days later</title>',
    '<desc id="desc">Two separate scales show adjusted differences and 95 percent confidence intervals in the university trial.</desc>',
    '<rect width="600" height="352" rx="12" fill="#f4f1fa"/>',
    '<g fill="#1d1931" font-family="Arial,sans-serif">',
    '<text x="34" y="41" font-size="25" font-weight="700">Basketball feedback packages, 14 days later</text>',
    '<text x="34" y="70" font-size="20">AI feedback package minus instructor feedback package</text>',
]
for index, row in enumerate(rows):
    top = 112 + index * 120
    maximum = int(row["axis_max"])
    step = int(row["axis_step"])
    positions = {field: 35 + 530 * float(row[field]) / maximum
                 for field in ("estimate", "ci_low", "ci_high")}
    svg.extend([
        f'<text x="34" y="{top}" font-size="22" font-weight="700">{escape(row["outcome"])} · {escape(row["unit"])}</text>',
        f'<text x="34" y="{top + 27}" font-size="20">+{row["estimate"]} (95% CI {row["ci_low"]} to {row["ci_high"]})</text>',
        f'<path d="M35 {top + 48}H565" stroke="#9d90b2" stroke-width="2"/>',
        f'<path d="M{positions["ci_low"]:.2f} {top + 48}H{positions["ci_high"]:.2f}" stroke="#593582" stroke-width="5"/>',
        f'<circle cx="{positions["estimate"]:.2f}" cy="{top + 48}" r="8" fill="#593582"/>',
    ])
    for tick in range(0, maximum + 1, step):
        x = 35 + 530 * tick / maximum
        svg.append(f'<text x="{x:.2f}" y="{top + 75}" text-anchor="middle" font-size="18">{tick}</text>')
svg.extend([
    '<text x="34" y="330" font-size="18">Source: Dong and Zhang (2026) · 240 randomised, 229 tested</text>',
    '</g></svg>',
])
base.with_suffix(".svg").write_text("\n".join(svg) + "\n", encoding="utf-8")
