"""
MLL Intake Data Models and Schemas
Pydantic models for type-safe data handling
"""

from datetime import datetime
from enum import Enum
from typing import Optional, List, Dict, Any
from pydantic import BaseModel, Field


class RequestType(str, Enum):
    """Types of MLL requests"""
    INTAKE = "intake"
    INCIDENT = "incident"
    NETWORK = "network"
    ACCESS = "access"
    STATUS = "status"
    UNKNOWN = "unknown"


class CapabilityCenter(str, Enum):
    """MLL Capability Centers"""
    AGILE_DEMAND = "agile_demand"
    APPROVED_PROJECT = "approved_project"
    ESTIMATE_DEMAND = "estimate_demand"
    TS_INTERNAL = "ts_internal"
    UNKNOWN = "unknown"


class Priority(str, Enum):
    """Network request priorities"""
    P1 = "P1"
    P2 = "P2"
    P3 = "P3"
    P4 = "P4"


class Intent(str, Enum):
    """User intents"""
    CREATE_REQUEST = "create_request"
    REPORT_INCIDENT = "report_incident"
    CHECK_STATUS = "check_status"
    GET_ACCESS = "get_access"
    GET_HELP = "get_help"
    MODIFY_REQUEST = "modify_request"
    UNKNOWN = "unknown"


class AgentType(str, Enum):
    """Agent types in the system"""
    SUPERVISOR = "supervisor"
    CLASSIFIER = "classifier"
    INTAKE = "intake"
    NETWORK = "network"
    INCIDENT = "incident"
    ACCESS = "access"
    STATUS = "status"
    KNOWLEDGE = "knowledge"


class ClassificationResult(BaseModel):
    """Result from the 4-stage classification pipeline"""
    request_type: RequestType = RequestType.UNKNOWN
    capability_center: CapabilityCenter = CapabilityCenter.UNKNOWN
    intent: Intent = Intent.UNKNOWN
    confidence: float = 0.0
    entities: Dict[str, Any] = Field(default_factory=dict)
    similar_requests: List[Dict[str, Any]] = Field(default_factory=list)
    routing_system: str = "IRIS"
    requires_network: bool = False
    requires_nexus_id: bool = False


class IntakeRequest(BaseModel):
    """Solution Intake Request"""
    site_id: Optional[str] = None
    site_name: Optional[str] = None
    zcode: Optional[str] = None
    capability_center: Optional[CapabilityCenter] = None
    nexus_id: Optional[str] = None
    business_problem: Optional[str] = None
    business_value: Optional[str] = None
    requires_network: bool = False
    attachments: List[str] = Field(default_factory=list)


class NetworkRequest(BaseModel):
    """Network/Firewall Request"""
    parent_ritm: Optional[str] = None
    request_type: List[str] = Field(default_factory=list)
    cable_drops: Optional[int] = None
    switch_names: List[str] = Field(default_factory=list)
    ip_addresses: List[str] = Field(default_factory=list)
    network_zone: Optional[str] = None
    priority: Priority = Priority.P3
    firewall_rules: Optional[str] = None


class IncidentReport(BaseModel):
    """Incident Report"""
    affected_system: Optional[str] = None
    error_message: Optional[str] = None
    impact: Optional[str] = None
    urgency: Optional[str] = None
    when_started: Optional[datetime] = None
    workaround_available: bool = False


class AccessRequest(BaseModel):
    """Access Request"""
    application: Optional[str] = None
    application_ci: Optional[str] = None
    login_id: Optional[str] = None
    account_type: str = "Standard Account"
    access_level: Optional[str] = None
    justification: Optional[str] = None


class StatusQuery(BaseModel):
    """Status Query"""
    ritm_number: Optional[str] = None
    jira_key: Optional[str] = None
    employee_id: Optional[str] = None


class Message(BaseModel):
    """Chat message"""
    role: str
    content: str
    timestamp: datetime = Field(default_factory=datetime.now)
    metadata: Dict[str, Any] = Field(default_factory=dict)


class ConversationState(BaseModel):
    """State of the conversation"""
    session_id: str
    messages: List[Message] = Field(default_factory=list)
    current_intent: Intent = Intent.UNKNOWN
    classification: Optional[ClassificationResult] = None
    collected_fields: Dict[str, Any] = Field(default_factory=dict)
    missing_fields: List[str] = Field(default_factory=list)
    active_agent: AgentType = AgentType.SUPERVISOR
    created_tickets: List[str] = Field(default_factory=list)
    intake_request: Optional[IntakeRequest] = None
    network_request: Optional[NetworkRequest] = None
    incident_report: Optional[IncidentReport] = None
    access_request: Optional[AccessRequest] = None
    status_query: Optional[StatusQuery] = None
    awaiting_confirmation: bool = False
    escalated: bool = False
    completed: bool = False


class AgentResponse(BaseModel):
    """Response from an agent"""
    agent: AgentType
    message: str
    next_agent: Optional[AgentType] = None
    state_updates: Dict[str, Any] = Field(default_factory=dict)
    tools_used: List[str] = Field(default_factory=list)
    confidence: float = 1.0
    requires_human: bool = False


class SimilarRequest(BaseModel):
    """Similar request from vector search"""
    key: str
    summary: str
    similarity_score: float
    request_type: Optional[str] = None
    status: Optional[str] = None


class KnowledgeResult(BaseModel):
    """Knowledge base search result"""
    title: str
    content: str
    source: str
    relevance_score: float
