"""Pydantic schemas for Review Note API requests and responses."""

from datetime import datetime

from pydantic import BaseModel, Field


class ReviewNoteCreateRequest(BaseModel):
    """Request body for creating a new review note."""

    asset_key: str = Field(..., description="Asset identifier (e.g., 'en', 'es_accessible')")
    timestamp_seconds: float = Field(..., ge=0, description="Video timestamp in seconds")
    content: str = Field(..., min_length=1, max_length=2000, description="Note content")


class ReviewNoteUpdateRequest(BaseModel):
    """Request body for updating an existing review note."""

    content: str = Field(..., min_length=1, max_length=2000, description="Updated note content")


class ReviewNoteResponse(BaseModel):
    """Response model for a single review note."""

    id: str
    job_id: str
    asset_key: str
    timestamp_seconds: float
    content: str
    user_id: str
    user_name: str
    created_at: str  # ISO format
    updated_at: str | None = None  # ISO format

    @classmethod
    def from_model(cls, note: dict) -> "ReviewNoteResponse":
        """Create a response from a MongoDB document."""
        return cls(
            id=str(note.get("_id", note.get("id"))),
            job_id=note["job_id"],
            asset_key=note["asset_key"],
            timestamp_seconds=note["timestamp_seconds"],
            content=note["content"],
            user_id=note["user_id"],
            user_name=note["user_name"],
            created_at=note["created_at"].isoformat() if isinstance(note["created_at"], datetime) else note["created_at"],
            updated_at=note["updated_at"].isoformat() if note.get("updated_at") and isinstance(note["updated_at"], datetime) else note.get("updated_at"),
        )


class ReviewNotesListResponse(BaseModel):
    """Response model for a list of review notes."""

    notes: list[ReviewNoteResponse]
    total: int
