#!/opt/homebrew/bin/python3.11
"""Simple test of ASR processor directly."""

import sys
import os
import tempfile
import json
import time

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

# Import the run_asr function
try:
    from scripts.asr_processor_debug import run_asr

    print("✓ Imported run_asr from asr_processor_debug")
except ImportError as e:
    print(f"✗ Failed to import: {e}")
    sys.exit(1)


def test_small_video():
    """Test on a small video file (should work)."""
    test_video = "/Users/accusys/test_video/BigBuckBunny_320x180.mp4"
    if not os.path.exists(test_video):
        print(f"Test video not found: {test_video}")
        return

    print(f"Testing on small video: {os.path.basename(test_video)}")
    print(f"File size: {os.path.getsize(test_video) / (1024**2):.1f} MB")

    # Create temp output file
    with tempfile.NamedTemporaryFile(suffix=".json", delete=False) as f:
        output_path = f.name

    try:
        # Run ASR directly (no subprocess)
        print("Calling run_asr...")
        start = time.time()
        run_asr(
            video_path=test_video,
            output_path=output_path,
            uuid="test_direct",
            chunk_duration=600,  # 10 minutes
            max_direct_duration=1200,  # 20 minutes
            model_size="tiny",
            compute_type="int8",
            monitor_interval=60,
        )
        elapsed = time.time() - start

        # Check output
        if os.path.exists(output_path):
            with open(output_path, "r") as f:
                output = json.load(f)
            print(f"✓ ASR completed successfully in {elapsed:.1f}s")
            print(f"  Segments: {len(output.get('segments', []))}")
            print(f"  Language: {output.get('language')}")
            print(f"  Processor: {output.get('processor_name')}")
        else:
            print("✗ Output file not created")

    except Exception as e:
        print(f"✗ ASR failed: {e}")
        import traceback

        traceback.print_exc()
    finally:
        if os.path.exists(output_path):
            os.unlink(output_path)
            print("✓ Cleaned up output file")


if __name__ == "__main__":
    test_small_video()
