import argparse
import os
from dotenv import load_dotenv
from elevenlabs.client import ElevenLabs

load_dotenv()


def create_audio_from_text(text: str, output_path: str) -> str:
    """Generate audio from text using ElevenLabs text-to-speech API."""
    client = ElevenLabs(api_key=os.getenv("ELEVENLABS_API_KEY"))

    audio = client.text_to_speech.convert(
        text=text,
        voice_id="JBFqnCBsd6RMkjVDRZzb",  # George voice
        model_id="eleven_multilingual_v2",
        output_format="mp3_44100_128",
    )

    with open(output_path, "wb") as f:
        for chunk in audio:
            f.write(chunk)

    return output_path


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="ElevenLabs audio tools")
    parser.add_argument("text", help="Text to convert to speech")
    parser.add_argument("--output", "-o", required=True, help="Output file path for the audio")

    args = parser.parse_args()

    result = create_audio_from_text(args.text, args.output)
    print(f"Audio saved to: {result}")
