#!/usr/bin/env python3
"""
Test ASR processor on a single large video file.
"""

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

# Add scripts directory to path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "scripts"))


def test_large_video(video_path, output_dir="test_large_output"):
    """Test ASR on a large video file."""
    if not os.path.exists(video_path):
        print(f"Video file not found: {video_path}")
        return False

    os.makedirs(output_dir, exist_ok=True)

    # Create output file path
    video_name = os.path.splitext(os.path.basename(video_path))[0]
    output_file = os.path.join(output_dir, f"{video_name}.asr.json")

    print(f"Testing ASR on: {os.path.basename(video_path)}")
    print(f"Output: {output_file}")

    # Run ASR processor with timeout
    # Use the script directly
    script_path = os.path.join(os.path.dirname(__file__), "scripts", "asr_processor.py")

    cmd = [
        "/opt/homebrew/bin/python3.11",
        script_path,
        "--video",
        video_path,
        "--output",
        output_file,
        "--model-size",
        "tiny",  # Use smallest for testing
        "--progress",
    ]

    print(f"Command: {' '.join(cmd)}")

    start_time = time.time()

    try:
        # Run with timeout (2 hours for 2GB file)
        timeout = 7200  # 2 hours

        # Create a process
        proc = subprocess.Popen(
            cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True
        )

        # Wait with timeout
        try:
            stdout, stderr = proc.communicate(timeout=timeout)
            returncode = proc.returncode
        except subprocess.TimeoutExpired:
            print(f"Process timed out after {timeout} seconds")
            proc.kill()
            stdout, stderr = proc.communicate()
            returncode = -1

        elapsed = time.time() - start_time

        print(f"Elapsed time: {elapsed:.2f} seconds")
        print(f"Return code: {returncode}")

        if stdout:
            print(f"STDOUT (last 10 lines):")
            for line in stdout.strip().split("\n")[-10:]:
                print(f"  {line}")

        if stderr:
            print(f"STDERR (last 20 lines):")
            for line in stderr.strip().split("\n")[-20:]:
                print(f"  {line}")

        # Check output file
        if os.path.exists(output_file):
            try:
                with open(output_file, "r") as f:
                    data = json.load(f)
                print(
                    f"Success! Output file created with {len(data.get('segments', []))} segments"
                )
                print(f"Processing mode: {data.get('processing_mode', 'unknown')}")
                print(f"Chunk count: {data.get('chunk_count', 1)}")
                return True
            except json.JSONDecodeError as e:
                print(f"Output file is not valid JSON: {e}")
                return False
        else:
            print("Output file not created")
            return False

    except Exception as e:
        print(f"Error running ASR: {e}")
        return False


if __name__ == "__main__":
    # Test with a large video file
    video_path = "../test_video/1636719d-c31f-78ac-f1dd-8ab0b0b36c66.mov"

    # Check if file exists
    if not os.path.exists(video_path):
        # Try absolute path
        video_path = (
            "/Users/accusys/test_video/1636719d-c31f-78ac-f1dd-8ab0b0b36c66.mov"
        )

    if not os.path.exists(video_path):
        print(f"Large video file not found at {video_path}")
        print("Please specify a different video file path.")
        sys.exit(1)

    success = test_large_video(video_path)

    if success:
        print("\n✅ Large video ASR test PASSED")
        sys.exit(0)
    else:
        print("\n❌ Large video ASR test FAILED")
        sys.exit(1)
