"""
MLL Intake Base Agent
"""

from abc import ABC, abstractmethod
from typing import Dict, Any, List, Optional
from pathlib import Path
import sys

sys.path.insert(0, str(Path(__file__).parent.parent))
from models.schemas import AgentType, AgentResponse, ConversationState
from config import settings


class BaseAgent(ABC):
    """Base class for all MLL Intake agents"""
    
    def __init__(self, agent_type: AgentType):
        self.agent_type = agent_type
        self.name = agent_type.value.title() + " Agent"
        self.tools: List[str] = []
        self.llm_client = None
        self._initialized = False
    
    def initialize(self, llm_client: Any) -> bool:
        """Initialize the agent with LLM client"""
        self.llm_client = llm_client
        self._initialized = True
        return True
    
    @abstractmethod
    def get_system_prompt(self) -> str:
        """Return the system prompt for this agent"""
        pass
    
    @abstractmethod
    def process(self, user_message: str, state: ConversationState) -> AgentResponse:
        """Process user message and return response"""
        pass
    
    def _call_llm(self, messages: List[Dict[str, str]], system_prompt: Optional[str] = None) -> str:
        """Call the LLM with messages"""
        if not self.llm_client:
            return "I apologize, but I'm experiencing technical difficulties."
        
        try:
            system = system_prompt or self.get_system_prompt()
            
            response = self.llm_client.messages.create(
                model=settings.model.model_name,
                max_tokens=settings.model.max_tokens,
                system=system,
                messages=messages
            )
            
            return response.content[0].text
            
        except Exception as e:
            return f"I encountered an error. Please try again."
    
    def get_required_fields(self) -> List[str]:
        """Return list of required fields for this agent"""
        return []
    
    def generate_question(self, field: str) -> str:
        """Generate a question to collect a specific field"""
        questions = {
            'site_id': "What site is this request for? Please provide the 4-digit Site ID.",
            'capability_center': "Which capability center does this fall under?",
            'business_problem': "What business problem are you trying to solve?",
            'business_value': "What is the expected business value?",
            'parent_ritm': "Do you have an existing MLL Intake RITM?",
            'application': "Which application do you need access to?",
            'affected_system': "Which system is affected?",
        }
        return questions.get(field, f"Please provide the {field.replace('_', ' ')}.")
