#!/opt/homebrew/bin/python3.11
"""Test Whisper model loading and minimal transcription."""

import sys
import os
import time
import whisper


def test_model_load():
    print("Testing Whisper model loading...")

    # Test loading tiny model
    start = time.time()
    try:
        model = whisper.load_model("tiny", device="cpu")
        elapsed = time.time() - start
        print(f"✓ Model loaded successfully in {elapsed:.2f}s")

        # Test transcription on a tiny audio file
        # Create a 1-second silent audio file or use a test file
        import tempfile
        import subprocess

        # Create a 1-second silent WAV file using sox or ffmpeg
        with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as f:
            temp_wav = f.name

        # Create silent audio using ffmpeg
        cmd = [
            "ffmpeg",
            "-f",
            "lavfi",
            "-i",
            "anullsrc=r=16000:cl=mono",
            "-t",
            "1",
            "-acodec",
            "pcm_s16le",
            temp_wav,
            "-y",
        ]
        try:
            subprocess.run(cmd, capture_output=True, check=True)
            print(f"✓ Created test audio file: {temp_wav}")

            # Try transcription
            print("Testing transcription...")
            start_trans = time.time()
            result = model.transcribe(temp_wav, beam_size=5)
            elapsed_trans = time.time() - start_trans
            print(f"✓ Transcription successful in {elapsed_trans:.2f}s")
            print(f"  Segments: {len(result['segments'])}")

            os.unlink(temp_wav)

        except Exception as e:
            print(f"✗ Failed to create test audio: {e}")
            if os.path.exists(temp_wav):
                os.unlink(temp_wav)

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

        traceback.print_exc()


if __name__ == "__main__":
    test_model_load()
