"""
IndicWav2Vec Validator
======================

Uses AI4Bharat's IndicWav2Vec models for ASR and alignment.
https://github.com/AI4Bharat/indicwav2vec

Supports:
- Language-specific fine-tuned models with LM decoding
- CTC-based forced alignment
- Pre-trained on 40 Indian languages
"""
import os
import time
from typing import Optional, List, Dict, Any
from pathlib import Path

from .base import (
    BaseValidator, 
    ValidationResult, 
    WordAlignment,
    ValidatorStatus,
    normalize_language_code
)


class IndicWav2VecValidator(BaseValidator):
    """
    Validator using AI4Bharat's IndicWav2Vec models.
    
    Provides:
    - ASR transcription with language model support
    - CTC-based forced alignment with word timestamps
    """
    
    name = "indicwav2vec"
    description = "AI4Bharat IndicWav2Vec - Multilingual Indian ASR & Alignment"
    
    # AI4Bharat model download URLs (fairseq format)
    MODEL_URLS = {
        "te": {
            "acoustic": "https://objectstore.e2enetworks.net/indic-superb/aaai_ckpts/models/te/te.pt",
            "dict": "https://objectstore.e2enetworks.net/indic-superb/aaai_ckpts/models/te/dict.ltr.txt",
            "lm": "https://objectstore.e2enetworks.net/indic-superb/aaai_ckpts/models/te/lm.binary",
            "lexicon": "https://objectstore.e2enetworks.net/indic-superb/aaai_ckpts/models/te/lexicon.lst",
        },
        "hi": {
            "acoustic": "https://objectstore.e2enetworks.net/indic-superb/aaai_ckpts/models/hi/hi.pt",
            "dict": "https://objectstore.e2enetworks.net/indic-superb/aaai_ckpts/models/hi/dict.ltr.txt",
            "lm": "https://objectstore.e2enetworks.net/indic-superb/aaai_ckpts/models/hi/lm.binary",
            "lexicon": "https://objectstore.e2enetworks.net/indic-superb/aaai_ckpts/models/hi/lexicon.lst",
        },
        "bn": {
            "acoustic": "https://objectstore.e2enetworks.net/indic-superb/aaai_ckpts/models/bn/bn.pt",
            "dict": "https://objectstore.e2enetworks.net/indic-superb/aaai_ckpts/models/bn/dict.ltr.txt",
            "lm": "https://objectstore.e2enetworks.net/indic-superb/aaai_ckpts/models/bn/lm.binary",
            "lexicon": "https://objectstore.e2enetworks.net/indic-superb/aaai_ckpts/models/bn/lexicon.lst",
        },
        "ta": {
            "acoustic": "https://objectstore.e2enetworks.net/indic-superb/aaai_ckpts/models/ta/ta.pt",
            "dict": "https://objectstore.e2enetworks.net/indic-superb/aaai_ckpts/models/ta/dict.ltr.txt",
            "lm": "https://objectstore.e2enetworks.net/indic-superb/aaai_ckpts/models/ta/lm.binary",
            "lexicon": "https://objectstore.e2enetworks.net/indic-superb/aaai_ckpts/models/ta/lexicon.lst",
        },
        "mr": {
            "acoustic": "https://objectstore.e2enetworks.net/indic-superb/aaai_ckpts/models/mr/mr.pt",
            "dict": "https://objectstore.e2enetworks.net/indic-superb/aaai_ckpts/models/mr/dict.ltr.txt",
            "lm": "https://objectstore.e2enetworks.net/indic-superb/aaai_ckpts/models/mr/lm.binary",
            "lexicon": "https://objectstore.e2enetworks.net/indic-superb/aaai_ckpts/models/mr/lexicon.lst",
        },
        "gu": {
            "acoustic": "https://objectstore.e2enetworks.net/indic-superb/aaai_ckpts/models/gu/gu.pt",
            "dict": "https://objectstore.e2enetworks.net/indic-superb/aaai_ckpts/models/gu/dict.ltr.txt",
            "lm": "https://objectstore.e2enetworks.net/indic-superb/aaai_ckpts/models/gu/lm.binary",
            "lexicon": "https://objectstore.e2enetworks.net/indic-superb/aaai_ckpts/models/gu/lexicon.lst",
        },
        "or": {
            "acoustic": "https://objectstore.e2enetworks.net/indic-superb/aaai_ckpts/models/or/or.pt",
            "dict": "https://objectstore.e2enetworks.net/indic-superb/aaai_ckpts/models/or/dict.ltr.txt",
            "lm": "https://objectstore.e2enetworks.net/indic-superb/aaai_ckpts/models/or/lm.binary",
            "lexicon": "https://objectstore.e2enetworks.net/indic-superb/aaai_ckpts/models/or/lexicon.lst",
        },
        "kn": {
            "acoustic": "https://indic-asr-public.objectstore.e2enetworks.net/indic-superb/models/acoustic/kannada.pt",
            "dict": "https://indic-asr-public.objectstore.e2enetworks.net/indic-superb/models/acoustic/kannada.dict.txt",
            "lm": "https://indic-asr-public.objectstore.e2enetworks.net/indic-superb/models/kenlm/kannada/lm.binary",
            "lexicon": "https://indic-asr-public.objectstore.e2enetworks.net/indic-superb/models/kenlm/kannada/lexicon.lst",
        },
        "ml": {
            "acoustic": "https://indic-asr-public.objectstore.e2enetworks.net/indic-superb/models/acoustic/malayalam.pt",
            "dict": "https://indic-asr-public.objectstore.e2enetworks.net/indic-superb/models/acoustic/malayalam.dict.txt",
            "lm": "https://indic-asr-public.objectstore.e2enetworks.net/indic-superb/models/kenlm/malayalam/lm.binary",
            "lexicon": "https://indic-asr-public.objectstore.e2enetworks.net/indic-superb/models/kenlm/malayalam/lexicon.lst",
        },
    }
    
    # Fallback HuggingFace models (CTC fine-tuned)
    HF_MODELS = {
        "te": "anuragshas/wav2vec2-large-xlsr-53-telugu",
        "hi": "theainerd/Wav2Vec2-large-xlsr-hindi",
        "bn": "ai4bharat/indicwav2vec_v1_bengali",
        "ta": "Harveenchadha/vakyansh-wav2vec2-tamil-tam-250",
        "kn": "Harveenchadha/vakyansh-wav2vec2-kannada-knm-560",
        "ml": "gvs/wav2vec2-large-xlsr-malayalam",
        "gu": "Harveenchadha/vakyansh-wav2vec2-gujarati-gnm-100",
        "mr": "Harveenchadha/vakyansh-wav2vec2-marathi-mrm-100",
    }
    
    def __init__(
        self,
        enabled: bool = True,
        models_dir: str = "./models/indicwav2vec",
        device: str = "auto",
        language: str = "te",
        use_lm: bool = False,  # LM decoding requires fairseq
        **kwargs
    ):
        """
        Initialize IndicWav2Vec validator.
        
        Args:
            enabled: Whether validator is active
            models_dir: Directory for AI4Bharat models
            device: "cuda", "cpu", or "auto"
            language: Default language code
            use_lm: Use language model for decoding (requires fairseq models)
        """
        super().__init__(enabled=enabled, **kwargs)
        self.models_dir = Path(models_dir)
        self.device_preference = device
        self.language = normalize_language_code(language)
        self.use_lm = use_lm
        self.processor = None
        self.model = None
        self.device = None
        self._model_name = None
        self._model_type = None  # "fairseq" or "huggingface"
        self._current_language = None
        
    def _get_model_files(self, language: str) -> Dict[str, Path]:
        """Get paths to model files for given language."""
        lang_code = normalize_language_code(language)
        lang_dir = self.models_dir / lang_code
        
        return {
            "acoustic": lang_dir / f"{lang_code}.pt",
            "dict": lang_dir / "dict.ltr.txt",
            "lm": lang_dir / "lm.binary",
            "lexicon": lang_dir / "lexicon.lst",
        }
    
    def _download_models(self, language: str) -> bool:
        """Download AI4Bharat models for language."""
        lang_code = normalize_language_code(language)
        
        if lang_code not in self.MODEL_URLS:
            print(f"[{self.name}] No AI4Bharat models for {lang_code}")
            return False
        
        try:
            import urllib.request
            
            urls = self.MODEL_URLS[lang_code]
            lang_dir = self.models_dir / lang_code
            lang_dir.mkdir(parents=True, exist_ok=True)
            
            files = self._get_model_files(lang_code)
            
            for file_type, url in urls.items():
                out_path = files[file_type]
                
                # Use standard naming
                if file_type == "acoustic":
                    out_path = lang_dir / f"{lang_code}.pt"
                elif file_type == "dict":
                    out_path = lang_dir / "dict.ltr.txt"
                elif file_type == "lm":
                    out_path = lang_dir / "lm.binary"
                elif file_type == "lexicon":
                    out_path = lang_dir / "lexicon.lst"
                
                if not out_path.exists():
                    print(f"[{self.name}] Downloading {file_type}...")
                    urllib.request.urlretrieve(url, out_path)
            
            print(f"[{self.name}] Models downloaded for {lang_code}")
            return True
            
        except Exception as e:
            print(f"[{self.name}] Download failed: {e}")
            return False
        
    def setup(self, language: str = None) -> bool:
        """Load the Wav2Vec2 model and processor."""
        try:
            import torch
            
            # Determine device
            if self.device_preference == "auto":
                self.device = "cuda" if torch.cuda.is_available() else "cpu"
            else:
                self.device = self.device_preference
                
            print(f"[{self.name}] Loading model on {self.device}...")
            
            lang_code = normalize_language_code(language or self.language)
            self._current_language = lang_code
            
            # Try fairseq models if LM requested
            if self.use_lm:
                files = self._get_model_files(lang_code)
                if not files["acoustic"].exists():
                    self._download_models(lang_code)
                
                if files["acoustic"].exists():
                    return self._setup_fairseq(lang_code)
            
            # Use HuggingFace models
            return self._setup_huggingface(lang_code)
            
        except ImportError as e:
            print(f"[{self.name}] Missing dependency: {e}")
            return False
        except Exception as e:
            print(f"[{self.name}] Setup error: {e}")
            import traceback
            traceback.print_exc()
            return False
    
    def _setup_fairseq(self, language: str) -> bool:
        """Setup using fairseq models with LM."""
        try:
            # Fairseq setup is complex - fall back to HuggingFace for now
            print(f"[{self.name}] Fairseq LM decoding not yet implemented, using HuggingFace")
            return self._setup_huggingface(language)
            
        except Exception as e:
            print(f"[{self.name}] Fairseq setup failed: {e}")
            return self._setup_huggingface(language)
    
    def _setup_huggingface(self, language: str) -> bool:
        """Setup using HuggingFace model."""
        try:
            from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
            
            lang_code = normalize_language_code(language)
            
            # Get HuggingFace model name
            self._model_name = self.HF_MODELS.get(
                lang_code, 
                "anuragshas/wav2vec2-large-xlsr-53-telugu"  # Default to Telugu
            )
            
            print(f"[{self.name}] Using HuggingFace model: {self._model_name}")
            
            self.processor = Wav2Vec2Processor.from_pretrained(self._model_name)
            self.model = Wav2Vec2ForCTC.from_pretrained(self._model_name)
            self.model.to(self.device)
            self.model.eval()
            
            self._model_type = "huggingface"
            print(f"[{self.name}] Model loaded successfully")
            return True
            
        except Exception as e:
            print(f"[{self.name}] HuggingFace setup failed: {e}")
            import traceback
            traceback.print_exc()
            return False
    
    def _load_audio(self, audio_path: str, target_sr: int = 16000):
        """Load and resample audio to target sample rate."""
        import soundfile as sf
        import torch
        import numpy as np
        
        # Use soundfile directly (avoids torchaudio torchcodec issues)
        data, sample_rate = sf.read(audio_path)
        
        # Convert to torch tensor
        waveform = torch.from_numpy(data).float()
        
        # Ensure 2D tensor (channels, samples)
        if len(waveform.shape) == 1:
            waveform = waveform.unsqueeze(0)
        elif len(waveform.shape) == 2 and waveform.shape[1] < waveform.shape[0]:
            # Transpose if shape is (samples, channels)
            waveform = waveform.T
        
        # Convert to mono if stereo
        if waveform.shape[0] > 1:
            waveform = waveform.mean(dim=0, keepdim=True)
        
        # Resample if needed
        if sample_rate != target_sr:
            import torchaudio
            resampler = torchaudio.transforms.Resample(sample_rate, target_sr)
            waveform = resampler(waveform)
            
        return waveform.squeeze(), target_sr
    
    def _get_word_alignments_ctc(
        self,
        logits,
        transcription: str,
        audio_duration: float
    ) -> List[WordAlignment]:
        """
        Extract word-level alignments from CTC logits.
        Uses proportional alignment based on character count.
        """
        import torch
        
        words = transcription.split()
        if not words:
            return []
            
        # Simple proportional alignment based on character count
        total_chars = sum(len(w) for w in words)
        if total_chars == 0:
            return []
            
        alignments = []
        current_time = 0.0
        
        for word in words:
            word_duration = (len(word) / total_chars) * audio_duration
            alignments.append(WordAlignment(
                word=word,
                start_time=current_time,
                end_time=current_time + word_duration,
                confidence=None
            ))
            current_time += word_duration
            
        return alignments
    
    def validate(
        self,
        audio_path: str,
        reference_text: Optional[str] = None,
        language: str = "te"
    ) -> ValidationResult:
        """
        Process audio through IndicWav2Vec.
        
        Args:
            audio_path: Path to audio file
            reference_text: Optional reference transcription
            language: Language code
            
        Returns:
            ValidationResult with ASR output and alignments
        """
        if not self.enabled:
            return ValidationResult(
                validator_name=self.name,
                audio_path=audio_path,
                success=False,
                error_message="Validator disabled"
            )
        
        start_time = time.time()
        lang_code = normalize_language_code(language)
        
        # Check if we need to reload model for different language
        if self._current_language != lang_code or self.model is None:
            if not self.setup(lang_code):
                return ValidationResult(
                    validator_name=self.name,
                    audio_path=audio_path,
                    success=False,
                    error_message="Model not loaded"
                )
        
        try:
            import torch
            
            # Load audio
            waveform, sr = self._load_audio(audio_path)
            audio_duration = len(waveform) / sr
            
            # Process through model
            inputs = self.processor(
                waveform.numpy(),
                sampling_rate=sr,
                return_tensors="pt",
                padding=True
            )
            
            input_values = inputs.input_values.to(self.device)
            
            with torch.no_grad():
                outputs = self.model(input_values)
                logits = outputs.logits
            
            # Decode transcription
            predicted_ids = torch.argmax(logits, dim=-1)
            transcription = self.processor.batch_decode(predicted_ids)[0]
            
            # Get alignments (basic CTC-based)
            alignments = self._get_word_alignments_ctc(
                logits.cpu(),
                transcription,
                audio_duration
            )
            
            # Calculate confidence from logits
            probs = torch.softmax(logits, dim=-1)
            max_probs = probs.max(dim=-1).values
            overall_confidence = max_probs.mean().item()
            
            processing_time = time.time() - start_time
            
            return ValidationResult(
                validator_name=self.name,
                audio_path=audio_path,
                success=True,
                transcription=transcription,
                word_alignments=alignments,
                overall_confidence=overall_confidence,
                processing_time_sec=processing_time,
                audio_duration_sec=audio_duration,
                raw_output={
                    "language": lang_code,
                    "model": self._model_name,
                    "model_type": self._model_type
                }
            )
            
        except Exception as e:
            import traceback
            traceback.print_exc()
            return ValidationResult(
                validator_name=self.name,
                audio_path=audio_path,
                success=False,
                error_message=str(e),
                processing_time_sec=time.time() - start_time
            )
    
    def cleanup(self):
        """Release GPU memory."""
        if self.model is not None:
            del self.model
            self.model = None
        if self.processor is not None:
            del self.processor
            self.processor = None
            
        import torch
        if torch.cuda.is_available():
            torch.cuda.empty_cache()
