from io import BytesIO
from elevenlabs.client import ElevenLabs
from dotenv import load_dotenv
from config.constants import ELEVENLABS_MODEL_ID
import os

load_dotenv(dotenv_path=os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), 'config', '.env'))

class ElevenLabsTTS:
    def __init__(self):
        self.api_key = os.environ.get('ELEVENLABS_API_KEY')
        if not self.api_key:
            raise ValueError("ELEVENLABS_API_KEY not found in environment variables.")
        try:
            self.client = ElevenLabs(api_key=self.api_key)
        except Exception as e:
            raise RuntimeError(f"Failed to initialize ElevenLabs client: {str(e)}")

    def text_to_speech(self, data):
        if not isinstance(data, dict):
            raise TypeError("Input must be a dictionary containing 'text' and 'voice'.")
        
        text = data.get("text")
        voice = data.get("voice")

        if not text or not voice:
            raise ValueError("Missing required 'text' or 'voice' field in input data.")

        try:
            audio_generator = self.client.text_to_speech.convert(
                text=text,
                voice_id=voice,
                model_id=ELEVENLABS_MODEL_ID,
                output_format="mp3_44100_128",
            )

            buffer = BytesIO()
            for chunk in audio_generator:
                buffer.write(chunk)
            buffer.seek(0)
            return buffer

        except Exception as e:
            raise RuntimeError(f"Text-to-Speech conversion failed: {str(e)}")
