"""Admin Annotation Schemas."""

from datetime import datetime

from pydantic import BaseModel, Field

from .enums import AnnotationSource


class BoundingBox(BaseModel):
    """Bounding box coordinates."""

    x: int = Field(..., ge=0, description="X coordinate (pixels)")
    y: int = Field(..., ge=0, description="Y coordinate (pixels)")
    width: int = Field(..., ge=1, description="Width (pixels)")
    height: int = Field(..., ge=1, description="Height (pixels)")


class AnnotationCreate(BaseModel):
    """Request to create an annotation."""

    page_number: int = Field(default=1, ge=1, description="Page number (1-indexed)")
    class_id: int = Field(..., ge=0, le=9, description="Class ID (0-9)")
    bbox: BoundingBox = Field(..., description="Bounding box in pixels")
    text_value: str | None = Field(None, description="Text value (optional)")


class AnnotationUpdate(BaseModel):
    """Request to update an annotation."""

    class_id: int | None = Field(None, ge=0, le=9, description="New class ID")
    bbox: BoundingBox | None = Field(None, description="New bounding box")
    text_value: str | None = Field(None, description="New text value")


class AnnotationItem(BaseModel):
    """Single annotation item."""

    annotation_id: str = Field(..., description="Annotation UUID")
    page_number: int = Field(..., ge=1, description="Page number")
    class_id: int = Field(..., ge=0, le=9, description="Class ID")
    class_name: str = Field(..., description="Class name")
    bbox: BoundingBox = Field(..., description="Bounding box in pixels")
    normalized_bbox: dict[str, float] = Field(
        ..., description="Normalized bbox (x_center, y_center, width, height)"
    )
    text_value: str | None = Field(None, description="Text value")
    confidence: float | None = Field(None, ge=0, le=1, description="Confidence score")
    source: AnnotationSource = Field(..., description="Annotation source")
    created_at: datetime = Field(..., description="Creation timestamp")


class AnnotationResponse(BaseModel):
    """Response for annotation operation."""

    annotation_id: str = Field(..., description="Annotation UUID")
    message: str = Field(..., description="Status message")


class AnnotationListResponse(BaseModel):
    """Response for annotation list."""

    document_id: str = Field(..., description="Document UUID")
    page_count: int = Field(..., ge=1, description="Total pages")
    total_annotations: int = Field(..., ge=0, description="Total annotations")
    annotations: list[AnnotationItem] = Field(
        default_factory=list, description="Annotation list"
    )


class AnnotationLockRequest(BaseModel):
    """Request to acquire annotation lock."""

    duration_seconds: int = Field(
        default=300,
        ge=60,
        le=3600,
        description="Lock duration in seconds (60-3600)",
    )


class AnnotationLockResponse(BaseModel):
    """Response for annotation lock operation."""

    document_id: str = Field(..., description="Document UUID")
    locked: bool = Field(..., description="Whether lock was acquired/released")
    lock_expires_at: datetime | None = Field(
        None, description="Lock expiration time"
    )
    message: str = Field(..., description="Status message")


class AutoLabelRequest(BaseModel):
    """Request to trigger auto-labeling."""

    field_values: dict[str, str] = Field(
        ...,
        description="Field values to match (e.g., {'invoice_number': '12345'})",
    )
    replace_existing: bool = Field(
        default=False, description="Replace existing auto annotations"
    )


class AutoLabelResponse(BaseModel):
    """Response for auto-labeling."""

    document_id: str = Field(..., description="Document UUID")
    status: str = Field(..., description="Auto-labeling status")
    annotations_created: int = Field(
        default=0, ge=0, description="Number of annotations created"
    )
    message: str = Field(..., description="Status message")


class AnnotationVerifyRequest(BaseModel):
    """Request to verify an annotation."""

    pass  # No body needed, just POST to verify


class AnnotationVerifyResponse(BaseModel):
    """Response for annotation verification."""

    annotation_id: str = Field(..., description="Annotation UUID")
    is_verified: bool = Field(..., description="Verification status")
    verified_at: datetime = Field(..., description="Verification timestamp")
    verified_by: str = Field(..., description="Admin token who verified")
    message: str = Field(..., description="Status message")


class AnnotationOverrideRequest(BaseModel):
    """Request to override an annotation."""

    bbox: dict[str, int] | None = Field(
        None, description="Updated bounding box {x, y, width, height}"
    )
    text_value: str | None = Field(None, description="Updated text value")
    class_id: int | None = Field(None, ge=0, le=9, description="Updated class ID")
    class_name: str | None = Field(None, description="Updated class name")
    reason: str | None = Field(None, description="Reason for override")


class AnnotationOverrideResponse(BaseModel):
    """Response for annotation override."""

    annotation_id: str = Field(..., description="Annotation UUID")
    source: str = Field(..., description="New source (manual)")
    override_source: str | None = Field(None, description="Original source (auto)")
    original_annotation_id: str | None = Field(None, description="Original annotation ID")
    message: str = Field(..., description="Status message")
    history_id: str = Field(..., description="History record UUID")
