"""Admin Document Schemas."""

from __future__ import annotations

from datetime import datetime
from typing import TYPE_CHECKING

from pydantic import BaseModel, Field

from .enums import AutoLabelStatus, DocumentStatus

if TYPE_CHECKING:
    from .annotations import AnnotationItem
    from .training import TrainingHistoryItem


class DocumentUploadResponse(BaseModel):
    """Response for document upload."""

    document_id: str = Field(..., description="Document UUID")
    filename: str = Field(..., description="Original filename")
    file_size: int = Field(..., ge=0, description="File size in bytes")
    page_count: int = Field(..., ge=1, description="Number of pages")
    status: DocumentStatus = Field(..., description="Document status")
    category: str = Field(default="invoice", description="Document category (e.g., invoice, letter, receipt)")
    group_key: str | None = Field(None, description="User-defined group key")
    auto_label_started: bool = Field(
        default=False, description="Whether auto-labeling was started"
    )
    message: str = Field(..., description="Status message")


class DocumentItem(BaseModel):
    """Single document in list."""

    document_id: str = Field(..., description="Document UUID")
    filename: str = Field(..., description="Original filename")
    file_size: int = Field(..., ge=0, description="File size in bytes")
    page_count: int = Field(..., ge=1, description="Number of pages")
    status: DocumentStatus = Field(..., description="Document status")
    auto_label_status: AutoLabelStatus | None = Field(
        None, description="Auto-labeling status"
    )
    annotation_count: int = Field(default=0, ge=0, description="Number of annotations")
    upload_source: str = Field(default="ui", description="Upload source (ui or api)")
    batch_id: str | None = Field(None, description="Batch ID if uploaded via batch")
    group_key: str | None = Field(None, description="User-defined group key")
    category: str = Field(default="invoice", description="Document category (e.g., invoice, letter, receipt)")
    can_annotate: bool = Field(default=True, description="Whether document can be annotated")
    created_at: datetime = Field(..., description="Creation timestamp")
    updated_at: datetime = Field(..., description="Last update timestamp")


class DocumentListResponse(BaseModel):
    """Response for document list."""

    total: int = Field(..., ge=0, description="Total documents")
    limit: int = Field(..., ge=1, description="Page size")
    offset: int = Field(..., ge=0, description="Current offset")
    documents: list[DocumentItem] = Field(
        default_factory=list, description="Document list"
    )


class DocumentDetailResponse(BaseModel):
    """Response for document detail."""

    document_id: str = Field(..., description="Document UUID")
    filename: str = Field(..., description="Original filename")
    file_size: int = Field(..., ge=0, description="File size in bytes")
    content_type: str = Field(..., description="MIME type")
    page_count: int = Field(..., ge=1, description="Number of pages")
    status: DocumentStatus = Field(..., description="Document status")
    auto_label_status: AutoLabelStatus | None = Field(
        None, description="Auto-labeling status"
    )
    auto_label_error: str | None = Field(None, description="Auto-labeling error")
    upload_source: str = Field(default="ui", description="Upload source (ui or api)")
    batch_id: str | None = Field(None, description="Batch ID if uploaded via batch")
    group_key: str | None = Field(None, description="User-defined group key")
    category: str = Field(default="invoice", description="Document category (e.g., invoice, letter, receipt)")
    csv_field_values: dict[str, str] | None = Field(
        None, description="CSV field values if uploaded via batch"
    )
    can_annotate: bool = Field(default=True, description="Whether document can be annotated")
    annotation_lock_until: datetime | None = Field(
        None, description="Lock expiration time if document is locked"
    )
    annotations: list["AnnotationItem"] = Field(
        default_factory=list, description="Document annotations"
    )
    image_urls: list[str] = Field(
        default_factory=list, description="URLs to page images"
    )
    training_history: list["TrainingHistoryItem"] = Field(
        default_factory=list, description="Training tasks that used this document"
    )
    created_at: datetime = Field(..., description="Creation timestamp")
    updated_at: datetime = Field(..., description="Last update timestamp")


class DocumentStatsResponse(BaseModel):
    """Document statistics response."""

    total: int = Field(..., ge=0, description="Total documents")
    pending: int = Field(default=0, ge=0, description="Pending documents")
    auto_labeling: int = Field(default=0, ge=0, description="Auto-labeling documents")
    labeled: int = Field(default=0, ge=0, description="Labeled documents")
    exported: int = Field(default=0, ge=0, description="Exported documents")


class DocumentUpdateRequest(BaseModel):
    """Request for updating document metadata."""

    category: str | None = Field(None, description="Document category (e.g., invoice, letter, receipt)")
    group_key: str | None = Field(None, description="User-defined group key")


class DocumentCategoriesResponse(BaseModel):
    """Response for available document categories."""

    categories: list[str] = Field(..., description="List of available categories")
    total: int = Field(..., ge=0, description="Total number of categories")
