"""Speech-to-text via OpenAI Whisper API"""

import logging
from openai import AsyncOpenAI
from config import settings

logger = logging.getLogger("kira.stt")


def _get_client() -> AsyncOpenAI:
    return AsyncOpenAI(api_key=settings.openai_api_key)


async def transcribe_audio(audio_bytes: bytes) -> str | None:
    """Transcribe audio bytes to text using Whisper API."""
    try:
        client = _get_client()
        transcript = await client.audio.transcriptions.create(
            model="whisper-1",
            file=("audio.webm", audio_bytes, "audio/webm"),
            language="en",
            response_format="text",
        )
        return transcript.strip() if transcript and transcript.strip() else None
    except Exception as e:
        logger.error(f"STT error: {e}")
        return None
