#!/usr/bin/env python3
"""Строит учебный SVG из study-observations.csv без сторонних библиотек."""
import csv
from html import escape
from pathlib import Path
root = Path(__file__).parent
with (root / "study-observations.csv").open(encoding="utf-8", newline="") as fh:
    rows = list(csv.DictReader(fh))
assert len(rows) == 6 and len({r["participant_id"] for r in rows}) == 6
parts = ['<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 700 430" role="img" aria-labelledby="title desc">',
         '<title id="title">Учебная диаграмма часов занятий и баллов</title>',
         '<desc id="desc">Шесть вымышленных наблюдений. Часы за неделю от 1 до 6; балл от 5 до 10.</desc>',
         '<rect width="700" height="430" fill="white"/>',
         '<path d="M90 35V340H650" fill="none" stroke="#334155" stroke-width="2"/>']
for h in range(0, 7):
    x = 90 + h * 88
    parts.append(f'<path d="M{x} 340v6" stroke="#334155"/><text x="{x}" y="367" text-anchor="middle" font-size="16">{h}</text>')
for score in range(0, 11, 2):
    y = 340 - score * 28
    parts.append(f'<path d="M85 {y}h565" stroke="#e2e8f0"/><text x="73" y="{y+5}" text-anchor="end" font-size="16">{score}</text>')
for r in rows:
    h = float(r["study_hours_per_week"]); score = float(r["quiz_score_points"])
    assert 0 <= h <= 6 and 0 <= score <= 10
    parts.append(f'<circle cx="{90+h*88:g}" cy="{340-score*28:g}" r="7" fill="#ea580c"><title>{escape(r["participant_id"])}: {h:g} ч/нед, {score:g} баллов</title></circle>')
parts += ['<text x="370" y="406" text-anchor="middle" font-size="17">Занятия, ч/нед</text>',
          '<text transform="translate(24 190) rotate(-90)" text-anchor="middle" font-size="17">Результат теста, баллы из 10</text>', '</svg>']
(root / "study-chart.svg").write_text("\n".join(parts) + "\n", encoding="utf-8")
