"""Symbol upload API schemas."""

from typing import List
from uuid import UUID

from pydantic import BaseModel, Field, field_validator


class SymbolEntry(BaseModel):
    """Single symbol definition for upload.

    Attributes:
        symbol: The symbol string to be mapped (e.g., "○", "×", "△").
        meaning: Human-readable meaning of the symbol.
    """

    symbol: str = Field(
        ...,
        min_length=1,
        max_length=50,
        description="Symbol string (1-50 characters)",
    )
    meaning: str = Field(
        ...,
        min_length=1,
        max_length=500,
        description="Human-readable meaning of the symbol (1-500 characters)",
    )

    model_config = {
        "json_schema_extra": {
            "examples": [
                {
                    "symbol": "○",
                    "meaning": "Applicable/Yes",
                },
                {
                    "symbol": "×",
                    "meaning": "Not applicable/No",
                },
            ]
        }
    }


class SymbolUploadRequest(BaseModel):
    """Request schema for custom symbol upload.

    Attributes:
        symbols: List of symbol definitions to upload (1-100 symbols).
    """

    symbols: List[SymbolEntry] = Field(
        ...,
        min_length=1,
        max_length=100,
        description="List of symbol definitions (1-100 symbols per request)",
    )

    @field_validator("symbols")
    @classmethod
    def validate_unique_symbols(cls, v: List[SymbolEntry]) -> List[SymbolEntry]:
        """Ensure no duplicate symbols in the request."""
        seen_symbols = set()
        for entry in v:
            if entry.symbol in seen_symbols:
                raise ValueError(f"Duplicate symbol '{entry.symbol}' in request")
            seen_symbols.add(entry.symbol)
        return v

    model_config = {
        "json_schema_extra": {
            "examples": [
                {
                    "symbols": [
                        {"symbol": "★", "meaning": "Priority item"},
                        {"symbol": "△", "meaning": "Pending review"},
                    ]
                }
            ]
        }
    }


class SymbolUploadResponse(BaseModel):
    """Response schema for custom symbol upload.

    Attributes:
        document_id: UUID of the document the symbols were added to.
        symbols_added: Number of symbols successfully added.
        symbols_updated: Number of existing symbols that were updated.
        message: Human-readable result message.
        reprocessing_triggered: Whether re-resolution was triggered.
    """

    document_id: UUID = Field(
        ...,
        description="UUID of the document the symbols were added to",
    )
    symbols_added: int = Field(
        ...,
        ge=0,
        description="Number of new symbols added",
    )
    symbols_updated: int = Field(
        default=0,
        ge=0,
        description="Number of existing symbols updated",
    )
    message: str = Field(
        ...,
        description="Human-readable result message",
    )
    reprocessing_triggered: bool = Field(
        default=False,
        description="Whether symbol re-resolution was triggered",
    )

    model_config = {
        "json_schema_extra": {
            "examples": [
                {
                    "document_id": "550e8400-e29b-41d4-a716-446655440000",
                    "symbols_added": 2,
                    "symbols_updated": 0,
                    "message": "Custom symbols will be applied to this document",
                    "reprocessing_triggered": False,
                },
                {
                    "document_id": "550e8400-e29b-41d4-a716-446655440000",
                    "symbols_added": 1,
                    "symbols_updated": 1,
                    "message": "Custom symbols added. Re-resolution triggered for processed document.",
                    "reprocessing_triggered": True,
                },
            ]
        }
    }


class SymbolDeleteResponse(BaseModel):
    """Response schema for symbol deletion.

    Attributes:
        document_id: UUID of the document.
        symbol: The symbol that was deleted.
        message: Human-readable result message.
        reprocessing_triggered: Whether re-resolution was triggered.
    """

    document_id: UUID = Field(
        ...,
        description="UUID of the document",
    )
    symbol: str = Field(
        ...,
        description="The symbol that was deleted",
    )
    message: str = Field(
        ...,
        description="Human-readable result message",
    )
    reprocessing_triggered: bool = Field(
        default=False,
        description="Whether symbol re-resolution was triggered",
    )

    model_config = {
        "json_schema_extra": {
            "examples": [
                {
                    "document_id": "550e8400-e29b-41d4-a716-446655440000",
                    "symbol": "★",
                    "message": "Symbol deleted successfully",
                    "reprocessing_triggered": True,
                }
            ]
        }
    }


class SymbolListResponse(BaseModel):
    """Response schema for listing symbols of a document.

    Attributes:
        document_id: UUID of the document.
        symbols: List of symbols for the document.
        total: Total number of symbols.
    """

    document_id: UUID = Field(
        ...,
        description="UUID of the document",
    )
    symbols: List[SymbolEntry] = Field(
        ...,
        description="List of symbols for this document",
    )
    total: int = Field(
        ...,
        ge=0,
        description="Total number of symbols",
    )
    custom_count: int = Field(
        default=0,
        ge=0,
        description="Number of custom (user-uploaded) symbols",
    )
    auto_detected_count: int = Field(
        default=0,
        ge=0,
        description="Number of auto-detected symbols",
    )

    model_config = {
        "json_schema_extra": {
            "examples": [
                {
                    "document_id": "550e8400-e29b-41d4-a716-446655440000",
                    "symbols": [
                        {"symbol": "○", "meaning": "Applicable/Yes"},
                        {"symbol": "×", "meaning": "Not applicable/No"},
                        {"symbol": "★", "meaning": "Priority item"},
                    ],
                    "total": 3,
                    "custom_count": 1,
                    "auto_detected_count": 2,
                }
            ]
        }
    }
