#!/usr/bin/env python3
"""
Test ASR v2 processor.
"""

import sys
import os
import tempfile
import json
import subprocess
from pathlib import Path


def test_asr_v2():
    video_path = "../test_video/BigBuckBunny_320x180.mp4"
    if not Path(video_path).exists():
        print(f"Video not found: {video_path}")
        sys.exit(1)

    # Create output directory
    output_dir = Path("test_output_v2")
    output_dir.mkdir(exist_ok=True, parents=True)
    output_path = output_dir / "asr_v2_output.json"

    # Run ASR v2
    script_path = Path("scripts/asr_processor_v2.py")
    if not script_path.exists():
        print(f"Script not found: {script_path}")
        sys.exit(1)

    cmd = [
        sys.executable,
        str(script_path),
        video_path,
        str(output_path),
        "--chunk-duration",
        "300",  # 5 minutes
        "--model-size",
        "tiny",
        "--compute-type",
        "int8",
    ]

    print(f"Running: {' '.join(cmd)}")
    print(f"Video: {video_path}")

    start_time = time.time()
    result = subprocess.run(cmd, capture_output=True, text=True)
    elapsed = time.time() - start_time

    print(f"Exit code: {result.returncode}")
    print(f"Elapsed time: {elapsed:.1f}s")

    if result.stdout:
        print(f"Stdout:\n{result.stdout[:500]}")
    if result.stderr:
        print(f"Stderr:\n{result.stderr[:500]}")

    if output_path.exists():
        with open(output_path, "r") as f:
            data = json.load(f)
        print(f"\nResults:")
        print(f"  Language: {data.get('language')}")
        print(f"  Segments: {len(data.get('segments', []))}")
        print(f"  Chunks: {data.get('chunk_count', 1)}")
        print(f"  Processing mode: {data.get('processing_mode', 'unknown')}")

        if data.get("segments"):
            print(f"\nFirst 3 segments:")
            for i, seg in enumerate(data["segments"][:3]):
                print(
                    f"  {i}: {seg['start']:.1f}-{seg['end']:.1f}: {seg['text'][:50]}..."
                )

    return result.returncode == 0


if __name__ == "__main__":
    import time

    success = test_asr_v2()
    sys.exit(0 if success else 1)
