"""
MLL Intake Data Loader
Load and process historical data from IRIS and Jira Excel files
"""

import re
from pathlib import Path
from typing import List, Dict, Any, Optional
import pandas as pd
from loguru import logger

import sys
sys.path.insert(0, str(Path(__file__).parent.parent))
from config import settings, PROJECT_ROOT


class DataLoader:
    """Load and process MLL historical data"""
    
    def __init__(self):
        self.iris_data: Optional[pd.DataFrame] = None
        self.epics_data: Optional[pd.DataFrame] = None
        self.stories_data: Optional[pd.DataFrame] = None
        self._loaded = False
    
    def load_all(self) -> bool:
        """Load all data sources"""
        try:
            self.load_iris_data()
            self.load_epics_data()
            self.load_stories_data()
            self._loaded = True
            logger.info("All data sources loaded successfully")
            return True
        except Exception as e:
            logger.error(f"Error loading data: {e}")
            return False
    
    def load_iris_data(self) -> pd.DataFrame:
        """Load IRIS Solution Intake requests"""
        path = settings.data.iris_path
        if not path.exists():
            path = PROJECT_ROOT / "data" / "IRIS_Solution_Intake_requests.xlsx"
        
        if path.exists():
            self.iris_data = pd.read_excel(path)
            logger.info(f"Loaded {len(self.iris_data)} IRIS records")
        else:
            logger.warning(f"IRIS data not found at {path}")
            self.iris_data = pd.DataFrame()
        
        return self.iris_data
    
    def load_epics_data(self) -> pd.DataFrame:
        """Load Jira Epics"""
        path = settings.data.epics_path
        if not path.exists():
            path = PROJECT_ROOT / "data" / "MLL_JIRA_Epics_extract.xlsx"
        
        if path.exists():
            self.epics_data = pd.read_excel(path)
            logger.info(f"Loaded {len(self.epics_data)} Jira Epics")
        else:
            logger.warning(f"Epics data not found at {path}")
            self.epics_data = pd.DataFrame()
        
        return self.epics_data
    
    def load_stories_data(self) -> pd.DataFrame:
        """Load Jira User Stories"""
        path = settings.data.stories_path
        if not path.exists():
            path = PROJECT_ROOT / "data" / "MLL_JIRA_Userstory_extracts.xlsx"
        
        if path.exists():
            self.stories_data = pd.read_excel(path)
            logger.info(f"Loaded {len(self.stories_data)} User Stories")
        else:
            logger.warning(f"Stories data not found at {path}")
            self.stories_data = pd.DataFrame()
        
        return self.stories_data
    
    def get_documents_for_embedding(self) -> List[Dict[str, Any]]:
        """Extract documents for embedding"""
        documents = []
        
        # Process IRIS records
        if self.iris_data is not None and len(self.iris_data) > 0:
            for _, row in self.iris_data.iterrows():
                doc_id = str(row.get('number', ''))
                if not doc_id:
                    continue
                
                text_parts = []
                if pd.notna(row.get('short_description')):
                    text_parts.append(str(row['short_description']))
                if pd.notna(row.get('description')):
                    text_parts.append(str(row['description']))
                
                if text_parts:
                    documents.append({
                        'id': doc_id,
                        'text': ' '.join(text_parts),
                        'metadata': {'source': 'iris', 'type': 'ritm'}
                    })
        
        # Process Jira Epics
        if self.epics_data is not None and len(self.epics_data) > 0:
            key_col = next((c for c in ['Issue key', 'Key', 'key'] if c in self.epics_data.columns), None)
            summary_col = next((c for c in ['Summary', 'summary'] if c in self.epics_data.columns), None)
            
            if key_col and summary_col:
                for _, row in self.epics_data.iterrows():
                    doc_id = str(row.get(key_col, ''))
                    if not doc_id:
                        continue
                    
                    text = str(row.get(summary_col, ''))
                    if text:
                        documents.append({
                            'id': doc_id,
                            'text': text,
                            'metadata': {'source': 'jira', 'type': 'epic'}
                        })
        
        logger.info(f"Prepared {len(documents)} documents for embedding")
        return documents
    
    def search_iris_by_ritm(self, ritm_number: str) -> Optional[Dict[str, Any]]:
        """Search IRIS data by RITM number"""
        if self.iris_data is None or len(self.iris_data) == 0:
            return None
        
        num_col = next((c for c in ['number', 'Number'] if c in self.iris_data.columns), None)
        if not num_col:
            return None
        
        matches = self.iris_data[self.iris_data[num_col].astype(str).str.contains(ritm_number, case=False, na=False)]
        
        if len(matches) > 0:
            return matches.iloc[0].to_dict()
        return None
    
    def search_epics_by_key(self, jira_key: str) -> Optional[Dict[str, Any]]:
        """Search Jira Epics by key"""
        if self.epics_data is None or len(self.epics_data) == 0:
            return None
        
        key_col = next((c for c in ['Issue key', 'Key', 'key'] if c in self.epics_data.columns), None)
        if not key_col:
            return None
        
        matches = self.epics_data[self.epics_data[key_col].astype(str).str.upper() == jira_key.upper()]
        
        if len(matches) > 0:
            return matches.iloc[0].to_dict()
        return None
    
    def get_statistics(self) -> Dict[str, Any]:
        """Get statistics about loaded data"""
        stats = {
            'iris_count': len(self.iris_data) if self.iris_data is not None else 0,
            'epics_count': len(self.epics_data) if self.epics_data is not None else 0,
            'stories_count': len(self.stories_data) if self.stories_data is not None else 0,
            'total_documents': 0
        }
        stats['total_documents'] = stats['iris_count'] + stats['epics_count'] + stats['stories_count']
        return stats


# Global instance
data_loader = DataLoader()
