"""
User context extraction for personalization
"""

import re
from typing import Dict, Any, List, Optional

class UserContextExtractor:
    """Extract user context from conversation history for personalization"""
    
    @staticmethod
    def extract_user_context(conversation_history: str) -> Dict[str, Any]:
        """
        Extract user context from conversation history
        
        Args:
            conversation_history: Previous conversation history
            
        Returns:
            Dict[str, Any]: Extracted user context
        """
        from utils.agent_config import agent_config
        
        context = {
            "name": None,
            "company": None,
            "project_type": None,
            "technologies": [],
            "budget_mentioned": False,
            "email": None,
            "phone": None
        }
        
        # Extract name
        name_patterns = [
            r"User:.*?(?:my name is|I am|I'm) (\w+)",
            r"User:.*?(?:this is|it's) (\w+)",
            r"User:.*?(?:call me) (\w+)"
        ]
        
        for pattern in name_patterns:
            match = re.search(pattern, conversation_history, re.IGNORECASE)
            if match:
                extracted_name = match.group(1)
                # Make sure we don't confuse agent name with user name
                if extracted_name.lower() != agent_config.AGENT_NAME.lower():
                    context["name"] = extracted_name
                    break
        
        # Extract company
        company_patterns = [
            r"(?:I work for|I'm from|I am from|with) (?:a company called |)([A-Z][A-Za-z0-9\s&]+)",
            r"(?:my company|our company|our organization) (?:is|called) ([A-Z][A-Za-z0-9\s&]+)"
        ]
        
        for pattern in company_patterns:
            match = re.search(pattern, conversation_history, re.IGNORECASE)
            if match:
                context["company"] = match.group(1).strip()
                break
        
        # Extract project type
        project_patterns = [
            r"(?:looking to build|interested in|need|want) (?:a|an) ([a-z\s]+) (?:app|application|website|system|platform)",
            r"(?:project|work) (?:involves|is about|on) ([a-z\s]+)"
        ]
        
        for pattern in project_patterns:
            match = re.search(pattern, conversation_history, re.IGNORECASE)
            if match:
                context["project_type"] = match.group(1).strip()
                break
        
        # Extract technologies
        tech_keywords = [
            "React", "Angular", "Vue", "Node", "Python", "Django", "Flask", 
            "PHP", "Laravel", "Symfony", "Java", "Spring", ".NET", "C#",
            "React Native", "Flutter", "Swift", "Kotlin", "TensorFlow", 
            "PyTorch", "AWS", "Azure", "Google Cloud", "MongoDB", "PostgreSQL", 
            "MySQL", "Redis", "Elasticsearch", "Docker", "Kubernetes"
        ]
        
        for tech in tech_keywords:
            if re.search(r'\b' + re.escape(tech) + r'\b', conversation_history, re.IGNORECASE):
                context["technologies"].append(tech)
        
        # Check if budget was mentioned
        budget_patterns = [
            r"(?:budget|cost|price|pricing|quote|estimate)",
            r"(?:how much|how many|what is the cost)"
        ]
        
        for pattern in budget_patterns:
            if re.search(pattern, conversation_history, re.IGNORECASE):
                context["budget_mentioned"] = True
                break
        
        # Extract email
        email_pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
        email_match = re.search(email_pattern, conversation_history)
        if email_match:
            context["email"] = email_match.group(0)
        
        # Extract phone
        phone_pattern = r'\b(?:\+\d{1,3}[\s-]?)?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}\b'
        phone_match = re.search(phone_pattern, conversation_history)
        if phone_match:
            context["phone"] = phone_match.group(0)
        
        return context
    
    @staticmethod
    def get_personalization_text(context: Dict[str, Any]) -> str:
        """
        Generate personalization text based on extracted context
        
        Args:
            context: Extracted user context
            
        Returns:
            str: Personalization text for prompts
        """
        from utils.agent_config import agent_config
        
        personalization = ""
        
        # Add user name context only if it's valid and not the agent's name
        if context["name"] and context["name"].lower() != agent_config.AGENT_NAME.lower():
            personalization += f"\nThe user's name is {context['name']}. Address them by name occasionally."
            
        if context["company"]:
            personalization += f"\nThe user is from {context['company']}."
            
        if context["project_type"]:
            personalization += f"\nThe user is interested in a {context['project_type']} project."
            
        if context["technologies"]:
            tech_list = ", ".join(context["technologies"])
            personalization += f"\nThe user has mentioned these technologies: {tech_list}."
            
        if context["budget_mentioned"]:
            personalization += "\nThe user has inquired about pricing/budget."
            
        if context["email"]:
            personalization += f"\nThe user has shared their email: {context['email']}."
            
        if context["phone"]:
            personalization += f"\nThe user has shared their phone number: {context['phone']}."
        
        # Add explicit instruction to avoid confusion
        personalization += f"\nIMPORTANT: You are {agent_config.AGENT_NAME}, the AI assistant. Do not address yourself by name in responses."
            
        return personalization

# Create a singleton instance
user_context_extractor = UserContextExtractor()
