import litellm
import io
from app.core.providers.base import STTProvider

class GeneralSTTProvider(STTProvider):
    """General Speech-to-Text provider using LiteLLM."""
    def __init__(self, model_name: str, api_key: str, **kwargs):
        self.model_name = model_name
        self.api_key = api_key
        self.kwargs = kwargs
        
        # Validation for missing keys
        if not api_key or "*" in str(api_key):
             import logging
             logging.getLogger(__name__).warning(f"GeneralSTTProvider: api_key is missing or masked for model {model_name}")

    async def transcribe_audio(self, audio_data: bytes) -> str:
        """Transcribes audio data using LiteLLM atranscription."""
        try:
            # We must pass file-like object for the LiteLLM (OpenAI) underlying handler
            buffer = io.BytesIO(audio_data)
            buffer.name = "audio.wav"
            
            response = await litellm.atranscription(
                model=self.model_name,
                file=buffer,
                api_key=self.api_key,
                **self.kwargs
            )
            
            # Response object has 'text' for generic transcription
            return getattr(response, "text", "")
                
        except Exception as e:
            raise RuntimeError(f"Failed to transcribe audio with LiteLLM for model '{self.model_name}': {e}")
