"""Event request/response schemas."""

from datetime import datetime

from pydantic import BaseModel, ConfigDict, Field, field_validator

from app.models.event import EventPhase, SeverityLevel


class EventCreate(BaseModel):
    """Payload for creating an event."""

    engagement_id: int = Field(..., gt=0)
    ts: datetime | None = None
    phase: EventPhase
    source: str = Field(..., min_length=1, max_length=100)
    title: str = Field(..., min_length=1, max_length=255)
    content: str = Field(..., min_length=1)
    tags: list[str] = Field(default_factory=list)
    severity: SeverityLevel
    evidence_paths: list[str] | None = None

    @field_validator("source", "title", "content")
    @classmethod
    def _strip_required_text(cls, value: str) -> str:
        text = value.strip()
        if not text:
            raise ValueError("field cannot be empty")
        return text

    @field_validator("tags")
    @classmethod
    def _strip_tags(cls, value: list[str]) -> list[str]:
        return [tag.strip() for tag in value if tag and tag.strip()]

    @field_validator("evidence_paths")
    @classmethod
    def _strip_paths(cls, value: list[str] | None) -> list[str] | None:
        if value is None:
            return None
        cleaned = [path.strip() for path in value if path and path.strip()]
        return cleaned or None


class EventRead(BaseModel):
    """Serialized event object."""

    model_config = ConfigDict(from_attributes=True)

    id: int
    engagement_id: int
    ts: datetime
    phase: EventPhase
    source: str
    title: str
    content: str
    tags: list[str]
    severity: SeverityLevel
    evidence_paths: list[str] | None = None
