"""Multi-beaker comparison scene SVG generator for AGS Math instruments.

generate(beakers, size) -> str  (self-contained SVG)

  Renders 2-4 beakers side by side, each drawn exactly like the single
  beaker.py instrument (cream glass body, steel-blue graduations, crimson
  fill to the reading), with its caption label beneath. The overall width
  is auto-sized to the number of beakers.

  beakers : list of dicts, e.g.
      [{"capacity": 1000, "reading": 600, "unit": "ml", "label": "A"},
       {"capacity": 1000, "reading": 350, "unit": "ml", "label": "B"}]

  Per-beaker keys: capacity, reading, unit (default "ml"), label (optional).

  Reuses beaker.py's per-beaker drawing logic via draw_beaker().
"""

from beaker import STYLE, draw_beaker


def generate(beakers=None, size=320):
    if not beakers:
        beakers = [
            {"capacity": 1000, "reading": 600, "unit": "ml", "label": "A"},
            {"capacity": 1000, "reading": 350, "unit": "ml", "label": "B"},
        ]
    # Clamp to a sensible 2-4 beakers (render whatever is given, up to 4).
    beakers = list(beakers)[:4]
    n = len(beakers)

    cell_w = size            # one single-beaker cell per beaker
    height = size
    width = cell_w * n

    top = height * 0.16
    bottom = height * 0.82   # leave room for unit + caption beneath
    unit_y = height * 0.90
    body_w = cell_w * 0.40   # same proportion as single beaker.py

    parts = []
    parts.append(
        f'<svg xmlns="http://www.w3.org/2000/svg" width="{width}" height="{height}" '
        f'viewBox="0 0 {width} {height}">'
    )
    parts.append(STYLE)

    # Single rounded border frame around the whole scene
    parts.append(
        f'<rect x="3" y="3" width="{width-6}" height="{height-6}" rx="18" ry="18" '
        f'fill="none" stroke="var(--border)" stroke-width="4"/>'
    )

    for i, bk in enumerate(beakers):
        cell_x = i * cell_w
        bx = cell_x + (cell_w - body_w) / 2
        draw_beaker(
            parts, bx, body_w, top, bottom,
            capacity=bk.get("capacity", 1000),
            reading=bk.get("reading", 0),
            unit=bk.get("unit", "ml"),
            clip_id=f"beakerClip{i}",
            label=bk.get("label"),
            label_y=unit_y,
        )

    parts.append('</svg>')
    return '\n'.join(parts)


if __name__ == '__main__':
    import os
    out = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'samples'))
    os.makedirs(out, exist_ok=True)

    samples = [
        [
            {"capacity": 1000, "reading": 600, "unit": "ml", "label": "A"},
            {"capacity": 1000, "reading": 350, "unit": "ml", "label": "B"},
        ],
        [
            {"capacity": 50, "reading": 12, "unit": "l", "label": "A"},
            {"capacity": 50, "reading": 15, "unit": "l", "label": "B"},
            {"capacity": 50, "reading": 38, "unit": "l", "label": "C"},
        ],
    ]
    for i, bks in enumerate(samples, 1):
        p = os.path.join(out, f'beakers_{i}.svg')
        with open(p, 'w', encoding='utf-8') as f:
            f.write(generate(beakers=bks))
        print('wrote', p)
