"""AI Processor model."""

from __future__ import annotations

from typing import Any, Dict, Optional

from netorca_sdk.v2.base import BaseModel


class AIProcessor(BaseModel):
    """
    AI Processor model.

    Represents an AI processor that handles automated processing tasks.

    Attributes:
        id: Processor ID
        name: Processor name
        service: Associated service
        llm_model: Associated LLM model
        action_type: Type of action to perform
        active: Whether the processor is active
        prompt_template: The prompt template for the processor
    """

    @property
    def name(self) -> Optional[str]:
        """Get processor name."""
        return self.data.get("name")

    @property
    def service_id(self) -> Optional[int]:
        """Get service ID."""
        service = self.data.get("service")
        if isinstance(service, dict):
            return service.get("id")
        return service

    @property
    def service_name(self) -> Optional[str]:
        """Get service name."""
        service = self.data.get("service")
        if isinstance(service, dict):
            return service.get("name")
        return None

    @property
    def llm_model_id(self) -> Optional[int]:
        """Get LLM model ID."""
        llm_model = self.data.get("llm_model")
        if isinstance(llm_model, dict):
            return llm_model.get("id")
        return llm_model

    @property
    def llm_model_name(self) -> Optional[str]:
        """Get LLM model name."""
        llm_model = self.data.get("llm_model")
        if isinstance(llm_model, dict):
            return llm_model.get("name")
        return None

    @property
    def action_type(self) -> Optional[str]:
        """Get action type."""
        return self.data.get("action_type")

    @property
    def active(self) -> bool:
        """Check if processor is active."""
        return self.data.get("active", False)

    @property
    def prompt_template(self) -> Optional[str]:
        """Get prompt template."""
        return self.data.get("prompt")

    @property
    def response_schema(self) -> Optional[Dict[str, Any]]:
        """Get response schema."""
        return self.data.get("response_schema")

    @property
    def description(self) -> Optional[str]:
        """Get processor description."""
        return self.data.get("description")

    def __repr__(self) -> str:
        return f"AIProcessor(id={self.id}, name='{self.name}', action_type='{self.action_type}')"


class AIProcessorHistory(BaseModel):
    """
    AI Processor History model.

    Represents a historical record of AI processor execution.
    """

    @property
    def processor_id(self) -> Optional[int]:
        """Get processor ID."""
        processor = self.data.get("processor") or self.data.get("ai_processor")
        if isinstance(processor, dict):
            return processor.get("id")
        return processor

    @property
    def input_data(self) -> Optional[Dict[str, Any]]:
        """Get input data."""
        return self.data.get("input_data")

    @property
    def output_data(self) -> Optional[Dict[str, Any]]:
        """Get output data."""
        return self.data.get("output_data")

    @property
    def status(self) -> Optional[str]:
        """Get execution status."""
        return self.data.get("status")

    @property
    def error_message(self) -> Optional[str]:
        """Get error message if any."""
        return self.data.get("error_message")

    @property
    def execution_time(self) -> Optional[float]:
        """Get execution time in seconds."""
        return self.data.get("execution_time")

    def __repr__(self) -> str:
        return f"AIProcessorHistory(id={self.id}, processor_id={self.processor_id}, status='{self.status}')"
