#!/usr/bin/env python3
"""
MCP Protocol Results

This module contains result classes for MCP (Model Context Protocol) responses.
These classes define the structure of data returned by various commands.
"""

from typing import Annotated, Any

from pydantic import BaseModel, ConfigDict, Field, field_validator

from .type_models import CircuitConnection, CircuitElement


class ElementCreationResult(BaseModel):
    """Result data for element creation"""

    element_id: Annotated[int, Field(gt=0)]

    model_config = ConfigDict(extra="forbid")


class ElementListResult(BaseModel):
    """Result data for element listing"""

    elements: list[CircuitElement] = Field(description="List of elements")

    model_config = ConfigDict(extra="forbid")


class ConnectionListResult(BaseModel):
    """Result data for connection listing"""

    connections: list[CircuitConnection] = Field(description="List of connections")

    model_config = ConfigDict(extra="forbid")


class OutputValueResult(BaseModel):
    """Result data for output value queries"""

    value: bool = Field(description="Output value")

    model_config = ConfigDict(extra="forbid")


# Test Result Models
class TestResults(BaseModel):
    """Model for test execution results"""

    total: Annotated[int, Field(ge=0)]
    passed: Annotated[int, Field(ge=0)]
    failed: Annotated[int, Field(ge=0)]
    errors: list[str] = Field(default_factory=list, description="List of actual failure error messages")
    known_issues: list[str] = Field(default_factory=list, description="List of documented limitations")
    process_issues: list[str] = Field(default_factory=list, description="List of process/stability issues")

    @field_validator("passed", "failed")
    @classmethod
    def validate_totals(cls, v: int, info: Any) -> int:
        """Ensure passed + failed <= total"""
        if info.data and "total" in info.data:
            total = info.data["total"]
            passed = info.data.get("passed", 0) if info.field_name == "failed" else v
            failed = v if info.field_name == "failed" else info.data.get("failed", 0)
            if passed + failed > total:
                raise ValueError("passed + failed cannot exceed total")
        return v

    model_config = ConfigDict(extra="forbid")
