"""Admin Model Version Schemas."""

from datetime import datetime
from typing import Any

from pydantic import BaseModel, Field


class ModelVersionCreateRequest(BaseModel):
    """Request to create a model version."""

    version: str = Field(..., min_length=1, max_length=50, description="Semantic version")
    name: str = Field(..., min_length=1, max_length=255, description="Model name")
    model_path: str = Field(..., min_length=1, max_length=512, description="Path to model file")
    description: str | None = Field(None, description="Optional description")
    task_id: str | None = Field(None, description="Training task UUID")
    dataset_id: str | None = Field(None, description="Dataset UUID")
    metrics_mAP: float | None = Field(None, ge=0.0, le=1.0, description="Mean Average Precision")
    metrics_precision: float | None = Field(None, ge=0.0, le=1.0, description="Precision")
    metrics_recall: float | None = Field(None, ge=0.0, le=1.0, description="Recall")
    document_count: int = Field(0, ge=0, description="Documents used in training")
    training_config: dict[str, Any] | None = Field(None, description="Training configuration")
    file_size: int | None = Field(None, ge=0, description="Model file size in bytes")
    trained_at: datetime | None = Field(None, description="Training completion time")


class ModelVersionUpdateRequest(BaseModel):
    """Request to update a model version."""

    name: str | None = Field(None, min_length=1, max_length=255, description="Model name")
    description: str | None = Field(None, description="Description")
    status: str | None = Field(None, description="Status (inactive, archived)")


class ModelVersionItem(BaseModel):
    """Model version in list view."""

    version_id: str = Field(..., description="Version UUID")
    version: str = Field(..., description="Semantic version")
    name: str = Field(..., description="Model name")
    status: str = Field(..., description="Status (active, inactive, archived)")
    is_active: bool = Field(..., description="Is currently active for inference")
    model_type: str = Field(default="base", description="Model type (base or finetune)")
    base_model_version_id: str | None = Field(None, description="Base model version UUID (for fine-tuned models)")
    gating_status: str = Field(default="pending", description="Deployment gating status")
    metrics_mAP: float | None = Field(None, description="Mean Average Precision")
    document_count: int = Field(..., description="Documents used in training")
    trained_at: datetime | None = Field(None, description="Training completion time")
    activated_at: datetime | None = Field(None, description="Last activation time")
    created_at: datetime = Field(..., description="Creation timestamp")


class ModelVersionListResponse(BaseModel):
    """Paginated model version list."""

    total: int = Field(..., ge=0, description="Total model versions")
    limit: int = Field(..., ge=1, description="Page size")
    offset: int = Field(..., ge=0, description="Current offset")
    models: list[ModelVersionItem] = Field(default_factory=list, description="Model versions")


class ModelVersionDetailResponse(BaseModel):
    """Detailed model version info."""

    version_id: str = Field(..., description="Version UUID")
    version: str = Field(..., description="Semantic version")
    name: str = Field(..., description="Model name")
    description: str | None = Field(None, description="Description")
    model_path: str = Field(..., description="Path to model file")
    status: str = Field(..., description="Status (active, inactive, archived)")
    is_active: bool = Field(..., description="Is currently active for inference")
    model_type: str = Field(default="base", description="Model type (base or finetune)")
    base_model_version_id: str | None = Field(None, description="Base model version UUID")
    base_training_dataset_id: str | None = Field(None, description="Base training dataset UUID")
    gating_status: str = Field(default="pending", description="Deployment gating status")
    task_id: str | None = Field(None, description="Training task UUID")
    dataset_id: str | None = Field(None, description="Dataset UUID")
    metrics_mAP: float | None = Field(None, description="Mean Average Precision")
    metrics_precision: float | None = Field(None, description="Precision")
    metrics_recall: float | None = Field(None, description="Recall")
    document_count: int = Field(..., description="Documents used in training")
    training_config: dict[str, Any] | None = Field(None, description="Training configuration")
    file_size: int | None = Field(None, description="Model file size in bytes")
    trained_at: datetime | None = Field(None, description="Training completion time")
    activated_at: datetime | None = Field(None, description="Last activation time")
    created_at: datetime = Field(..., description="Creation timestamp")
    updated_at: datetime = Field(..., description="Last update timestamp")


class ModelVersionResponse(BaseModel):
    """Response for model version operation."""

    version_id: str = Field(..., description="Version UUID")
    status: str = Field(..., description="Model status")
    message: str = Field(..., description="Status message")


class ActiveModelResponse(BaseModel):
    """Response for active model query."""

    has_active_model: bool = Field(..., description="Whether an active model exists")
    model: ModelVersionItem | None = Field(None, description="Active model if exists")
