from typing import Any
from uuid import UUID

from pydantic import BaseModel


class PatternBase(BaseModel):
    """Base pattern schema."""

    name: str
    slug: str
    description: str | None = None
    when_to_use: str | None = None
    pattern_type: str | None = None
    display_order: int | None = None


class PatternResponse(PatternBase):
    """Pattern response schema."""

    id: UUID
    question_count: int = 0

    model_config = {"from_attributes": True}


class PatternListResponse(BaseModel):
    """Response for pattern list."""

    items: list[PatternResponse]


class PatternDetailResponse(PatternBase):
    """Pattern detail with related questions."""

    id: UUID
    question_count: int = 0

    model_config = {"from_attributes": True}


# Tutorial-specific schemas


class CommonMistake(BaseModel):
    """A common mistake when using a pattern."""

    title: str
    description: str
    fix: str | None = None


class PatternVariation(BaseModel):
    """A variation of a pattern."""

    name: str
    description: str
    example: str | None = None


class RelatedPattern(BaseModel):
    """Brief info about a related pattern."""

    slug: str
    name: str
    description: str | None = None


class LearningQuestion(BaseModel):
    """Question for learning progression."""

    id: UUID
    title: str
    slug: str
    difficulty: str
    leetcode_id: int | None = None
    is_optimal: bool = False


class LearningProgression(BaseModel):
    """Curated learning path for a pattern."""

    warmup: list[LearningQuestion]
    core: list[LearningQuestion]
    challenge: list[LearningQuestion]


class VisualizationExample(BaseModel):
    """Interactive visualization example for a pattern."""

    id: str
    title: str
    input: dict[str, Any] | None = None
    code: str
    steps: list[dict[str, Any]]


class PatternTutorialResponse(PatternBase):
    """Full pattern tutorial with all educational content."""

    id: UUID
    question_count: int = 0

    # Tutorial content
    metaphor: str | None = None
    core_concept: str | None = None
    visualization: str | None = None
    code_template: str | None = None

    # Structured data
    recognition_signals: list[str] | None = None
    common_mistakes: list[CommonMistake] | None = None
    variations: list[PatternVariation] | None = None
    related_patterns: list[RelatedPattern] | None = None
    prerequisite_patterns: list[RelatedPattern] | None = None

    # Difficulty level (1-5)
    difficulty_level: int | None = None

    # Learning progression
    learning_progression: LearningProgression | None = None

    # Interactive visualization examples
    visualization_examples: list[VisualizationExample] | None = None

    model_config = {"from_attributes": True}
