#!/usr/bin/env python3
"""
Test audio playback in an active voice session.
This sends text to the TTS and should be heard in Discord voice.
"""

import asyncio
import sys
sys.path.insert(0, '/app')

from utils.voice_manager import voice_manager

async def test_voice_playback():
    print("🎤 Testing voice playback in active session...")
    
    if not voice_manager.active_session:
        print("❌ No active voice session! Use '!miku join' first.")
        return
    
    if not voice_manager.active_session.tts_streamer:
        print("❌ TTS streamer not initialized!")
        return
    
    if not voice_manager.active_session.voice_client:
        print("❌ Not connected to voice!")
        return
    
    print(f"✓ Active session in: {voice_manager.active_session.voice_channel.name}")
    print(f"✓ Voice client connected: {voice_manager.active_session.voice_client.is_connected()}")
    print(f"✓ Voice client playing: {voice_manager.active_session.voice_client.is_playing()}")
    
    try:
        test_text = "Hello! This is a test of the voice chat system."
        print(f"\n📤 Speaking: '{test_text}'")
        
        await voice_manager.active_session.tts_streamer.stream_text(test_text)
        
        print("✓ Text sent to TTS!")
        print("⏳ Audio should be playing in Discord voice channel...")
        print("   (Wait a few seconds for TTS processing and playback)")
        
        await asyncio.sleep(5)
        print("✅ Test complete!")
        
    except Exception as e:
        print(f"❌ Error: {e}")
        import traceback
        traceback.print_exc()

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