#!/usr/bin/env python3
"""
ElevenLabs ASR Word-Level Verification of the Fix
Compare correct format vs wrong format at word level
"""

import os
import sys
import requests
import re
import json

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

# Reference text (corrected - no closing tags)
REFERENCE_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. 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. 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. 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! 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. 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! 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. 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! 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! There was no time to waste.

<excited> 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. <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. 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." 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! The demon lord let out a terrible scream as the divine light consumed him.

<laugh> 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.

<excited> 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.

<whisper> But Aisha remained humble. 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. 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!"""

AUDIO_FILE = "correct_format_test.wav"

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

def get_words(text):
    """Extract word list."""
    clean = clean_text(text)
    words = []
    for match in re.finditer(r'\b[\w]+\b', clean.lower()):
        words.append({
            'word': match.group(),
            'position': match.start(),
            'index': len(words)
        })
    return words

def transcribe_elevenlabs(audio_file):
    """Transcribe with ElevenLabs."""
    print(f"🎧 Transcribing with ElevenLabs API...")
    
    url = "https://api.elevenlabs.io/v1/audio-native"
    
    # Upload file
    with open(audio_file, 'rb') as f:
        files = {'file': (audio_file, f, 'audio/wav')}
        headers = {'xi-api-key': ELEVENLABS_API_KEY}
        
        # Try the audio-native endpoint
        response = requests.post(
            url,
            files=files,
            headers=headers,
            data={'model': 'eleven_multilingual_v2'}
        )
    
    if response.status_code != 200:
        print(f"   Trying alternative endpoint...")
        # Try speech-to-text endpoint
        url = "https://api.elevenlabs.io/v1/speech-to-text"
        with open(audio_file, 'rb') as f:
            files = {'audio': (audio_file, f, 'audio/wav')}
            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
    
    return response.json()

print("="*80)
print("🔬 ELEVENLABS ASR WORD-LEVEL VERIFICATION")
print("="*80)
print()

# File info
if not os.path.exists(AUDIO_FILE):
    print(f"❌ File not found: {AUDIO_FILE}")
    sys.exit(1)

file_size = os.path.getsize(AUDIO_FILE)
duration = (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"   Duration: {duration:.1f}s ({duration/60:.1f} min)")
print()

# Reference
ref_words = get_words(REFERENCE_TEXT)
print(f"📝 Reference:")
print(f"   Total words: {len(ref_words)}")
print(f"   Text length: {len(REFERENCE_TEXT)} chars")
print()

# Transcribe
result = transcribe_elevenlabs(AUDIO_FILE)

if not result:
    print("❌ Transcription failed - trying simpler approach...")
    print()
    print("Using curl to test API:")
    os.system(f'curl -X POST "https://api.elevenlabs.io/v1/speech-to-text" -H "xi-api-key: {ELEVENLABS_API_KEY}" -F "audio=@{AUDIO_FILE}" > elevenlabs_response.json 2>&1')
    print()
    
    if os.path.exists('elevenlabs_response.json'):
        with open('elevenlabs_response.json', 'r') as f:
            content = f.read()
            print("Response:")
            print(content[:500])
            
            try:
                result = json.loads(content)
            except:
                print("Failed to parse JSON")
                result = None

if not result:
    print("\n⚠️  ElevenLabs API not working, using OpenAI Whisper for word-level...")
    from openai import OpenAI
    
    OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY')
    if not OPENAI_API_KEY:
        print("❌ OPENAI_API_KEY also not set!")
        sys.exit(1)
    
    client = OpenAI(api_key=OPENAI_API_KEY)
    
    with open(AUDIO_FILE, 'rb') as f:
        transcription = client.audio.transcriptions.create(
            model="whisper-1",
            file=f,
            response_format="verbose_json"
        )
    
    transcript_text = transcription.text
    trans_words_data = transcription.words if hasattr(transcription, 'words') else []
    
    print(f"✅ Transcribed with OpenAI Whisper")
else:
    transcript_text = result.get('text', '')
    trans_words_data = result.get('words', [])
    print(f"✅ Transcribed with ElevenLabs")

print()

# Extract words from transcript
trans_words = []
if trans_words_data:
    for w in trans_words_data:
        word_text = w.get('word', w.get('text', '')).lower().strip()
        if word_text:
            trans_words.append({
                'word': word_text,
                'start': w.get('start', 0),
                'end': w.get('end', 0)
            })
else:
    # Fallback - just get words from text
    trans_words = [{'word': w, 'start': 0, 'end': 0} for w in re.findall(r'\b\w+\b', transcript_text.lower())]

print(f"📊 Transcription Results:")
print(f"   Words transcribed: {len(trans_words)}")
print(f"   Accuracy: {len(trans_words)/len(ref_words)*100:.1f}%")
print()

# Word-level matching
trans_word_set = set([w['word'] for w in trans_words])
ref_word_list = [w['word'] for w in ref_words]

missing_words = []
for i, word in enumerate(ref_word_list):
    if word not in trans_word_set:
        # Get context
        context_before = ' '.join(ref_word_list[max(0,i-3):i])
        context_after = ' '.join(ref_word_list[i+1:min(len(ref_word_list),i+4)])
        
        missing_words.append({
            'word': word,
            'index': i,
            'context': f"...{context_before} [{word}] {context_after}..."
        })

print(f"📉 Missing Words: {len(missing_words)}")
print()

# Analyze ending
ENDING_WORDS = [
    "day", "forward", "dedicated", "herself", "teaching",
    "next", "generation", "ensuring", "ready", "when",
    "darkness", "threatened", "again", "kingdom", "prospered",
    "watchful", "protection", "peace", "reigned", "many", "years", "come"
]

print(f"🎯 Critical Ending Analysis (22 words):")
ending_found = sum(1 for w in ENDING_WORDS if w in trans_word_set)
print(f"   Found: {ending_found}/{len(ENDING_WORDS)} ({ending_found/len(ENDING_WORDS)*100:.0f}%)")
print()

if ending_found >= 20:
    print("   ✅ EXCELLENT! Ending is complete")
elif ending_found >= 15:
    print("   ✅ GOOD! Most of ending present")
elif ending_found >= 10:
    print("   ⚠️  PARTIAL: Some ending present")
else:
    print("   ❌ POOR: Ending mostly missing")

print()

# Show missing from ending
ending_missing = [w for w in ENDING_WORDS if w not in trans_word_set]
if ending_missing:
    print(f"   Missing from ending: {', '.join(ending_missing[:10])}")
    if len(ending_missing) > 10:
        print(f"   ... and {len(ending_missing)-10} more")
    print()

# Last transcribed word
if trans_words:
    last_word = trans_words[-1]
    print(f"📍 Last Transcribed Word:")
    print(f"   Word: '{last_word['word']}'")
    if last_word['end'] > 0:
        print(f"   Timestamp: {last_word['end']:.1f}s")
    print()

# Comparison with previous results
print("="*80)
print("📊 COMPARISON: Wrong Format vs Correct Format")
print("="*80)
print()

print("| Metric | Wrong (with </tags>) | Correct (no </tags>) |")
print("|--------|----------------------|----------------------|")
print(f"| Duration | 472s | {duration:.0f}s |")
print(f"| Size | 22.6 MB | {file_size/1024/1024:.1f} MB |")
print(f"| Words | 702/762 (92.1%) | {len(trans_words)}/{len(ref_words)} ({len(trans_words)/len(ref_words)*100:.1f}%) |")
print(f"| Missing | 98 words | {len(missing_words)} words |")
print(f"| Ending words | 0/22 (0%) | {ending_found}/22 ({ending_found/22*100:.0f}%) |")
print()

# Save report
with open("elevenlabs_verification_report.txt", 'w') as f:
    f.write("ELEVENLABS ASR VERIFICATION - CORRECT FORMAT\n")
    f.write("="*80 + "\n\n")
    
    f.write(f"Audio: {AUDIO_FILE}\n")
    f.write(f"Duration: {duration:.1f}s\n")
    f.write(f"Reference words: {len(ref_words)}\n")
    f.write(f"Transcribed words: {len(trans_words)}\n")
    f.write(f"Accuracy: {len(trans_words)/len(ref_words)*100:.1f}%\n")
    f.write(f"Missing: {len(missing_words)}\n")
    f.write(f"Ending accuracy: {ending_found}/{len(ENDING_WORDS)} ({ending_found/len(ENDING_WORDS)*100:.0f}%)\n\n")
    
    if missing_words:
        f.write("MISSING WORDS:\n")
        f.write("-"*80 + "\n")
        for m in missing_words[:50]:
            f.write(f"Word {m['index']}: {m['word']}\n")
            f.write(f"  Context: {m['context']}\n")
        if len(missing_words) > 50:
            f.write(f"\n... and {len(missing_words)-50} more\n")
    
    f.write("\n" + "="*80 + "\n")
    f.write("TRANSCRIPT:\n")
    f.write("="*80 + "\n")
    f.write(transcript_text)

print("📄 Detailed report: elevenlabs_verification_report.txt")
print()

# Final verdict
print("="*80)
print("🎯 FINAL VERDICT")
print("="*80)
print()

if ending_found >= 20 and len(trans_words) >= 720:
    print("✅ FIX CONFIRMED!")
    print("   - Ending is present")
    print("   - Word-level accuracy high")
    print("   - Correct emotion tag format works!")
elif ending_found >= 15:
    print("✅ MAJOR IMPROVEMENT!")
    print("   - Most of ending present")
    print("   - Much better than before")
    print("   - Some minor issues remain")
else:
    print("⚠️  PARTIAL FIX")
    print("   - Some improvement but issues remain")
    print(f"   - Only {ending_found}/22 ending words found")

print()

