#!/opt/homebrew/bin/python3.11
"""Test ASR processor directly (not via subprocess)."""

import sys
import os
import tempfile
import json

# 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}")
    # Try alternative import
    try:
        import scripts.asr_processor_debug as asr_module

        run_asr = asr_module.run_asr
        print("✓ Imported module")
    except Exception as e2:
        print(f"✗ Failed: {e2}")
        sys.exit(1)


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

    print(f"Testing on small video: {test_video}")

    # 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...")
        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,
        )

        # Check output
        if os.path.exists(output_path):
            with open(output_path, "r") as f:
                output = json.load(f)
            print(f"✓ ASR completed successfully")
            print(f"  Segments: {len(output.get('segments', []))}")
            print(f"  Language: {output.get('language')}")
        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")


def test_large_video_chunk():
    """Test on a large video but with small chunk duration to debug hang."""
    # Use a large video file
    test_video = "/Users/accusys/test_video/1636719d-c31f-78ac-f1dd-8ab0b0b36c66.mov"
    if not os.path.exists(test_video):
        print(f"Large test video not found: {test_video}")
        return

    print(f"\nTesting on large video with 60s chunks (debug): {test_video}")

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

    try:
        # Run with very short chunk duration to see if we can get past first chunk
        print("Calling run_asr with 60s chunks...")
        # We'll run in a separate thread with timeout
        import threading
        import time

        result = {"success": False, "error": None, "output": None}

        def run_asr_thread():
            try:
                run_asr(
                    video_path=test_video,
                    output_path=output_path,
                    uuid="test_large_debug",
                    chunk_duration=60,  # 1 minute chunks (smaller for debugging)
                    max_direct_duration=120,  # 2 minutes
                    model_size="tiny",
                    compute_type="int8",
                    monitor_interval=30,
                )
                result["success"] = True
            except Exception as e:
                result["error"] = str(e)
                result["traceback"] = traceback.format_exc()

        thread = threading.Thread(target=run_asr_thread)
        thread.daemon = True
        thread.start()

        # Wait for 30 seconds max
        thread.join(timeout=30)

        if thread.is_alive():
            print("✗ ASR is hanging (thread still alive after 30 seconds)")
            result["timeout"] = True
        else:
            if result["success"]:
                print("✓ ASR completed within 30 seconds")
                if os.path.exists(output_path):
                    with open(output_path, "r") as f:
                        output = json.load(f)
                    print(f"  Segments: {len(output.get('segments', []))}")
            else:
                print(f"✗ ASR failed: {result.get('error')}")

    except Exception as e:
        print(f"✗ Test 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()
    test_large_video_chunk()
