#!/usr/bin/env python3
import argparse,json,textwrap
from pathlib import Path
import xml.etree.ElementTree as ET
NS="http://www.w3.org/2000/svg"
ET.register_namespace("",NS)
def render(data):
    if not isinstance(data,dict) or set(data)!={"title","claim","boundary"}:
        raise ValueError("expected exactly title, claim, boundary")
    if not all(isinstance(v,str) and 1<=len(v)<=240 for v in data.values()):
        raise ValueError("each field must be a string of 1 to 240 characters")
    lines=[(key,line) for key in ("title","claim","boundary") for line in textwrap.wrap(data[key],35)]
    height=80+len(lines)*32
    root=ET.Element("{"+NS+"}svg",{"viewBox":f"0 0 800 {height}","role":"img"})
    ET.SubElement(root,"{"+NS+"}rect",{"width":"800","height":str(height),"rx":"18","fill":"#f5f8fc"})
    for i,(key,line) in enumerate(lines):
        t=ET.SubElement(root,"{"+NS+"}text",{"x":"28","y":str(48+i*32),"font-family":"Arial,sans-serif","font-size":"20","fill":"#172b46","data-field":key})
        t.text=line
    return ET.tostring(root,encoding="unicode")+"\n"
def main():
    p=argparse.ArgumentParser(description="Render one SVG; refuses overwrites")
    p.add_argument("--input",type=Path,required=True);p.add_argument("--output",type=Path,required=True)
    a=p.parse_args();result=render(json.loads(a.input.read_text(encoding="utf-8")))
    with a.output.open("x",encoding="utf-8") as f:f.write(result)
    print(json.dumps({"output":str(a.output),"format":"svg"}))
if __name__=="__main__":main()
