"""Create original synthetic HEIC fixtures; no personal photos or external artwork."""
from pathlib import Path
import hashlib
import json
import argparse
from PIL import Image, ImageDraw, ImageFont
import pillow_heif

pillow_heif.register_heif_opener()
parser = argparse.ArgumentParser(description="Create Filepetal synthetic HEIC fixtures. Install pillow and pillow-heif first.")
parser.add_argument("--output", default="heic-jpg-samples", help="Directory for generated samples (default: ./heic-jpg-samples)")
args = parser.parse_args()
root = Path(args.output).resolve()
root.mkdir(parents=True, exist_ok=True)
font = ImageFont.truetype("C:/Windows/Fonts/arial.ttf", 22) if Path("C:/Windows/Fonts/arial.ttf").exists() else ImageFont.load_default(size=22)
rows = []
for name, size in [("color-grid", (640, 400)), ("portrait-marker", (384, 576)), ("over-width-limit", (4097, 96))]:
    image = Image.new("RGB", size, "#f7f5f2")
    draw = ImageDraw.Draw(image)
    w, h = size
    for index, color in enumerate(["#a65f46", "#236b63", "#e3ba55", "#8a8bb1"]):
        left = index * w // 4
        right = (index + 1) * w // 4 - 1
        draw.rectangle((left, h // 4, right, 3 * h // 4), fill=color)
    draw.rectangle((12, 12, min(140, w - 12), min(48, h - 12)), fill="#242321")
    draw.text((18, 16), "TOP LEFT", font=font, fill="white")
    if h > 150:
        draw.text((16, h - 50), f"{name}: {w} x {h}", font=font, fill="#242321")
    exif = Image.Exif()
    exif[270] = "Filepetal synthetic HEIC conversion sample; no personal data"
    exif[274] = 1
    path = root / f"{name}.heic"
    image.save(path, format="HEIF", quality=80, exif=exif.tobytes())
    with Image.open(path) as encoded:
        assert encoded.size == size, (name, encoded.size)
        encoded.convert("RGB").save(root / f"{name}-reference.png")
        rows.append({"name": name, "input": path.name, "width": w, "height": h, "bytes": path.stat().st_size, "sha256": hashlib.sha256(path.read_bytes()).hexdigest(), "exif_tags": sorted(encoded.getexif().keys())})
invalid = root / "invalid-header.heic"
invalid.write_bytes(b"Filepetal invalid HEIC header test. Not a photo.\n")
rows.append({"name":"invalid-header", "input":invalid.name, "bytes":invalid.stat().st_size, "sha256":hashlib.sha256(invalid.read_bytes()).hexdigest()})
(root / "sample-manifest.json").write_text(json.dumps({"license":"CC0-1.0", "created":"2026-10-02", "synthetic":True, "encoder":"pillow-heif 1.8.0; HEIF quality 80", "samples":rows}, indent=2)+"\n", encoding="utf-8")
(root / "README.txt").write_text("Filepetal original synthetic fixtures, created 2026-10-02.\nCC0-1.0: these sample graphics and files may be used and shared freely.\nThese are geometric test patterns, not iPhone photographs.\nThe reference PNGs are decoded from each HEIC, not the pre-encoding source.\nJPEG outputs must be generated with Filepetal before they can be described as tested.\n", encoding="utf-8")
print(json.dumps(rows, indent=2))
