"""Render an SVG from two published regression coefficients. Python 3, no dependencies."""
import csv
from pathlib import Path
from xml.sax.saxutils import escape

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

chart = [
    '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 400 300" role="img" aria-labelledby="similarity-title similarity-desc">',
    '<title id="similarity-title">Similarity to other stories in the same group</title>',
    '<desc id="similarity-desc">Compared with no AI access, one starter available increased similarity by 0.871 points and up to five by 0.718 points.</desc>',
    '<rect width="400" height="300" rx="12" fill="#f8f1e8"/>',
    '<g fill="#29201a" font-family="Arial, sans-serif">',
    '<text x="18" y="35" font-size="23" font-weight="700">How much more alike?</text>',
    '<text x="18" y="62" font-size="18">Difference versus no AI access</text>',
    '<text x="18" y="83" font-size="18">on a 0 to 100 similarity scale</text>',
]
for row, y in zip(rows, (119, 179), strict=True):
    value = float(row["estimate_similarity_points"])
    width = 295 * value
    label = escape(row["condition"])
    n = int(row["writers"])
    chart.extend([
        f'<text x="18" y="{y}" font-size="18">{label} · {n} writers</text>',
        f'<rect x="18" y="{y + 11}" width="{width:.2f}" height="20" rx="3" fill="#176054"/>',
        f'<text x="{27 + width:.2f}" y="{y + 28}" font-size="18" font-weight="700">+{value:.3f}</text>',
    ])
chart.extend([
    '<line x1="18" x2="313" y1="226" y2="226" stroke="#594d44"/>',
    '<text x="18" y="248" font-size="17">0</text>',
    '<text x="313" y="248" font-size="17" text-anchor="end">+1 point</text>',
    '<text x="18" y="279" font-size="16">Doshi and Hauser (2024) · 293 writers</text>',
    '</g>',
    '</svg>',
])
print("\n".join(chart))
