"""Models for UI component processing service."""

from enum import Enum
from typing import Any

from pydantic import BaseModel, Field, model_validator


class UIComponentDataType(str, Enum):
    """Supported UI component data types."""

    TEXT = "text"
    MARKDOWN = "markdown"
    HTML = "html"
    CODE = "code"
    JSON = "json"
    TABLE = "table"
    IMAGE = "image"
    VIDEO = "video"
    AUDIO = "audio"
    FILES = "files"
    CHART = "chart"
    MERMAID = "mermaid"
    MAP = "map"
    SEARCH = "search"
    TOOL_USE = "toolUse"
    OPEN_TAB = "openTab"
    MCP_UI = "mcpUi"
    WIDGET = "widget"


class UIComponentRequest(BaseModel):
    """Request model for UI component processing - executor-agnostic."""

    data_type: UIComponentDataType = Field(..., description="Type of UI component to render")
    data: dict[str, Any] | str = Field(
        ..., description="Component data payload or JSON schema for LLM mode"
    )
    use_llm: bool = Field(..., description="Whether to use LLM to generate component data")
    input_data: str | None = Field(
        None, description="Input payload or prompt for LLM (required when use_llm is True)"
    )
    llm_schema: dict[str, Any] | None = Field(
        None, description="JSON schema for LLM generation (required when use_llm is True)"
    )
    widget_template: dict[str, Any] | None = Field(
        None, description="Widget content template (required for widget data type)"
    )

    @model_validator(mode="after")
    def validate_llm_mode_requirements(self) -> "UIComponentRequest":
        """Validate that required fields are present when use_llm is True."""
        if self.use_llm:
            if self.input_data is None:
                raise ValueError("input_data is required when use_llm is True")
            if self.llm_schema is None:
                raise ValueError("llm_schema is required when use_llm is True")
        return self

    @model_validator(mode="after")
    def validate_widget_requirements(self) -> "UIComponentRequest":
        """Validate that widget_template is present for widget data type."""
        if self.data_type == UIComponentDataType.WIDGET and self.widget_template is None:
            raise ValueError("widget_template is required when data_type is 'widget'")
        return self
