#!/usr/bin/env python3
"""
Final ASR Verification with ElevenLabs
Compare expected text with actual audio using word-level timestamps
"""

import os
import sys
import requests
import re
from collections import defaultdict

# ElevenLabs API configuration
ELEVENLABS_API_KEY = os.environ.get('ELEVENLABS_API_KEY')
if not ELEVENLABS_API_KEY:
    print("❌ ELEVENLABS_API_KEY not set!")
    print("   Export it: export ELEVENLABS_API_KEY='your-key'")
    sys.exit(1)

# Original test text (same as generation)
TEST_TEXT = """<excited> 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. </excited> 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.

<whisper> But few knew of the prophecy that surrounded her birth. </whisper> 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. <curious> They taught her not just the physical arts of combat, but also the spiritual disciplines that would strengthen her mind and soul. </curious> 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.

<angry> One fateful day, dark clouds gathered over the kingdom! </angry> 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.

<sigh> The kingdom's armies fought valiantly, but they were no match for Ravaksh's dark magic. </sigh> 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.

<excited> But Aisha refused to give up! </excited> 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.

एक बार की बात है, ऐशा ने अपने सबसे भरोसेमंद योद्धाओं को इकट्ठा किया। उन्होंने एक खतरनाक योजना बनाई। वे रावक्ष के किले पर हमला करेंगे। यह असंभव लग रहा था, लेकिन उनके पास कोई विकल्प नहीं था। राज्य का भविष्य उनके हाथों में था।

<whisper> Under the cover of darkness, they approached the fortress. </whisper> The shadow guards patrolled the walls, their red eyes glowing in the night. <laugh> But Aisha had learned from the wind spirits how to move unseen! </laugh> 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. <angry> He was trying to summon an even greater evil from the void! </angry> There was no time to waste.

<excited> Aisha drew her legendary sword, which began to glow with divine light! </excited> 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. <curious> 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. </curious> But Aisha had trained for this her entire life.

She dodged his dark bolts, deflected his curse spells, and pressed forward relentlessly. <whisper> She remembered the words of her master: "True strength comes not from power, but from purpose." </whisper> 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. <excited> The blade struck true, piercing through Ravaksh's dark armor and into his corrupted heart! </excited> The demon lord let out a terrible scream as the divine light consumed him.

<laugh> The shadow army dissolved like smoke in the wind! </laugh> 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.

<excited> Aisha returned to her kingdom as a hero beyond measure! </excited> 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.

<whisper> But Aisha remained humble. </whisper> She knew that true victory was not in defeating enemies, but in protecting those she loved. <sigh> She had seen too much loss, too much suffering. </sigh> From that day forward, she dedicated herself to teaching the next generation, ensuring they would be ready when darkness threatened again.

<excited> And so the kingdom prospered under her watchful protection, and peace reigned for many years to come! </excited>"""

AUDIO_FILE = "fixed_test.wav"

def clean_text(text):
    """Remove emotion tags and normalize text."""
    # Remove emotion tags
    text = re.sub(r'<[^>]+>', '', text)
    # Normalize whitespace
    text = re.sub(r'\s+', ' ', text)
    return text.strip()

def extract_words(text):
    """Extract all words from text in order."""
    words = []
    for match in re.finditer(r'\b[\w]+\b', text.lower()):
        words.append({
            'word': match.group(),
            'position': match.start()
        })
    return words

def transcribe_with_elevenlabs(audio_file):
    """Transcribe audio using ElevenLabs API with word-level timestamps."""
    print(f"🎧 Transcribing with ElevenLabs API...")
    print(f"   File: {audio_file}")
    
    url = "https://api.elevenlabs.io/v1/audio-native/transcribe"
    
    with open(audio_file, 'rb') as f:
        files = {'audio': (audio_file, f, 'audio/wav')}
        headers = {'xi-api-key': ELEVENLABS_API_KEY}
        
        response = requests.post(url, files=files, headers=headers)
    
    if response.status_code != 200:
        print(f"❌ API Error: {response.status_code}")
        print(response.text)
        return None
    
    result = response.json()
    print(f"✅ Transcription complete")
    print(f"   Text length: {len(result.get('text', ''))} chars")
    
    return result

