"""
MLL Intake Classifier Agent
"""

from pathlib import Path
import sys

sys.path.insert(0, str(Path(__file__).parent.parent))
from agents.base_agent import BaseAgent
from models.schemas import AgentType, AgentResponse, ConversationState
from utils.semantic_classifier import classify_request
from utils.embeddings import find_duplicates


class ClassifierAgent(BaseAgent):
    """Classifier Agent - Request Classification"""
    
    def __init__(self):
        super().__init__(AgentType.CLASSIFIER)
        self.tools = ['extract_entities', 'search_similar_requests']
    
    def get_system_prompt(self) -> str:
        return """You are the MLL Intake Classifier Agent, responsible for understanding and categorizing IT service requests."""
    
    def process(self, user_message: str, state: ConversationState) -> AgentResponse:
        """Classify the user's request"""
        
        classification = classify_request(user_message, state.collected_fields)
        duplicates = find_duplicates(user_message)
        
        # Generate classification summary
        parts = ["**Request Analysis:**\n"]
        parts.append(f"• **Type**: {classification.request_type.value.title()}")
        parts.append(f"• **Intent**: {classification.intent.value.replace('_', ' ').title()}")
        parts.append(f"• **Confidence**: {int(classification.confidence * 100)}%")
        
        if classification.similar_requests:
            parts.append(f"\n**Similar Requests Found**: {len(classification.similar_requests)}")
        
        if duplicates and duplicates[0].get('similarity_score', 0) > 0.9:
            parts.append(f"\n⚠️ **Potential Duplicate**: {duplicates[0].get('id')}")
        
        response_message = '\n'.join(parts)
        
        # Determine next agent
        type_to_agent = {
            'intake': AgentType.INTAKE,
            'network': AgentType.NETWORK,
            'incident': AgentType.INCIDENT,
            'access': AgentType.ACCESS,
            'status': AgentType.STATUS,
        }
        next_agent = type_to_agent.get(classification.request_type.value, AgentType.SUPERVISOR)
        
        return AgentResponse(
            agent=self.agent_type,
            message=response_message,
            next_agent=next_agent,
            state_updates={'classification': classification.model_dump()},
            confidence=classification.confidence
        )


classifier_agent = ClassifierAgent()
