"""Render native-grid examples for the pallet-body annotation rule."""
from __future__ import annotations

from pathlib import Path

from PIL import Image, ImageDraw

from pallet.data.protocol import file_digest, write_json


EXAMPLES = (
    ("pallet_224.png",
     "outputs/audit_review_round_002/views/4010d88609a3d4e648bf/intensity.png",
     (38, 94, 130, 57)),
    ("pallet_352.png",
     "outputs/audit_review_round_002/views/e68a6daab042d1886de6/intensity.png",
     (75, 124, 197, 42)),
)


def render(output_dir="docs/assets/annotation_policy_v3"):
    output_dir = Path(output_dir)
    output_dir.mkdir(parents=True, exist_ok=True)
    sources = []
    for name, source, (x, y, width, height) in EXAMPLES:
        image = Image.open(source).convert("RGB")
        if x + width > image.width or y + height > image.height:
            raise ValueError("example box extends beyond its native sensor grid")
        draw = ImageDraw.Draw(image)
        draw.rectangle((x, y, x + width, y + height), outline=(238, 66, 47), width=2)
        output = output_dir / name
        image.save(output)
        sources.append({"source": str(Path(source).resolve()),
                        "source_sha256": file_digest(source),
                        "output": str(output.resolve()),
                        "native_size": list(image.size),
                        "illustrative_xywh": [x, y, width, height]})
    write_json(output_dir / "manifest.json", {
        "note": "Illustrative annotation-policy overlays only; these are not frozen reference labels.",
        "examples": sources})
    return sources


if __name__ == "__main__":
    print(render())