def analyze_timestamps(reference_text, transcription):
    """Analyze word-level timestamps and find missing content."""
    clean_ref = clean_text(reference_text)
    ref_words = extract_words(clean_ref)
    
    transcript_text = transcription.get('text', '')
    transcript_words_raw = transcription.get('words', [])
    
    print(f"\n📊 Basic Statistics:")
    print(f"   Reference words: {len(ref_words)}")
    print(f"   Transcribed words: {len(transcript_words_raw)}")
    print(f"   Accuracy: {len(transcript_words_raw)/len(ref_words)*100:.1f}%")
    
    # Find missing words
    trans_words_set = set([w['text'].lower() for w in transcript_words_raw])
    ref_words_list = [w['word'] for w in ref_words]
    
    missing = []
    for i, word in enumerate(ref_words_list):
        if word not in trans_words_set:
            missing.append({
                'word': word,
                'index': i,
                'context_before': ' '.join(ref_words_list[max(0,i-3):i]),
                'context_after': ' '.join(ref_words_list[i+1:min(len(ref_words_list),i+4)])
            })
    
    print(f"   Missing words: {len(missing)}")
    
    # Analyze where audio ends
    if transcript_words_raw:
        last_word = transcript_words_raw[-1]
        last_timestamp = last_word.get('end', 0)
        
        print(f"\n🎯 Audio Ending Analysis:")
        print(f"   Last transcribed word: '{last_word.get('text', '')}'")
        print(f"   Timestamp: {last_timestamp:.2f}s")
        print(f"   Total duration: ~{last_timestamp:.2f}s")
        
        # Find where this word is in reference
        last_word_text = last_word.get('text', '').lower()
        for i, word_info in enumerate(ref_words):
            if word_info['word'] == last_word_text:
                remaining = len(ref_words) - i - 1
                print(f"   Position in text: {i+1}/{len(ref_words)}")
                print(f"   Words remaining: {remaining}")
                if remaining > 0:
                    print(f"   Missing from end: {' '.join(ref_words_list[i+1:i+11])}")
                break
    
    return missing, transcript_words_raw

def find_missing_sections(reference_text, missing_words, ref_words_list):
    """Group missing words into sections."""
    if not missing_words:
        return []
    
    sections = []
    current_section = []
    
    for i, miss in enumerate(missing_words):
        if not current_section:
            current_section = [miss]
        else:
            # Check if consecutive
            if miss['index'] - missing_words[i-1]['index'] <= 3:
                current_section.append(miss)
            else:
                sections.append(current_section)
                current_section = [miss]
    
    if current_section:
        sections.append(current_section)
    
    return sections

