"""
Dashboard API Schemas

Pydantic models for dashboard statistics and activity endpoints.
"""

from datetime import datetime
from typing import Any, Literal

from pydantic import BaseModel, Field


# Activity type literals for type safety
ActivityType = Literal[
    "document_uploaded",
    "annotation_modified",
    "training_completed",
    "training_failed",
    "model_activated",
]


class DashboardStatsResponse(BaseModel):
    """Response for dashboard statistics."""

    total_documents: int = Field(..., description="Total number of documents")
    annotation_complete: int = Field(
        ..., description="Documents with complete annotations"
    )
    annotation_incomplete: int = Field(
        ..., description="Documents with incomplete annotations"
    )
    pending: int = Field(..., description="Documents pending processing")
    completeness_rate: float = Field(
        ..., description="Annotation completeness percentage"
    )


class ActiveModelInfo(BaseModel):
    """Active model information."""

    version_id: str = Field(..., description="Model version UUID")
    version: str = Field(..., description="Model version string")
    name: str = Field(..., description="Model name")
    metrics_mAP: float | None = Field(None, description="Mean Average Precision")
    metrics_precision: float | None = Field(None, description="Precision score")
    metrics_recall: float | None = Field(None, description="Recall score")
    document_count: int = Field(0, description="Number of training documents")
    activated_at: datetime | None = Field(None, description="Activation timestamp")


class RunningTrainingInfo(BaseModel):
    """Running training task information."""

    task_id: str = Field(..., description="Training task UUID")
    name: str = Field(..., description="Training task name")
    status: str = Field(..., description="Training status")
    started_at: datetime | None = Field(None, description="Start timestamp")
    progress: int = Field(0, description="Training progress percentage")


class DashboardActiveModelResponse(BaseModel):
    """Response for dashboard active model endpoint."""

    model: ActiveModelInfo | None = Field(
        None, description="Active model info, null if none"
    )
    running_training: RunningTrainingInfo | None = Field(
        None, description="Running training task, null if none"
    )


class ActivityItem(BaseModel):
    """Single activity item."""

    type: ActivityType = Field(
        ...,
        description="Activity type: document_uploaded, annotation_modified, training_completed, training_failed, model_activated",
    )
    description: str = Field(..., description="Human-readable description")
    timestamp: datetime = Field(..., description="Activity timestamp")
    metadata: dict[str, Any] = Field(
        default_factory=dict, description="Additional metadata"
    )


class RecentActivityResponse(BaseModel):
    """Response for recent activity endpoint."""

    activities: list[ActivityItem] = Field(
        default_factory=list, description="List of recent activities"
    )
