"""Pydantic schemas for request/response validation."""

from datetime import datetime
from typing import Optional, Any
from pydantic import BaseModel, ConfigDict


# ---------------------------------------------------------------------------
# Project schemas
# ---------------------------------------------------------------------------
class ProjectBase(BaseModel):
    name: str
    description: Optional[str] = None
    video_path: Optional[str] = None
    status: Optional[str] = "pending"


class ProjectCreate(ProjectBase):
    pass


class ProjectUpdate(BaseModel):
    name: Optional[str] = None
    description: Optional[str] = None
    video_path: Optional[str] = None
    status: Optional[str] = None


class ProjectOut(ProjectBase):
    model_config = ConfigDict(from_attributes=True)

    id: int
    created_at: datetime
    updated_at: datetime


# ---------------------------------------------------------------------------
# Frame schemas
# ---------------------------------------------------------------------------
class FrameBase(BaseModel):
    frame_index: int
    image_url: str
    width: Optional[int] = None
    height: Optional[int] = None


class FrameCreate(FrameBase):
    project_id: int


class FrameOut(FrameBase):
    model_config = ConfigDict(from_attributes=True)

    id: int
    project_id: int
    created_at: datetime


# ---------------------------------------------------------------------------
# Template schemas
# ---------------------------------------------------------------------------
class TemplateBase(BaseModel):
    name: str
    color: str
    z_index: int = 0
    mapping_rules: Optional[dict[str, Any]] = None


class TemplateCreate(TemplateBase):
    pass


class TemplateUpdate(BaseModel):
    name: Optional[str] = None
    color: Optional[str] = None
    z_index: Optional[int] = None
    mapping_rules: Optional[dict[str, Any]] = None


class TemplateOut(TemplateBase):
    model_config = ConfigDict(from_attributes=True)

    id: int
    created_at: datetime


# ---------------------------------------------------------------------------
# Annotation schemas
# ---------------------------------------------------------------------------
class AnnotationBase(BaseModel):
    project_id: int
    frame_id: Optional[int] = None
    template_id: Optional[int] = None
    mask_data: Optional[dict[str, Any]] = None
    points: Optional[list[list[float]]] = None
    bbox: Optional[list[float]] = None


class AnnotationCreate(AnnotationBase):
    pass


class AnnotationUpdate(BaseModel):
    mask_data: Optional[dict[str, Any]] = None
    points: Optional[list[list[float]]] = None
    bbox: Optional[list[float]] = None
    template_id: Optional[int] = None


class AnnotationOut(AnnotationBase):
    model_config = ConfigDict(from_attributes=True)

    id: int
    created_at: datetime
    updated_at: datetime


# ---------------------------------------------------------------------------
# Mask schemas
# ---------------------------------------------------------------------------
class MaskBase(BaseModel):
    annotation_id: int
    mask_url: str
    format: str = "png"


class MaskCreate(MaskBase):
    pass


class MaskOut(MaskBase):
    model_config = ConfigDict(from_attributes=True)

    id: int
    created_at: datetime


# ---------------------------------------------------------------------------
# AI schemas
# ---------------------------------------------------------------------------
class PredictRequest(BaseModel):
    image_id: int
    prompt_type: str  # point / box / semantic
    prompt_data: Any


class PredictResponse(BaseModel):
    polygons: list[list[list[float]]]
    scores: Optional[list[float]] = None


# ---------------------------------------------------------------------------
# Export schemas
# ---------------------------------------------------------------------------
class ExportStatus(BaseModel):
    url: str
    format: str
