#!/opt/homebrew/bin/python3.11
"""Run ASR debug processor and capture real-time stderr output."""

import subprocess
import tempfile
import os
import time
import sys
import signal


def run_with_realtime_output():
    test_video = "/Users/accusys/test_video/1636719d-c31f-78ac-f1dd-8ab0b0b36c66.mov"
    if not os.path.exists(test_video):
        print(f"Test video not found: {test_video}")
        return

    with tempfile.NamedTemporaryFile(suffix=".json", delete=False) as f:
        output_path = f.name

    try:
        cmd = [
            "/opt/homebrew/bin/python3.11",
            "scripts/asr_processor_debug.py",
            test_video,
            output_path,
            "--uuid",
            "test_hang_debug",
            "--chunk-duration",
            "60",  # 1 minute chunks for debugging
        ]

        env = os.environ.copy()
        env["MOMENTRY_DISABLE_REDIS"] = "1"

        print(f"Running command: {' '.join(cmd)}")
        print(f"Environment: MOMENTRY_DISABLE_REDIS=1")
        print("-" * 60)

        start = time.time()
        proc = subprocess.Popen(
            cmd,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            text=True,
            bufsize=1,  # line buffered
            env=env,
        )

        # Set timeout to 45 seconds
        timeout = 45
        killed = False

        # Read stderr line by line in real-time
        stderr_lines = []
        while True:
            # Check if process has finished
            if proc.poll() is not None:
                # Process ended, read remaining output
                remaining_stderr = proc.stderr.read()
                if remaining_stderr:
                    lines = remaining_stderr.split("\n")
                    for line in lines:
                        if line:
                            print(f"[stderr] {line}")
                            stderr_lines.append(line)
                break

            # Check for timeout
            if time.time() - start > timeout:
                print(f"\n⏱️ TIMEOUT after {timeout}s - killing process")
                proc.kill()
                killed = True
                break

            # Try to read a line from stderr (non-blocking)
            line = proc.stderr.readline()
            if line:
                line = line.rstrip("\n")
                print(f"[stderr] {line}")
                stderr_lines.append(line)
            else:
                # No output, sleep a bit
                time.sleep(0.1)

        # Get final status
        if killed:
            proc.wait()

        elapsed = time.time() - start
        print(f"\n" + "=" * 60)
        print(f"Elapsed time: {elapsed:.1f}s")
        print(f"Process killed: {killed}")
        print(f"Return code: {proc.returncode}")

        # Look for debug markers in stderr
        print("\n" + "=" * 60)
        print("DEBUG OUTPUT ANALYSIS:")
        print("=" * 60)

        # Find key debug lines
        key_phrases = [
            "ASR_DEBUG: Audio stream check",
            "ASR_DEBUG: Creating temporary directory",
            "ASR_DEBUG: Extracting audio",
            "ASR_DEBUG: Audio extraction successful",
            "ASR_DEBUG: Loading Whisper model",
            "ASR_DEBUG: Whisper model loaded",
            "ASR_DEBUG: total_duration",
            "ASR_DEBUG: Starting chunked transcription",
            "ASR_DEBUG: Calculated",
            "ASR_DEBUG: Created chunk directory",
            "ASR_DEBUG: Starting loop over",
            "ASR_DEBUG: Loop iteration",
            "ASR_DEBUG: extract_chunk:",
            "ASR_DEBUG: Before publisher.progress",
            "ASR_DEBUG: After publisher.progress",
            "ASR_DEBUG: Redis disabled",
        ]

        for phrase in key_phrases:
            matches = [line for line in stderr_lines if phrase in line]
            for match in matches:
                print(f"  {match}")

        # Show last 20 lines of stderr
        print(f"\nLast 20 lines of stderr:")
        for line in stderr_lines[-20:]:
            print(f"  {line}")

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

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


if __name__ == "__main__":
    run_with_realtime_output()
