"""AI Validator model."""

from __future__ import annotations

from typing import Any, Dict, Optional

from netorca_sdk.v2.base import BaseModel


class AIValidator(BaseModel):
    """
    AI Validator model.

    Represents an AI-powered validator for validating submissions and changes.

    Attributes:
        id: Validator ID
        name: Validator name
        service: Associated service
        llm_model: Associated LLM model
        validation_prompt: The prompt used for validation
        active: Whether the validator is active
    """

    @property
    def name(self) -> Optional[str]:
        """Get validator 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 validation_prompt(self) -> Optional[str]:
        """Get validation prompt."""
        return self.data.get("validation_prompt")

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

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

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


class AIValidatorResponse(BaseModel):
    """
    AI Validator Response model.

    Represents a response from an AI validator execution.

    Attributes:
        id: Response ID
        ai_validator: Associated validator
        change_instance: Associated change instance
        state: Response state (e.g., 'approved', 'rejected')
        response: The AI response content
    """

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

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

    @property
    def change_instance_id(self) -> Optional[int]:
        """Get change instance ID."""
        ci = self.data.get("change_instance")
        if isinstance(ci, dict):
            return ci.get("id")
        return ci

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

    @property
    def response(self) -> Optional[str]:
        """Get AI response content."""
        return self.data.get("response")

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

    @property
    def is_approved(self) -> bool:
        """Check if response is approved."""
        return self.state == "approved"

    @property
    def is_rejected(self) -> bool:
        """Check if response is rejected."""
        return self.state == "rejected"

    def __repr__(self) -> str:
        return f"AIValidatorResponse(id={self.id}, validator_id={self.validator_id}, state='{self.state}')"
