#!/usr/bin/env python3
"""Debug test for ASR processor on large file with debug version."""

import subprocess
import sys
import os
import threading
import time


def read_stream(stream, prefix, log):
    """Read lines from stream and print with prefix."""
    for line in iter(stream.readline, ""):
        timestamp = time.time()
        log.append((timestamp, prefix, line.rstrip()))
        print(f"{prefix}[{timestamp:.3f}]: {line.rstrip()}")
    stream.close()


def main():
    video_path = "/Users/accusys/test_video/1636719d-c31f-78ac-f1dd-8ab0b0b36c66.mov"
    output_path = "/tmp/debug_output2.json"

    cmd = [
        "/opt/homebrew/bin/python3.11",
        "scripts/asr_processor_debug.py",
        video_path,
        output_path,
        # No uuid to avoid Redis connection
    ]

    print(f"Running: {' '.join(cmd)}")
    print(f"Video size: {os.path.getsize(video_path) / (1024**3):.2f} GB")

    # Start process
    proc = subprocess.Popen(
        cmd,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        text=True,
        bufsize=1,
        universal_newlines=True,
    )

    log = []
    stdout_thread = threading.Thread(
        target=read_stream, args=(proc.stdout, "STDOUT", log)
    )
    stderr_thread = threading.Thread(
        target=read_stream, args=(proc.stderr, "STDERR", log)
    )
    stdout_thread.daemon = True
    stderr_thread.daemon = True
    stdout_thread.start()
    stderr_thread.start()

    # Wait with timeout
    timeout = 5  # seconds
    start = time.time()
    try:
        while time.time() - start < timeout:
            if proc.poll() is not None:
                break
            time.sleep(0.1)
        else:
            print(f"\nTimeout after {timeout}s, killing process...")
            proc.kill()
            proc.wait()
            print("Process killed.")
            # Print summary of log
            print("\n=== LOG SUMMARY ===")
            for ts, prefix, line in log:
                print(f"{ts - start:7.3f}s {prefix}: {line}")
            return 1
    except KeyboardInterrupt:
        proc.kill()
        proc.wait()
        return 1

    # Wait for threads to finish reading remaining output
    time.sleep(0.5)

    print(f"\nProcess exited with code {proc.returncode}")
    if os.path.exists(output_path):
        print(f"Output written to {output_path}")
        import json

        with open(output_path, "r") as f:
            data = json.load(f)
            print(f"Segments: {len(data.get('segments', []))}")
    else:
        print("No output file created.")

    # Print log summary
    print("\n=== LOG SUMMARY ===")
    for ts, prefix, line in log:
        print(f"{ts - start:7.3f}s {prefix}: {line}")

    return 0


if __name__ == "__main__":
    sys.exit(main())
