#!/usr/bin/env python3
"""
Final Validation Suite for Spark TTS
Tests all critical functionality before commit
"""

import requests
import time
import json
from pathlib import Path

BASE_URL = "http://localhost:8000"
OUTPUT_DIR = Path("/home/ubuntu/veena3/test_outputs/final")
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)

def test_health():
    """Test 1: Health Endpoint"""
    print("\n" + "="*70)
    print("TEST 1: Health Endpoint")
    print("="*70)
    response = requests.get(f"{BASE_URL}/health")
    data = response.json()
    assert response.status_code == 200
    assert data["status"] == "healthy"
    assert data["service"] == "spark-tts"
    print(f"✅ Health check passed: {data}")
    return True

def test_simple_english():
    """Test 2: Simple English Generation"""
    print("\n" + "="*70)
    print("TEST 2: Simple English Generation")
    print("="*70)
    
    payload = {"text": "[excited] Hello world! This is a simple English test.", "speaker": "lipakshi"}
    start = time.time()
    response = requests.post(f"{BASE_URL}/v1/tts/generate", json=payload)
    duration = time.time() - start
    
    assert response.status_code == 200, f"Failed: {response.text}"
    audio_path = OUTPUT_DIR / "english.wav"
    audio_path.write_bytes(response.content)
    
    print(f"✅ Generated: {len(response.content)/1024:.1f}KB in {duration:.2f}s")
    return True

def test_hindi():
    """Test 3: Hindi Generation"""
    print("\n" + "="*70)
    print("TEST 3: Hindi Generation")
    print("="*70)
    
    payload = {"text": "[laughs] नमस्ते दोस्तों! यह हिंदी परीक्षण है।", "speaker": "vardan"}
    response = requests.post(f"{BASE_URL}/v1/tts/generate", json=payload)
    
    assert response.status_code == 200
    audio_path = OUTPUT_DIR / "hindi.wav"
    audio_path.write_bytes(response.content)
    
    print(f"✅ Generated: {len(response.content)/1024:.1f}KB")
    return True

def test_telugu():
    """Test 4: Telugu Generation"""
    print("\n" + "="*70)
    print("TEST 4: Telugu Generation")
    print("="*70)
    
    payload = {"text": "[curious] హలో! తెలుగు పరీక్ష నడుస్తోంది.", "speaker": "Nandini"}
    response = requests.post(f"{BASE_URL}/v1/tts/generate", json=payload)
    
    assert response.status_code == 200
    audio_path = OUTPUT_DIR / "telugu.wav"
    audio_path.write_bytes(response.content)
    
    print(f"✅ Generated: {len(response.content)/1024:.1f}KB")
    return True

def test_multilingual_mix():
    """Test 5: Multilingual Mixed"""
    print("\n" + "="*70)
    print("TEST 5: Multilingual Mixed (EN+Telugu+Hindi)")
    print("="*70)
    
    payload = {
        "text": "[excited] Hello from English! నమస్కారం తెలుగు నుండి! [laughs] नमस्ते हिंदी से!",
        "speaker": "reet"
    }
    start = time.time()
    response = requests.post(f"{BASE_URL}/v1/tts/generate", json=payload, timeout=60)
    duration = time.time() - start
    
    assert response.status_code == 200, f"Failed: {response.text}"
    audio_path = OUTPUT_DIR / "multilingual_mixed.wav"
    audio_path.write_bytes(response.content)
    
    print(f"✅ Generated: {len(response.content)/1024:.1f}KB in {duration:.2f}s")
    return True

def test_long_text():
    """Test 6: Long Text (800+ chars, triggers chunking)"""
    print("\n" + "="*70)
    print("TEST 6: Long Text with Chunking")
    print("="*70)
    
    long_text = """
    [excited] This is a comprehensive test of the Spark TTS system with long-form content.
    We need to ensure that the chunking mechanism works correctly for texts exceeding eight hundred characters.
    The system should automatically split this into appropriate chunks while maintaining voice consistency.
    Each chunk gets processed independently but should sound seamless when stitched together.
    
    తెలుగు భాషలో కూడా పొడవైన వచనాన్ని పరీక్షించాలి.
    స్పార్క్ టిటిఎస్ వ్యవస్థ ఎలా పనిచేస్తుందో చూడాలి.
    చంకింగ్ మెకానిజం సరిగ్గా పనిచేస్తుందా అని నిర్ధారించుకోవాలి.
    
    हिंदी में भी लंबे पाठ का परीक्षण आवश्यक है।
    स्पार्क टीटीएस प्रणाली कैसे काम करती है यह देखना महत्वपूर्ण है।
    चंकिंग तंत्र सही ढंग से काम करता है या नहीं यह सुनिश्चित करना होगा।
    """.strip()
    
    print(f"Text length: {len(long_text)} characters (should trigger chunking)")
    
    payload = {"text": long_text, "speaker": "krishna"}
    start = time.time()
    response = requests.post(f"{BASE_URL}/v1/tts/generate", json=payload, timeout=180)
    duration = time.time() - start
    
    assert response.status_code == 200, f"Failed: {response.text}"
    audio_path = OUTPUT_DIR / "long_text_chunked.wav"
    audio_path.write_bytes(response.content)
    
    print(f"✅ Generated: {len(response.content)/1024/1024:.2f}MB in {duration:.2f}s")
    return True

def run_all_tests():
    """Run all validation tests"""
    print("\n" + "="*70)
    print("🚀 SPARK TTS - FINAL VALIDATION SUITE")
    print("="*70)
    print(f"Server: {BASE_URL}")
    print(f"Output: {OUTPUT_DIR}")
    
    tests = [
        ("Health Endpoint", test_health),
        ("Simple English", test_simple_english),
        ("Hindi", test_hindi),
        ("Telugu", test_telugu),
        ("Multilingual Mixed", test_multilingual_mix),
        ("Long Text Chunking", test_long_text),
    ]
    
    results = []
    for name, test_func in tests:
        try:
            test_func()
            results.append((name, "✅ PASS"))
        except Exception as e:
            print(f"❌ Failed: {e}")
            results.append((name, f"❌ FAIL: {e}"))
    
    print("\n" + "="*70)
    print("📊 FINAL RESULTS")
    print("="*70)
    for name, result in results:
        print(f"{result:12} - {name}")
    
    passed = sum(1 for _, r in results if r.startswith("✅"))
    print(f"\n🎯 {passed}/{len(results)} tests passed")
    
    if passed == len(results):
        print("\n✅ ALL TESTS PASSED - READY FOR COMMIT!")
        return True
    else:
        print("\n⚠️  Some tests failed - review before commit")
        return False

if __name__ == "__main__":
    success = run_all_tests()
    exit(0 if success else 1)

