from __future__ import annotations
import os
from pathlib import Path
from pydub import AudioSegment
from utils.transcription import TranscriptionService

# Initialize the transcription service
transcription_service = TranscriptionService()

def convert_audio_to_wav(audio_path: str | Path) -> str:
    """Convert uploaded audio to WAV format if needed."""
    audio_path = Path(audio_path)
    output_path = audio_path.with_suffix('.wav')
    
    if audio_path.suffix.lower() != '.wav':
        print(f"Converting {audio_path.name} to WAV format...")
        audio = AudioSegment.from_file(audio_path)
        audio.export(output_path, format='wav')
        return str(output_path)
    
    return str(audio_path)

def transcribe_audio(audio_path: str | Path) -> str:
    """
    Transcribe audio using Deepgram.
    Supports multiple audio formats, converts to WAV if needed.
    """
    try:
        # Convert to WAV if needed
        wav_path = convert_audio_to_wav(audio_path)
        
        # Transcribe using Deepgram
        transcript = transcription_service.transcribe_file(wav_path)
        
        return transcript
        
    except Exception as e:
        raise Exception(f"Error transcribing audio: {str(e)}")