"""
Pydantic schemas for custom prompt endpoints.
Story 3.11: Custom Prompts - Endpoint CRUD
"""

from datetime import datetime
from typing import Optional

from pydantic import BaseModel, Field, field_validator


class PromptCreate(BaseModel):
    """Schema for creating a prompt."""

    name: str = Field(..., min_length=1, max_length=255, description="Nom du prompt")
    content: str = Field(
        ..., min_length=1, max_length=10000, description="Contenu du prompt"
    )

    @field_validator("name", "content")
    @classmethod
    def strip_whitespace(cls, v: str) -> str:
        return v.strip()


class PromptUpdate(BaseModel):
    """Schema for updating a prompt (at least one field required)."""

    name: Optional[str] = Field(None, min_length=1, max_length=255)
    content: Optional[str] = Field(None, min_length=1, max_length=10000)

    @field_validator("name", "content")
    @classmethod
    def strip_whitespace(cls, v: Optional[str]) -> Optional[str]:
        return v.strip() if v else None

    def has_updates(self) -> bool:
        """Check if at least one field is provided for update."""
        return self.name is not None or self.content is not None


class PromptResponse(BaseModel):
    """Schema for prompt in response."""

    id: str
    name: str
    content: str
    created_at: Optional[datetime] = None
    updated_at: Optional[datetime] = None

    model_config = {"from_attributes": True}


class PromptListItem(BaseModel):
    """Schema for prompt in list (lighter version)."""

    id: str
    name: str
    content_preview: str = Field(
        ..., description="First 100 chars of content for list view"
    )
    created_at: Optional[datetime] = None
    updated_at: Optional[datetime] = None

    model_config = {"from_attributes": True}


class PromptListResponse(BaseModel):
    """Schema for prompts list response."""

    data: list[PromptListItem] = []
    meta: dict = Field(default_factory=dict)


class PromptDetailResponse(BaseModel):
    """Schema for single prompt response."""

    data: PromptResponse
    meta: dict = Field(default_factory=dict)
