#!/usr/bin/env python3
"""
Comprehensive Word-Level Analysis using ElevenLabs ASR

ElevenLabs ASR is superior to OpenAI Whisper for better accuracy.
This will definitively show if content is missing due to:
- ASR limitation (words spoken but not transcribed)
- TTS chunking error (words not spoken at all)
"""

import os
import sys
import requests
import subprocess
import time
import re

# ElevenLabs API
ELEVENLABS_API_KEY = os.environ.get('ELEVENLABS_API_KEY')
if not ELEVENLABS_API_KEY:
    print("❌ ELEVENLABS_API_KEY not set")
    sys.exit(1)

API_BASE = "https://api.elevenlabs.io/v1"
AUDIO_FILE = "comprehensive_word_analysis.wav"

# Reference text (English only)
REFERENCE_TEXT = """In the heart of an enchanted kingdom, where magic flowed like rivers and mythical beasts roamed freely, there lived a legendary warrior princess named Aisha. She was known throughout the land for her incredible courage, her mastery of ancient combat arts, and her ability to communicate with all living creatures. But few knew of the prophecy that surrounded her birth. The ancient seers had foretold that she would one day face the greatest darkness the world had ever seen, and only she possessed the power to defeat it. Aisha spent her childhood training in the sacred mountains with the wise monks. They taught her not just the physical arts of combat, but also the spiritual disciplines that would strengthen her mind and soul. Every dawn, she would practice her sword techniques as the sun rose over the peaks. Every evening, she would meditate under the stars, learning to harness the cosmic energies that flowed through all things. One fateful day, dark clouds gathered over the kingdom! The evil demon lord Ravaksh had awakened from his thousand-year slumber. His army of shadow warriors began to sweep across the land, corrupting everything they touched. Villages were destroyed, forests withered, and despair spread like wildfire. The kingdom's armies fought valiantly, but they were no match for Ravaksh's dark magic. One by one, the greatest warriors fell. The king's advisors urged him to flee, to abandon the kingdom and save what few lives they could. But Aisha refused to give up! She knew this was the moment the prophecy had spoken of. She gathered the remaining defenders and devised a daring plan. They would launch a surprise attack on Ravaksh's fortress while he was still consolidating his power. Under the cover of darkness, they approached the fortress. The shadow guards patrolled the walls, their red eyes glowing in the night. But Aisha had learned from the wind spirits how to move unseen! She led her team through secret passages known only to the ancient monks. The infiltration was successful. They reached the inner sanctum where Ravaksh was performing a dark ritual. He was trying to summon an even greater evil from the void! There was no time to waste. Aisha drew her legendary sword, which began to glow with divine light! The blade had been forged by the celestial smiths themselves, imbued with the power to vanquish any darkness. As she charged forward, her companions engaged the shadow warriors, giving her a clear path to Ravaksh. The battle that followed was epic. Ravaksh wielded powers that defied comprehension - he could bend reality itself, create illusions that seemed more real than reality, and summon storms of pure destruction. But Aisha had trained for this her entire life. She dodged his dark bolts, deflected his curse spells, and pressed forward relentlessly. She remembered the words of her master: "True strength comes not from power, but from purpose." Her purpose was clear - protect her people, save her kingdom, restore the light. With a mighty cry, she leaped high into the air, her sword blazing like a star. The blade struck true, piercing through Ravaksh's dark armor and into his corrupted heart! The demon lord let out a terrible scream as the divine light consumed him. The shadow army dissolved like smoke in the wind! The dark clouds parted, and sunlight flooded the land once more. The corrupted forests began to heal, flowers bloomed, and the rivers ran clear again. Aisha returned to her kingdom as a hero beyond measure! The people celebrated for seven days and seven nights. Songs were written about her bravery, statues were erected in her honor, and her legend would be told for a thousand generations. But Aisha remained humble. She knew that true victory was not in defeating enemies, but in protecting those she loved. She had seen too much loss, too much suffering. From that day forward, she dedicated herself to teaching the next generation, ensuring they would be ready when darkness threatened again. And so the kingdom prospered under her watchful protection, and peace reigned for many years to come!"""


def submit_audio_for_transcription(audio_file):
    """Submit audio file to ElevenLabs for transcription."""
    print(f"📤 Uploading audio to ElevenLabs...")
    print(f"   File: {audio_file}")
    
    file_size_mb = os.path.getsize(audio_file) / (1024 * 1024)
    print(f"   Size: {file_size_mb:.1f} MB")
    
    url = f"{API_BASE}/speech-to-text"
    headers = {
        "xi-api-key": ELEVENLABS_API_KEY
    }
    
    with open(audio_file, 'rb') as f:
        # Model ID is required
        data = {
            'model_id': 'scribe_v1'  # ElevenLabs Scribe model
        }
        
        files = {
            'file': (os.path.basename(audio_file), f, 'audio/wav')
        }
        
        print(f"   Uploading (this may take 30-60s for large files)...")
        response = requests.post(url, headers=headers, files=files, data=data, timeout=180)
    
    if response.status_code == 200:
        result = response.json()
        print(f"✅ Transcription completed!")
        return result
    else:
        print(f"❌ Error: {response.status_code}")
        print(response.text)
        return None


