"""
ElevenLabs — Text-to-Speech Demo
=================================
A minimal, ready-to-run example of synthesizing speech with ElevenLabs.

Get an API key (free tier):
    1. Sign up at https://elevenlabs.io/sign-up
    2. Click your profile icon (bottom-left) -> "API Keys".
    3. Create a key and copy it.

Setup:
    pip install requests
    export ELEVENLABS_API_KEY="..."

Run:
    python elevenlabs_demo.py
"""

import os
import requests

# "Rachel" — a default public voice
VOICE_ID = "21m00Tcm4TlvDq8ikWAM"


def synthesize(text: str, out_path: str = "speech.mp3") -> None:
    """Convert text to speech and save it as an MP3."""
    response = requests.post(
        f"https://api.elevenlabs.io/v1/text-to-speech/{VOICE_ID}",
        headers={"xi-api-key": os.environ.get("ELEVENLABS_API_KEY")},
        json={"text": text, "model_id": "eleven_multilingual_v2"},
    )
    response.raise_for_status()
    with open(out_path, "wb") as f:
        f.write(response.content)
    print(f"Saved audio to {out_path}")


if __name__ == "__main__":
    synthesize("Hello from krueng.ai.")
