import logging
from fastapi import HTTPException
from app.core.providers.base import STTProvider

# Configure logging
logger = logging.getLogger(__name__)

class STTService:
    """
    Service class for transcribing audio into text using an STT provider.
    """

    def __init__(self, stt_provider: STTProvider = None):
        """
        Initializes the STTService with an optional STT provider.
        """
        self.default_stt_provider = stt_provider

    async def transcribe(self, audio_bytes: bytes, provider_override: STTProvider = None) -> str:
        """
        Transcribes the provided audio bytes into text using the STT provider.
        """
        provider = provider_override or self.default_stt_provider
        if not provider:
            logger.error("STT transcription attempt but no STT provider is configured (globally or override).")
            raise HTTPException(
                status_code=400, 
                detail="No active STT provider is configured. Please set an API key/provider in settings or user preferences."
            )
        logger.info(f"Starting transcription for audio data ({len(audio_bytes)} bytes).")

        if not audio_bytes:
            logger.warning("No audio data provided for transcription.")
            raise HTTPException(status_code=400, detail="No audio data provided.")

        try:
            transcript = await provider.transcribe_audio(audio_bytes)
            # Note: empty transcript is valid (e.g. silent audio), we let the caller decide
            logger.info(f"Transcribed audio. Length: {len(transcript)} characters.")
            return transcript

        except HTTPException:
            raise  # Pass through existing HTTPException without wrapping
        except Exception as e:
            logger.error(f"Unexpected error during transcription: {e}")
            raise HTTPException(
                status_code=500,
                detail=f"Error during transcription: {e}"
            ) from e
