"""
Compare Chirp3-HD vs Gemini TTS streaming behavior.

This test helps us understand why Chirp3 works with OGG_OPUS streaming
but Gemini TTS doesn't.
"""

import asyncio
from google.cloud import texttospeech
from config import config


async def test_chirp3_streaming():
    """Test Chirp3-HD with OGG_OPUS streaming."""
    print("\n" + "=" * 60)
    print("TEST 1: Chirp3-HD OGG_OPUS Streaming")
    print("=" * 60)

    client = texttospeech.TextToSpeechAsyncClient.from_service_account_file(config.CREDENTIALS_FILE)

    # Chirp3-HD configuration
    voice = texttospeech.VoiceSelectionParams(
        language_code="en-US",
        name="en-US-Chirp3-HD-Charon",
    )

    streaming_config = texttospeech.StreamingSynthesizeConfig(
        voice=voice,
        streaming_audio_config=texttospeech.StreamingAudioConfig(
            audio_encoding=texttospeech.AudioEncoding.OGG_OPUS,
            sample_rate_hertz=24000,
        ),
    )

    async def request_generator():
        yield texttospeech.StreamingSynthesizeRequest(streaming_config=streaming_config)
        yield texttospeech.StreamingSynthesizeRequest(
            input=texttospeech.StreamingSynthesisInput(text="Hello, this is a test with Chirp3 HD.")
        )

    print("\nStreaming from Chirp3-HD...")
    stream = await client.streaming_synthesize(request_generator())
    packets = []

    async for response in stream:
        packets.append(response.audio_content)
        print(f"  Packet {len(packets)}: {len(response.audio_content)} bytes")

    print(f"\nTotal packets: {len(packets)}")
    print(f"Total size: {sum(len(p) for p in packets)} bytes")

    # Save output
    output_file = "test_chirp3_streaming.ogg"
    with open(output_file, "wb") as f:
        for packet in packets:
            f.write(packet)
    print(f"Saved to: {output_file}")

    # Analyze first packet
    if packets:
        first_bytes = packets[0][:20]
        print(f"\nFirst 20 bytes: {first_bytes.hex()}")
        print(f"Starts with 'OggS': {first_bytes.startswith(b'OggS')}")
        print(f"Contains 'OpusHead': {b'OpusHead' in packets[0]}")

    return packets


async def test_gemini_streaming():
    """Test Gemini TTS with OGG_OPUS streaming."""
    print("\n" + "=" * 60)
    print("TEST 2: Gemini TTS OGG_OPUS Streaming")
    print("=" * 60)

    client = texttospeech.TextToSpeechAsyncClient.from_service_account_file(config.CREDENTIALS_FILE)

    # Gemini TTS configuration
    voice = texttospeech.VoiceSelectionParams(
        language_code="en-US",
        name="Kore",
        model_name="gemini-2.5-flash-preview-tts",
    )

    streaming_config = texttospeech.StreamingSynthesizeConfig(
        voice=voice,
        streaming_audio_config=texttospeech.StreamingAudioConfig(
            audio_encoding=texttospeech.AudioEncoding.OGG_OPUS,
            sample_rate_hertz=24000,
        ),
    )

    async def request_generator():
        yield texttospeech.StreamingSynthesizeRequest(streaming_config=streaming_config)
        yield texttospeech.StreamingSynthesizeRequest(
            input=texttospeech.StreamingSynthesisInput(
                text="Hello, this is a test with Gemini TTS.", prompt="Say the following"
            )
        )

    print("\nStreaming from Gemini TTS...")
    stream = await client.streaming_synthesize(request_generator())
    packets = []

    async for response in stream:
        packets.append(response.audio_content)
        print(f"  Packet {len(packets)}: {len(response.audio_content)} bytes")

    print(f"\nTotal packets: {len(packets)}")
    print(f"Total size: {sum(len(p) for p in packets)} bytes")

    # Save output
    output_file = "test_gemini_streaming.ogg"
    with open(output_file, "wb") as f:
        for packet in packets:
            f.write(packet)
    print(f"Saved to: {output_file}")

    # Analyze first packet
    if packets:
        first_bytes = packets[0][:20]
        print(f"\nFirst 20 bytes: {first_bytes.hex()}")
        print(f"Starts with 'OggS': {first_bytes.startswith(b'OggS')}")
        print(f"Contains 'OpusHead': {b'OpusHead' in packets[0]}")

    return packets


async def compare_formats():
    """Compare the two formats side by side."""
    print("\n" + "=" * 60)
    print("COMPARISON: Chirp3 vs Gemini")
    print("=" * 60)

    chirp3_packets = await test_chirp3_streaming()
    gemini_packets = await test_gemini_streaming()

    print("\n" + "=" * 60)
    print("SUMMARY")
    print("=" * 60)

    print("\nChirp3-HD:")
    print(f"  Packets: {len(chirp3_packets)}")
    print(f"  First packet starts with OggS: {chirp3_packets[0][:4] == b'OggS' if chirp3_packets else 'N/A'}")
    print(f"  Has OpusHead: {b'OpusHead' in chirp3_packets[0] if chirp3_packets else 'N/A'}")

    print("\nGemini TTS:")
    print(f"  Packets: {len(gemini_packets)}")
    print(f"  First packet starts with OggS: {gemini_packets[0][:4] == b'OggS' if gemini_packets else 'N/A'}")
    print(f"  Has OpusHead: {b'OpusHead' in gemini_packets[0] if gemini_packets else 'N/A'}")

    print("\n" + "=" * 60)
    print("CONCLUSION")
    print("=" * 60)

    if chirp3_packets and gemini_packets:
        chirp3_is_ogg = chirp3_packets[0][:4] == b"OggS"
        gemini_is_ogg = gemini_packets[0][:4] == b"OggS"

        if chirp3_is_ogg and not gemini_is_ogg:
            print("\n✅ Chirp3-HD sends proper OGG containers")
            print("❌ Gemini TTS sends raw OPUS packets")
            print("\nThis confirms our wrapper is needed for Gemini TTS!")
        elif chirp3_is_ogg and gemini_is_ogg:
            print("\n✅ Both send OGG containers")
            print("⚠️  The issue might be in how we're wrapping/sending the data")
        else:
            print("\n⚠️  Unexpected format from one or both models")

    print("\nTry playing the files:")
    print("  ffplay test_chirp3_streaming.ogg")
    print("  ffplay test_gemini_streaming.ogg")


if __name__ == "__main__":
    asyncio.run(compare_formats())
