"""Deterministic demo images (generated, no third-party artwork)."""

from __future__ import annotations

import io
import json
import random
from pathlib import Path
from typing import Any

from PIL import Image, ImageDraw

DEMO_DIR = Path(__file__).parent / "investment_proposal"


def cover_art_png(width: int = 600, height: int = 1240, seed: int = 7) -> bytes:
    """Low-poly teal/green artwork similar to the cover of the reference proposal (p1)."""
    rnd = random.Random(seed)
    img = Image.new("RGB", (width, height), (40, 140, 170))
    draw = ImageDraw.Draw(img)
    cols, rows = 6, 12
    pts = [
        [
            (
                int(c * width / cols + (rnd.uniform(-0.3, 0.3) * width / cols if 0 < c < cols else 0)),
                int(r * height / rows + (rnd.uniform(-0.3, 0.3) * height / rows if 0 < r < rows else 0)),
            )
            for c in range(cols + 1)
        ]
        for r in range(rows + 1)
    ]

    def color(x: float, y: float) -> tuple[int, int, int]:
        t = y / height
        base = (int(40 + 90 * t), int(150 + 50 * t), int(190 - 110 * t))
        j = rnd.randint(-22, 22)
        return tuple(max(0, min(255, v + j)) for v in base)  # type: ignore[return-value]

    for r in range(rows):
        for c in range(cols):
            a, b, cc, d = pts[r][c], pts[r][c + 1], pts[r + 1][c], pts[r + 1][c + 1]
            for tri in ((a, b, d), (a, d, cc)):
                cx = sum(p[0] for p in tri) / 3
                cy = sum(p[1] for p in tri) / 3
                draw.polygon(tri, fill=color(cx, cy))
    buf = io.BytesIO()
    img.save(buf, "JPEG", quality=88, optimize=True)
    return buf.getvalue()


def load(name: str) -> Any:
    return json.loads((DEMO_DIR / name).read_text(encoding="utf-8"))


def demo_scene() -> dict[str, Any]:
    return load("scene.json")


def demo_sample_data() -> dict[str, Any]:
    return load("sample_data.json")


def demo_variables() -> list[dict[str, Any]]:
    return load("variables.json")
