"""Text-to-speech via OpenAI TTS API"""

import logging
from openai import AsyncOpenAI
from config import settings

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


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


async def synthesize_speech(text: str, voice: str = "nova") -> bytes:
    """Synthesize text to speech audio bytes.
    
    Voices available: alloy, echo, fable, nova, shimmer
    Nova is the warmest female voice — fits Kira's personality.
    """
    try:
        client = _get_client()
        resp = await client.audio.speech.create(
            model="tts-1",
            voice=voice,
            input=text,
            response_format="opus",
        )
        return resp.content
    except Exception as e:
        logger.error(f"TTS error: {e}")
        return b""
