from __future__ import annotations

from pathlib import Path

from moviepy import VideoFileClip, concatenate_videoclips


def assemble_clips(clips: list[str | Path], output_path: str | Path) -> Path:
    out = Path(output_path)
    out.parent.mkdir(parents=True, exist_ok=True)
    if not clips:
        raise ValueError("clips must not be empty")

    vclips: list[VideoFileClip] = []
    for c in clips:
        vclips.append(VideoFileClip(str(c)))

    final = concatenate_videoclips(vclips, method="compose")
    try:
        fps = vclips[0].fps if vclips and vclips[0].fps else 24
        final.write_videofile(str(out), codec="libx264", audio_codec="aac", fps=fps, preset="medium", threads=4)
    finally:
        final.close()
        for c in vclips:
            c.close()
    return out