def main():
    print("="*80)
    print("🔬 FINAL ASR VERIFICATION WITH ELEVENLABS")
    print("="*80)
    print()
    
    # Check file exists
    if not os.path.exists(AUDIO_FILE):
        print(f"❌ Audio file not found: {AUDIO_FILE}")
        sys.exit(1)
    
    file_size = os.path.getsize(AUDIO_FILE)
    duration_est = (file_size - 44) / (24000 * 2)
    
    print(f"📁 Audio File:")
    print(f"   Name: {AUDIO_FILE}")
    print(f"   Size: {file_size:,} bytes ({file_size/1024/1024:.1f} MB)")
    print(f"   Estimated duration: {duration_est:.1f}s ({duration_est/60:.1f} min)")
    print()
    
    print(f"📝 Reference Text:")
    clean_ref = clean_text(TEST_TEXT)
    ref_words = extract_words(clean_ref)
    print(f"   Total characters: {len(TEST_TEXT)}")
    print(f"   Clean characters: {len(clean_ref)}")
    print(f"   Total words: {len(ref_words)}")
    print(f"   Emotion tags: {len(re.findall(r'<[^>]+>', TEST_TEXT))}")
    print()
    
    # Transcribe
    result = transcribe_with_elevenlabs(AUDIO_FILE)
    if not result:
        sys.exit(1)
    
    print()
    
    # Analyze
    missing, transcript_words = analyze_timestamps(TEST_TEXT, result)
    
    if missing:
        print(f"\n⚠️  MISSING WORDS DETECTED:")
        print(f"   Total missing: {len(missing)}")
        
        # Group into sections
        ref_words_list = [w['word'] for w in ref_words]
        sections = find_missing_sections(TEST_TEXT, missing, ref_words_list)
        
        print(f"   Missing sections: {len(sections)}")
        print()
        
        for i, section in enumerate(sections):
            print(f"   Section {i+1}: {len(section)} words")
            first_idx = section[0]['index']
            last_idx = section[-1]['index']
            print(f"      Position: {first_idx}-{last_idx} of {len(ref_words)}")
            words_str = ' '.join([m['word'] for m in section[:10]])
            if len(section) > 10:
                words_str += f" ... (+{len(section)-10} more)"
            print(f"      Words: {words_str}")
            print(f"      Context: ...{section[0]['context_before']} [{section[0]['word']}...] {section[-1]['context_after']}...")
            print()
    else:
        print(f"\n✅ NO MISSING WORDS!")
        print(f"   All {len(ref_words)} reference words found in transcript")
    
    # Save detailed report
    report_file = "asr_verification_report.txt"
    with open(report_file, 'w') as f:
        f.write("="*80 + "\n")
        f.write("ELEVENLABS ASR VERIFICATION REPORT\n")
        f.write("="*80 + "\n\n")
        
        f.write(f"Audio File: {AUDIO_FILE}\n")
        f.write(f"Size: {file_size:,} bytes\n")
        f.write(f"Duration: {duration_est:.1f}s\n\n")
        
        f.write(f"Reference Words: {len(ref_words)}\n")
        f.write(f"Transcribed Words: {len(transcript_words)}\n")
        f.write(f"Accuracy: {len(transcript_words)/len(ref_words)*100:.1f}%\n")
        f.write(f"Missing: {len(missing)}\n\n")
        
        if missing:
            f.write("MISSING WORDS:\n")
            f.write("-"*80 + "\n")
            for m in missing:
                f.write(f"Word: {m['word']}\n")
                f.write(f"Position: {m['index']}/{len(ref_words)}\n")
                f.write(f"Context: ...{m['context_before']} [{m['word']}] {m['context_after']}...\n")
                f.write("\n")
        
        f.write("\n" + "="*80 + "\n")
        f.write("FULL TRANSCRIPT:\n")
        f.write("="*80 + "\n")
        f.write(result.get('text', '') + "\n")
        
        f.write("\n" + "="*80 + "\n")
        f.write("WORD-LEVEL TIMESTAMPS:\n")
        f.write("="*80 + "\n")
        for w in transcript_words:
            f.write(f"{w.get('start', 0):.2f}s - {w.get('end', 0):.2f}s: {w.get('text', '')}\n")
    
    print(f"\n📄 Detailed report saved: {report_file}")
    print()
    
    # Summary
    print("="*80)
    print("📊 SUMMARY")
    print("="*80)
    print()
    
    accuracy = len(transcript_words)/len(ref_words)*100
    
    if accuracy >= 95 and len(missing) < 20:
        print("✅ RESULT: Audio generation is WORKING WELL!")
        print(f"   Accuracy: {accuracy:.1f}%")
        print(f"   Missing: {len(missing)} words (acceptable)")
    elif accuracy >= 85:
        print("⚠️  RESULT: Audio generation is MOSTLY working")
        print(f"   Accuracy: {accuracy:.1f}%")
        print(f"   Missing: {len(missing)} words (some issues)")
    else:
        print("❌ RESULT: Audio generation has SIGNIFICANT ISSUES")
        print(f"   Accuracy: {accuracy:.1f}%")
        print(f"   Missing: {len(missing)} words (major problems)")
    
    print()
    
    if missing:
        # Check if ending is missing
        ref_words_list = [w['word'] for w in ref_words]
        last_10_ref = set(ref_words_list[-10:])
        trans_words_set = set([w['text'].lower() for w in transcript_words])
        last_10_missing = last_10_ref - trans_words_set
        
        if len(last_10_missing) > 5:
            print("🚨 CRITICAL: Ending is STILL missing!")
            print(f"   Last 10 reference words: {len(last_10_missing)} missing")
            print(f"   Missing from end: {', '.join(list(last_10_missing)[:5])}")
        else:
            print("✅ Ending appears to be present")
            print(f"   Last 10 words: {10-len(last_10_missing)}/10 found")
    
    print()

if __name__ == "__main__":
    main()