def find_word_context_with_timestamp(word, reference, ref_words, duration):
    """Find context and estimated timestamp for a missing word."""
    try:
        word_index = ref_words.index(word)
        estimated_time = (word_index / len(ref_words)) * duration
        
        # Get surrounding words
        context_before = ' '.join(ref_words[max(0, word_index-5):word_index])
        context_after = ' '.join(ref_words[word_index+1:min(len(ref_words), word_index+6)])
        
        return {
            'timestamp': estimated_time,
            'context': f"{context_before} [{word}] {context_after}",
            'index': word_index
        }
    except ValueError:
        return None


def main():
    print("=" * 80)
    print("🎙️  ELEVENLABS ASR - DEFINITIVE WORD ANALYSIS")
    print("=" * 80)
    print()
    
    # Check audio file
    if not os.path.exists(AUDIO_FILE):
        print(f"❌ Audio file not found: {AUDIO_FILE}")
        sys.exit(1)
    
    # Get duration
    result = subprocess.run(
        ['ffprobe', '-v', 'error', '-show_entries', 'format=duration',
         '-of', 'default=noprint_wrappers=1:nokey=1', AUDIO_FILE],
        capture_output=True, text=True
    )
    actual_duration = float(result.stdout.strip())
    
    print(f"📁 Audio: {AUDIO_FILE}")
    print(f"   Duration: {actual_duration:.1f}s ({actual_duration/60:.1f} min)")
    print(f"   Reference text: {len(REFERENCE_TEXT)} chars")
    print()
    
    # Submit for transcription
    transcription_result = submit_audio_for_transcription(AUDIO_FILE)
    
    if not transcription_result:
        sys.exit(1)
    
    # Extract transcribed text
    if 'text' in transcription_result:
        transcribed_text = transcription_result['text']
    else:
        print("❌ Could not extract transcribed text from response")
        print(f"Response keys: {transcription_result.keys()}")
        sys.exit(1)
    
    print()
    print(f"📊 Transcription:")
    print(f"   Length: {len(transcribed_text)} chars")
    print(f"   Preview: {transcribed_text[:200]}...")
    print()
    
    # Normalize and compare
    def normalize(text):
        return re.sub(r'[^\w\s]', '', text.lower()).strip()
    
    ref_words = normalize(REFERENCE_TEXT).split()
    trans_words = normalize(transcribed_text).split()
    
    ref_set = set(ref_words)
    trans_set = set(trans_words)
    
    missing = ref_set - trans_set
    common = ref_set & trans_set
    
    print(f"📊 Word Analysis:")
    print(f"   Reference words: {len(ref_words)} (unique: {len(ref_set)})")
    print(f"   Transcribed words: {len(trans_words)} (unique: {len(trans_set)})")
    print(f"   Correct: {len(common)}")
    print(f"   Missing: {len(missing)}")
    print(f"   Accuracy: {len(common) / len(ref_set) * 100:.1f}%")
    print()
    
    # Analyze missing words with timestamps
    if missing:
        print("=" * 80)
        print(f"🔍 MISSING WORDS - DETAILED LIST ({len(missing)} words)")
        print("=" * 80)
        print()
        
        missing_with_info = []
        for word in sorted(list(missing)):
            info = find_word_context_with_timestamp(word, REFERENCE_TEXT, ref_words, actual_duration)
            if info:
                missing_with_info.append((word, info))
        
        # Sort by timestamp
        missing_with_info.sort(key=lambda x: x[1]['timestamp'])
        
        for i, (word, info) in enumerate(missing_with_info, 1):
            print(f"{i}. '{word}' at ~{info['timestamp']:.0f}s (word #{info['index']+1}/{len(ref_words)})")
            print(f"   Context: ...{info['context']}...")
            print(f"   👂 Listen: ffplay -ss {info['timestamp']:.0f} {AUDIO_FILE}")
            print()
        
        # Save verification commands
        with open('verify_missing_words.sh', 'w') as f:
            f.write("#!/bin/bash\n")
            f.write("# Verification commands for each missing word\n\n")
            for word, info in missing_with_info[:20]:  # First 20
                f.write(f"# Word: '{word}' at ~{info['timestamp']:.0f}s\n")
                f.write(f"echo \"Checking '{word}' at {info['timestamp']:.0f}s...\"\n")
                f.write(f"ffplay -ss {max(0, info['timestamp']-2):.0f} -t 8 {AUDIO_FILE} -nodisp -autoexit 2>/dev/null\n")
                f.write(f"read -p \"Did you hear '{word}'? (y/n): \" answer\n")
                f.write(f"echo \"$answer\" >> verification_results.txt\n\n")
        
        print(f"💾 Verification script: verify_missing_words.sh")
        print(f"   chmod +x verify_missing_words.sh && ./verify_missing_words.sh")
    else:
        print("✅ NO MISSING WORDS!")
        print("   100% accuracy - all content captured!")
    
    print()
    print("=" * 80)
    print(f"📊 SUMMARY:")
    print(f"   Total words: {len(ref_set)}")
    print(f"   Missing: {len(missing)}")
    print(f"   Accuracy: {len(common) / len(ref_set) * 100:.1f}%")
    
    if len(missing) == 0:
        print(f"\n   ✅ PERFECT - No chunking errors!")
    elif len(missing) < len(ref_set) * 0.05:
        print(f"\n   ✅ EXCELLENT - <5% missing (likely ASR errors)")
    elif len(missing) < len(ref_set) * 0.10:
        print(f"\n   ⚠️  GOOD - <10% missing (verify manually)")
    else:
        print(f"\n   ❌ POOR - >10% missing (likely chunking errors)")
    
    print("=" * 80)
    print()


if __name__ == '__main__':
    main()
