#!/usr/bin/env python3
"""Three solid creative examples using ElevenLabs v3 with proper tag support"""

import asyncio
import os
import sys
from pathlib import Path

# Add parent directory to path
parent_dir = Path(__file__).parent.parent.parent
sys.path.insert(0, str(parent_dir))

# Load .env file
env_file = Path.cwd() / ".env"
if env_file.exists():
    with open(env_file, "r") as f:
        for line in f:
            line = line.strip()
            if line and not line.startswith("#") and "=" in line:
                key, value = line.split("=", 1)
                os.environ[key] = value.strip('"').strip("'")

from elevenlabs_speech.server import ElevenLabsSpeechMCPServer  # noqa: E402
from elevenlabs_speech.voice_registry import VOICE_IDS  # noqa: E402

# Use the comprehensive voice registry
VOICES = VOICE_IDS


async def example_1_tech_podcast():
    """Tech podcast with background sounds and multiple emotional shifts (20-30 sec)"""
    print("\n🎙️ EXAMPLE 1: Tech Podcast Drama")
    print("=" * 60)

    server = ElevenLabsSpeechMCPServer()
    if not server.client:
        return

    # Longer narrative with multiple emotions and sounds (~25 seconds)
    podcast_text = """
    [podcast intro music] [EXCITED] Welcome back to Tech Talk Daily,
    where we dive deep into the latest innovations! [LAUGHS]

    [CONFIDENT] Today's topic? Artificial Intelligence. [PAUSES]

    [typing sounds] [CURIOUS] But wait... [NERVOUS]
    I'm getting breaking news right now... [keyboard clicking faster]

    [SHOCKED] [GASP] Oh my god! [WHISPERING] Are you seeing this?
    [papers rustling] [STAMMERING] The... the AI just...

    [BUILDING TENSION] [notification sound] It's responding to us!
    [EXCITED] [SHOUTING] It knows we're talking about it!

    [computer error sound] [PANICKED] Wait, wait, wait!
    [FRUSTRATED] The system is crashing! [alarm beeping]

    [RELIEVED] [SIGH] False alarm everyone... [LAUGHS NERVOUSLY]
    [EMBARRASSED] It was just... my screensaver.

    [PROFESSIONAL] [clearing throat] Let's... let's take a quick break.
    [WHISPERING] [MORTIFIED] Kill the stream... please...
    [podcast outro music fading]
    """

    print("Generating ~25 second tech podcast drama...")
    # NOTE: Using eleven_v3 as the default and recommended model.
    # This is the latest and most advanced model from ElevenLabs with
    # full support for all audio tags and effects.
    result = await server.synthesize_speech_v3(
        text=podcast_text.strip(),
        voice_id=VOICES["Rachel"],
        model="eleven_v3",  # Using v3 - default and most advanced model
        voice_settings={
            "stability": 0.3,  # Dynamic for emotional shifts
            "similarity_boost": 0.7,
            "style": 0.9,  # High style for expressive delivery
        },
    )

    if result.get("success"):
        print(f"✅ Success! Audio URL: {result.get('audio_url')}")
        if result.get("metadata"):
            print("   Duration estimate: ~25 seconds")
            print(f"   Text length: {result['metadata'].get('text_length', 0)} chars")
    else:
        print(f"❌ Failed: {result.get('error')}")


async def example_2_action_movie_scene():
    """Action movie scene with sound effects and dramatic tension (20-30 sec)"""
    print("\n🎬 EXAMPLE 2: Action Movie Scene")
    print("=" * 60)

    server = ElevenLabsSpeechMCPServer()
    if not server.client:
        return

    # Intense action scene (~25 seconds)
    action_text = """
    [thunderstorm] [rain pouring] [WHISPERING] [TENSE]
    They're here... [footsteps approaching] I can hear them.

    [DETERMINED] [gun cocking] Not today. [PAUSES] Not ever.

    [door creaking] [HOLDING BREATH] [heartbeat sound]
    Three... two... [EXPLOSIVE] [SHOUTING] NOW!

    [gunfire] [glass shattering] [AGGRESSIVE]
    GET DOWN! EVERYONE GET DOWN! [explosion]

    [RUSHED] [PANICKED] Move, move, MOVE!
    [car engine starting] [tires screeching]

    [BREATHING HEAVILY] [helicopter overhead]
    [FRUSTRATED] They're still following us!
    [DETERMINED] Hold on tight... [INTENSE]

    [SHOUTING] [car chase sounds] INCOMING!
    [crash sound] [GROANING] [PAINFUL] Ugh...

    [WEAKLY] [radio static] Command... [COUGHING]
    [WHISPERING] Mission... [PAUSES] accomplished...
    [FADING] Tell my family... [EMOTIONAL] I tried...
    [rain continuing] [somber music]
    """

    print("Generating ~25 second action scene...")
    # NOTE: Using eleven_v3 as the default and recommended model.
    # This is the latest and most advanced model from ElevenLabs with
    # full support for all audio tags and effects.
    result = await server.synthesize_speech_v3(
        text=action_text.strip(),
        voice_id=VOICES["Clyde"],  # Intense voice for action
        model="eleven_v3",
        voice_settings={
            "stability": 0.2,  # Very dynamic for action
            "similarity_boost": 0.6,
            "style": 1.0,  # Maximum intensity
        },
    )

    if result.get("success"):
        print(f"✅ Success! Audio URL: {result.get('audio_url')}")
        print("   Duration estimate: ~25 seconds")
    else:
        print(f"❌ Failed: {result.get('error')}")


