import subprocess
import os
import uuid

def load_audio(input_path: str) -> str:
    """
    Converts any audio format to WAV (16kHz, mono) using FFmpeg.
    Returns path to converted WAV file.
    """

    if not os.path.exists(input_path):
        raise FileNotFoundError("Audio file not found")

    output_dir = "app/storage/processed"
    os.makedirs(output_dir, exist_ok=True)

    output_path = os.path.join(
        output_dir,
        f"{uuid.uuid4().hex}.wav"
    )

    command = [
        "ffmpeg",
        "-y",
        "-i", input_path,
        "-ac", "1",        # mono
        "-ar", "16000",    # 16kHz
        output_path
    ]

    subprocess.run(
        command,
        stdout=subprocess.DEVNULL,
        stderr=subprocess.DEVNULL,
        check=True
    )

    return output_path
