import requests
import json
import base64
from typing import Dict, Any

class CricketTTSTester:
    def __init__(self, api_key: str):
        self.api_key = api_key
        self.base_url = "https://api.sarvam.ai"
        self.headers = {
            "api-subscription-key": api_key,
            "Content-Type": "application/json"
        }
    
    def test_cricket_commentary(self, text: str, filename_prefix: str = "cricket_lbw"):
        """Test TTS with cricket commentary text using different configurations."""
        
        # Test configurations optimized for sports commentary
        test_configs = [
            {
                "name": "normal_commentary",
                "speaker": "meera",
                "pitch": 0,
                "pace": 1.4,  # Slightly slower for clarity
                "loudness": 1.8,  # Louder for commentary style
                "speech_sample_rate": 22050,  # Higher quality
                "description": "Standard commentary voice"
            },
            {
                "name": "energetic_commentary", 
                "speaker": "meera",
                "pitch": 0.2,  # Slightly higher pitch
                "pace": 1.8,   # Faster pace
                "loudness": 2.0,  # Maximum loudness
                "speech_sample_rate": 22050,
                "description": "Energetic sports commentary"
            },
            {
                "name": "clear_explanation",
                "speaker": "meera", 
                "pitch": -0.1,  # Slightly lower for authority
                "pace": 1.2,    # Slower for educational content
                "loudness": 1.5,
                "speech_sample_rate": 24000,  # Highest quality
                "description": "Clear educational explanation"
            }
        ]
        
        results = {}
        
        for config in test_configs:
            print(f"\n--- Testing: {config['description']} ---")
            print(f"Config: pitch={config['pitch']}, pace={config['pace']}, loudness={config['loudness']}")
            
            payload = {
                "inputs": [text],
                "target_language_code": "hi-IN",
                "speaker": config["speaker"],
                "pitch": config["pitch"],
                "pace": config["pace"], 
                "loudness": config["loudness"],
                "speech_sample_rate": config["speech_sample_rate"],
                "enable_preprocessing": True,
                "model": "bulbul:v1"
            }
            
            try:
                response = requests.post(
                    f"{self.base_url}/text-to-speech",
                    headers=self.headers,
                    json=payload,
                    timeout=60  # Longer timeout for long text
                )
                
                print(f"Status Code: {response.status_code}")
                
                if response.status_code == 200:
                    response_data = response.json()
                    results[config["name"]] = response_data
                    
                    # Save audio file
                    if "audios" in response_data and len(response_data["audios"]) > 0:
                        filename = f"{filename_prefix}_{config['name']}.wav"
                        self.save_audio(response_data, filename)
                        print(f"✓ Audio saved: {filename}")
                    else:
                        print("⚠ No audio data in response")
                        
                else:
                    print(f"✗ Error: {response.text}")
                    results[config["name"]] = {"error": response.text, "status_code": response.status_code}
                    
            except requests.exceptions.RequestException as e:
                print(f"✗ Request failed: {e}")
                results[config["name"]] = {"error": str(e)}
        
        return results
    
    def save_audio(self, response_data: Dict[str, Any], filename: str) -> bool:
        """Save audio data to file."""
        try:
            audio_base64 = response_data["audios"][0]
            audio_bytes = base64.b64decode(audio_base64)
            
            with open(filename, "wb") as f:
                f.write(audio_bytes)
            return True
            
        except Exception as e:
            print(f"Error saving audio: {e}")
            return False
    
    def test_text_segments(self, full_text: str):
        """Test different segments of the text to find optimal breaking points."""
        
        # Break the long text into logical segments
        segments = [
            "लेग बिफोर विकेट. एल. बी. डब्ल्यू. फुटबॉल में ऑफ-साइट नियम की तरह है।",
            "बहुत से लोग इसे जानने का दावा करते हैं, लेकिन वास्तव में कितने लोग करते हैं।",
            "हमारी आसान चेकलिस्ट का मतलब है कि चाहे आप खुद को एक अंतरराष्ट्रीय टेस्ट मैच में अंपायर करते हुए पाएँ या समुद्र तट पर बच्चे, निष्पक्षता के लिए आपकी प्रतिष्ठा बरकरार रहेगी।",
            "विचार करने के लिए पांच बुनियादी मानदंड हैं। बल्लेबाज लेग बिफोर विकेट पर आउट है।"
        ]
        
        print("\n--- Testing Text Segments ---")
        
        for i, segment in enumerate(segments, 1):
            print(f"\nSegment {i}: {segment[:50]}...")
            
            payload = {
                "inputs": [segment],
                "target_language_code": "hi-IN",
                "speaker": "meera",
                "pitch": 0,
                "pace": 1.4,
                "loudness": 1.6,
                "speech_sample_rate": 22050,
                "enable_preprocessing": True,
                "model": "bulbul:v1"
            }
            
            try:
                response = requests.post(
                    f"{self.base_url}/text-to-speech",
                    headers=self.headers,
                    json=payload,
                    timeout=30
                )
                
                if response.status_code == 200:
                    response_data = response.json()
                    filename = f"lbw_segment_{i}.wav"
                    self.save_audio(response_data, filename)
                    print(f"✓ Segment {i} saved as: {filename}")
                else:
                    print(f"✗ Segment {i} failed: {response.text}")
                    
            except Exception as e:
                print(f"✗ Segment {i} error: {e}")

def main():
    # Your cricket LBW explanation text
    cricket_text = """लेग बिफोर विकेट. एल. बी. डब्ल्यू. फुटबॉल में ऑफ-साइट नियम की तरह है। बहुत से लोग इसे जानने का दावा करते हैं, लेकिन वास्तव में कितने लोग करते हैं। हमारी आसान चेकलिस्ट का मतलब है कि चाहे आप खुद को एक अंतरराष्ट्रीय टेस्ट मैच में अंपायर करते हुए पाएँ या समुद्र तट पर बच्चे, निष्पक्षता के लिए आपकी प्रतिष्ठा बरकरार रहेगी। """
    
    # Replace with your actual API key
    API_KEY = "sk_8xl5ihpj_NXS7YLNKlWqNUz8C30pnlfUd"
    
    print("=== CRICKET LBW COMMENTARY TTS TEST ===")
    print(f"Text length: {len(cricket_text)} characters")
    print(f"Text preview: {cricket_text[:100]}...\n")
    
    # Initialize tester
    tester = CricketTTSTester(API_KEY)
    
    # Test full text with different voice configurations
    print("1. Testing full text with different voice styles:")
    results = tester.test_cricket_commentary(cricket_text)
    
    # Print summary
    print("\n=== TEST SUMMARY ===")
    for config_name, result in results.items():
        if "error" in result:
            print(f"✗ {config_name}: FAILED - {result.get('error', 'Unknown error')}")
        else:
            print(f"✓ {config_name}: SUCCESS")
    
    # Test segments (uncomment if full text fails due to length)
    # print("\n2. Testing text in segments:")
    # tester.test_text_segments(cricket_text)
    
    print(f"\nDone! Check the generated .wav files for audio quality.")

if __name__ == "__main__":
    main()