async def example_3_comedy_restaurant():
    """Comedy sketch at a fancy restaurant with multiple characters (20-30 sec)"""
    print("\n😂 EXAMPLE 3: Fancy Restaurant Comedy")
    print("=" * 60)

    server = ElevenLabsSpeechMCPServer()
    if not server.client:
        return

    # Comedy scene with character voices (~30 seconds)
    comedy_text = """
    [fancy restaurant ambiance] [PRETENTIOUS] [SNOBBISH]
    Good evening sir, welcome to Le Château Prétentieux.
    [CONDESCENDING] I assume you have a reservation?

    [NERVOUS] [STAMMERING] Uh... y-yes! Smith party of two?
    [GULPS] [SWEATING] This place looks... expensive...

    [HAUGHTY] [SCOFFING] Smith? [PAUSES] How... quaint.
    [silverware clinking] Right this way... [DISGUSTED] sir.

    [WHISPERING] [PANICKED] [to date] I can't afford this!
    [FRANTIC] The appetizers cost more than my car!

    [LOUD] [FAKE CONFIDENT] Ah yes! [LAUGHS NERVOUSLY]
    Everything looks... [READING] "Délicieux"?
    [BUTCHERING PRONUNCIATION] "Dee-lick-ee-us"?

    [HORRIFIED] [French accent] Mon Dieu! [GASPS]
    [OFFENDED] Did you just... [DRAMATIC]
    Did you just pronounce it like THAT?!

    [kitchen crashes] [CHAOS] [chef voice] [ANGRY]
    WHO INSULTED MY CUISINE?! [pots banging]
    [ITALIAN ACCENT] [SHOUTING] I QUIT! AGAIN!

    [DEFEATED] [SIGH] Check please...
    [WHISPERING] [EMBARRASSED] Do you take... coupons?
    [SHOCKED] [waiter fainting] [body hitting floor]

    [NARRATOR VOICE] [AMUSED] And that's why Steve
    only dates at McDonald's now. [LAUGHS]
    [record scratch] [sitcom ending music]
    """

    print("Generating ~30 second restaurant comedy...")
    # NOTE: Using eleven_v3 as the default and recommended model.
    # This is the latest and most advanced model from ElevenLabs with
    # full support for all audio tags and effects.
    result = await server.synthesize_speech_v3(
        text=comedy_text.strip(),
        voice_id=VOICES["George"],  # British voice for the waiter
        model="eleven_v3",
        voice_settings={
            "stability": 0.35,  # Balanced for multiple characters
            "similarity_boost": 0.5,  # Allow voice variation
            "style": 0.9,  # High style for comedy
        },
    )

    if result.get("success"):
        print(f"✅ Success! Audio URL: {result.get('audio_url')}")
        print("   Duration estimate: ~30 seconds")
    else:
        print(f"❌ Failed: {result.get('error')}")


async def main():
    """Run the three solid v3 examples"""
    print("\n" + "=" * 80)
    print("🎨 THREE SOLID V3 CREATIVE EXAMPLES (20-30 seconds each)")
    print("=" * 80)
    print("\nUsing ElevenLabs v3 model for PROPER audio tag support!")
    print("Each example is designed to be 20-30 seconds of audio.")

    # Check if v3 is available
    server = ElevenLabsSpeechMCPServer()
    print(f"\nDefault model: {server.config.get('default_model')}")
    print("Overriding to use eleven_v3 for these examples...")

    # Run the three examples
    await example_1_tech_podcast()
    await example_2_action_movie_scene()
    await example_3_comedy_restaurant()

    print("\n" + "=" * 80)
    print("✅ V3 EXAMPLES COMPLETE!")
    print("\n💡 Key differences with v3:")
    print("  • Audio tags actually work (emotions, sounds, effects)")
    print("  • Much more expressive and dynamic")
    print("  • Better handling of multiple characters")
    print("  • Realistic background sounds and ambiance")
    print("=" * 80)


if __name__ == "__main__":
    asyncio.run(main())
