"""Generate a synthetic focus-sweep clip for pipeline testing.

The scene is a static textured plane. In frame ``i`` only a horizontal band is
in focus; the band's vertical position moves linearly down the image as ``i``
increases (the focal plane sweeping through a tilted surface). Everything
outside the band is Gaussian-blurred. Recovered depth should therefore increase
monotonically with the row coordinate.
"""
from __future__ import annotations

from pathlib import Path

import cv2
import numpy as np


def make_clip(path: Path, n: int = 40, h: int = 180, w: int = 240, seed: int = 0) -> Path:
    rng = np.random.default_rng(seed)
    # High-frequency texture so the Laplacian has something to bite on.
    texture = rng.integers(0, 256, size=(h, w), dtype=np.uint8)
    texture = cv2.cvtColor(texture, cv2.COLOR_GRAY2BGR)

    fourcc = cv2.VideoWriter_fourcc(*"mp4v")
    writer = cv2.VideoWriter(str(path), fourcc, 30.0, (w, h))
    band_h = max(8, h // 8)
    for i in range(n):
        # Sharp band center sweeps top -> bottom across the sequence.
        center = int((i / max(1, n - 1)) * (h - band_h)) + band_h // 2
        blurred = cv2.GaussianBlur(texture, (0, 0), sigmaX=4.0)
        frame = blurred.copy()
        top = max(0, center - band_h // 2)
        bot = min(h, center + band_h // 2)
        frame[top:bot] = texture[top:bot]  # this row band is sharp
        writer.write(frame)
    writer.release()
    return path


if __name__ == "__main__":
    out = Path(__file__).resolve().parent / "synthetic.mp4"
    make_clip(out)
    print("wrote", out)
