#!/usr/bin/env python3
# SPDX-License-Identifier: Apache-2.0
"""Generate two original A4 SVG teaching aids and matching PDF exports."""
from __future__ import annotations

import subprocess
from pathlib import Path
from xml.sax.saxutils import escape

HERE = Path(__file__).resolve().parent


def label(x: float, y: float, value: str, size: float = 4.5, bold: bool = False) -> str:
    weight = 700 if bold else 400
    return f'<text x="{x}" y="{y}" font-size="{size}" font-weight="{weight}">{escape(value)}</text>'


def line(x1: float, y: float, x2: float) -> str:
    return f'<path d="M{x1} {y}H{x2}" stroke="#304c5e" stroke-width="0.38"/>'


def shell(title: str, desc: str, inner: str) -> str:
    return f'''<svg xmlns="http://www.w3.org/2000/svg" width="210mm" height="297mm" viewBox="0 0 210 297" role="img" aria-labelledby="title desc">
<title id="title">{escape(title)}</title><desc id="desc">{escape(desc)}</desc>
<rect width="210" height="297" fill="#fffefa"/>
<g font-family="DejaVu Sans, sans-serif" fill="#18384b">{inner}</g>
<text x="13" y="287" font-family="DejaVu Sans, sans-serif" font-size="3.4" fill="#18384b">SubjectNest · original classroom aid · CC BY 4.0 · neuroforge.io</text>
</svg>
'''


def sorter() -> str:
    rows = [
        ("REPORT", "Describes what a source shows", "Headings + grouped details + limit", "The drawing labels ..."),
        ("PROCEDURE", "Helps a reader do a task", "Materials + ordered actions", "First, fold ..."),
        ("VIEWPOINT", "Offers a view and a reason", "I think + because + possible request", "In my view ..."),
        ("STORY", "Imagines an event and change", "Character + setting + action", "Ivo revised his card."),
    ]
    out = [label(13, 17, "One plan, four text jobs", 8, True),
           label(13, 26, "Sort by purpose AND structure; a topic word alone is not enough.", 4.1),
           line(13, 32, 197)]
    for i, (heading, purpose, clue, sample) in enumerate(rows):
        y = 39 + i * 47
        out.append(f'<rect x="13" y="{y}" width="184" height="41" rx="3" fill="{["#f3f7f9", "#f1f8f4", "#fff7e9", "#f7f3fa"][i]}" stroke="#34576b" stroke-width="0.55"/>')
        out.extend([label(18, y+8, heading, 5.3, True), label(18, y+17, purpose, 4.2), label(18, y+25, clue, 3.9), label(18, y+34, sample, 4.1)])
    out += [label(13, 237, "My source: _____________________", 4.6, True),
            label(13, 249, "My chosen job and two clues:", 4.6, True), line(13, 259, 197),
            label(13, 270, "One thing the source cannot prove:", 4.6, True), line(13, 279, 197)]
    return shell("Source and purpose sorter", "Four labelled bands: report, procedure, viewpoint and story. Each has purpose, structure clue and example. Blank lines prompt a source choice and its limit. Full text and tactile build in TEXT-ALTERNATIVES.md.", "\n".join(out))


def planner() -> str:
    blocks = [
        ("1 · WHAT THE MODEL SHOWS", "Source label and exact detail; say 'the drawing shows'.", "#f3f7f9"),
        ("2 · WHAT SOMEONE THINKS", "Mark the speaker's view; do not turn it into a fact.", "#fff7e9"),
        ("3 · WHAT IS STILL UNKNOWN", "Name a limit and ask what evidence is needed.", "#f1f8f4"),
    ]
    out = [label(13, 17, "Model → evidence → useful question", 8, True),
           label(13, 26, "A summary a new reader can trust", 4.3), line(13, 32, 197)]
    for i, (head, prompt, colour) in enumerate(blocks):
        y = 40 + i * 70
        out.append(f'<rect x="13" y="{y}" width="184" height="62" rx="3" fill="{colour}" stroke="#34576b" stroke-width="0.55"/>')
        out += [label(18, y+10, head, 5.1, True), label(18, y+18, prompt, 3.75)]
        out += [line(18, y+30, 192), line(18, y+42, 192), line(18, y+54, 192)]
    out += [label(13, 257, "My short summary: topic → detail → limit", 4.5, True), line(13, 269, 197), line(13, 280, 197)]
    return shell("Model evidence planner", "Three large numbered boxes distinguish source-supported model detail, someone's viewpoint and what remains unknown. Two lines for a short summary. Full text and tactile build in TEXT-ALTERNATIVES.md.", "\n".join(out))


def main() -> None:
    for stem, content in (("source-purpose-sorter", sorter()), ("model-evidence-planner", planner())):
        svg = HERE / f"{stem}.svg"
        svg.write_text(content, encoding="utf-8")
        subprocess.run(["rsvg-convert", "-f", "pdf", "-o", str(svg.with_suffix(".pdf")), str(svg)], check=True)
        print(svg.name, svg.with_suffix(".pdf").name)


if __name__ == "__main__":
    main()
