#!/usr/bin/env python3
"""Create 36 explicitly synthetic line images to smoke-test vision/classification.
These are not camera captures. Original teaching example. SPDX-License-Identifier: MIT.
"""
import argparse
from common import output_dir, write_csv, write_json


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--out", required=True)
    args = parser.parse_args()
    try:
        from PIL import Image, ImageDraw
        out = output_dir(args.out)
        rows = []
        for session in range(1, 5):
            split = "train" if session <= 3 else "test"
            for label, center in (("left", 40), ("center", 100), ("right", 160)):
                for sample in range(3):
                    # Small deterministic variations; all 36 files have distinct pixels.
                    name = f"synthetic_s{session}_{label}_{sample + 1}.png"
                    image = Image.new("L", (200, 120), color=225 + session * 3 + sample)
                    drawing = ImageDraw.Draw(image)
                    x = center + sample * 3 - 3
                    half_width = 5 + session
                    drawing.rectangle((x - half_width, 0, x + half_width, 119), fill=20 + sample)
                    image.save(out / name)
                    rows.append({"path": name, "label": label, "session": f"synthetic_session_{session}",
                                 "split": split, "source": "synthetic_generated_not_camera"})
        write_csv(out / "images.csv", list(rows[0]), rows)
        write_json(out / "SYNTHETIC.json", {
            "source": "synthetic_generated_not_camera", "images": 36,
            "train_images": 27, "test_images": 9,
            "note": "Smoke-test toy data only. Session IDs denote generator batches, not independent physical collection sessions. Perfect scores here do not establish camera or robot performance."})
        print(f"Wrote 36 SYNTHETIC images and images.csv to {out}")
    except ImportError:
        parser.error("Install Pillow in your environment: python -m pip install Pillow")
    except (OSError, ValueError) as exc:
        parser.error(str(exc))


if __name__ == "__main__":
    main()
