"""Admin Augmentation Schemas."""

from datetime import datetime
from typing import Any

from pydantic import BaseModel, Field


class AugmentationParamsSchema(BaseModel):
    """Single augmentation parameters."""

    enabled: bool = Field(default=False, description="Whether this augmentation is enabled")
    probability: float = Field(
        default=0.5, ge=0, le=1, description="Probability of applying (0-1)"
    )
    params: dict[str, Any] = Field(
        default_factory=dict, description="Type-specific parameters"
    )


class AugmentationConfigSchema(BaseModel):
    """Complete augmentation configuration."""

    # Geometric transforms
    perspective_warp: AugmentationParamsSchema = Field(
        default_factory=AugmentationParamsSchema
    )

    # Degradation effects
    wrinkle: AugmentationParamsSchema = Field(default_factory=AugmentationParamsSchema)
    edge_damage: AugmentationParamsSchema = Field(
        default_factory=AugmentationParamsSchema
    )
    stain: AugmentationParamsSchema = Field(default_factory=AugmentationParamsSchema)

    # Lighting effects
    lighting_variation: AugmentationParamsSchema = Field(
        default_factory=AugmentationParamsSchema
    )
    shadow: AugmentationParamsSchema = Field(default_factory=AugmentationParamsSchema)

    # Blur effects
    gaussian_blur: AugmentationParamsSchema = Field(
        default_factory=AugmentationParamsSchema
    )
    motion_blur: AugmentationParamsSchema = Field(
        default_factory=AugmentationParamsSchema
    )

    # Noise effects
    gaussian_noise: AugmentationParamsSchema = Field(
        default_factory=AugmentationParamsSchema
    )
    salt_pepper: AugmentationParamsSchema = Field(
        default_factory=AugmentationParamsSchema
    )

    # Texture effects
    paper_texture: AugmentationParamsSchema = Field(
        default_factory=AugmentationParamsSchema
    )
    scanner_artifacts: AugmentationParamsSchema = Field(
        default_factory=AugmentationParamsSchema
    )

    # Global settings
    preserve_bboxes: bool = Field(
        default=True, description="Whether to adjust bboxes for geometric transforms"
    )
    seed: int | None = Field(default=None, description="Random seed for reproducibility")


class AugmentationTypeInfo(BaseModel):
    """Information about an augmentation type."""

    name: str = Field(..., description="Augmentation name")
    description: str = Field(..., description="Augmentation description")
    affects_geometry: bool = Field(
        ..., description="Whether this augmentation affects bbox coordinates"
    )
    stage: str = Field(..., description="Processing stage")
    default_params: dict[str, Any] = Field(
        default_factory=dict, description="Default parameters"
    )


class AugmentationTypesResponse(BaseModel):
    """Response for listing augmentation types."""

    augmentation_types: list[AugmentationTypeInfo] = Field(
        ..., description="Available augmentation types"
    )


class PresetInfo(BaseModel):
    """Information about a preset."""

    name: str = Field(..., description="Preset name")
    description: str = Field(..., description="Preset description")


class PresetsResponse(BaseModel):
    """Response for listing presets."""

    presets: list[PresetInfo] = Field(..., description="Available presets")


class AugmentationPreviewRequest(BaseModel):
    """Request to preview augmentation on an image."""

    augmentation_type: str = Field(..., description="Type of augmentation to preview")
    params: dict[str, Any] = Field(
        default_factory=dict, description="Override parameters"
    )


class AugmentationPreviewResponse(BaseModel):
    """Response with preview image data."""

    preview_url: str = Field(..., description="URL to preview image")
    original_url: str = Field(..., description="URL to original image")
    applied_params: dict[str, Any] = Field(..., description="Applied parameters")


class AugmentationBatchRequest(BaseModel):
    """Request to augment a dataset offline."""

    dataset_id: str = Field(..., description="Source dataset UUID")
    config: AugmentationConfigSchema = Field(..., description="Augmentation config")
    output_name: str = Field(
        ..., min_length=1, max_length=255, description="Output dataset name"
    )
    multiplier: int = Field(
        default=2, ge=1, le=10, description="Augmented copies per image"
    )


class AugmentationBatchResponse(BaseModel):
    """Response for batch augmentation."""

    task_id: str = Field(..., description="Background task UUID")
    status: str = Field(..., description="Task status")
    message: str = Field(..., description="Status message")
    estimated_images: int = Field(..., description="Estimated total images")


class AugmentedDatasetItem(BaseModel):
    """Single augmented dataset in list."""

    dataset_id: str = Field(..., description="Dataset UUID")
    source_dataset_id: str = Field(..., description="Source dataset UUID")
    name: str = Field(..., description="Dataset name")
    status: str = Field(..., description="Dataset status")
    multiplier: int = Field(..., description="Augmentation multiplier")
    total_original_images: int = Field(..., description="Original image count")
    total_augmented_images: int = Field(..., description="Augmented image count")
    created_at: datetime = Field(..., description="Creation timestamp")


class AugmentedDatasetListResponse(BaseModel):
    """Response for listing augmented datasets."""

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


class AugmentedDatasetDetailResponse(BaseModel):
    """Detailed augmented dataset response."""

    dataset_id: str = Field(..., description="Dataset UUID")
    source_dataset_id: str = Field(..., description="Source dataset UUID")
    name: str = Field(..., description="Dataset name")
    status: str = Field(..., description="Dataset status")
    config: AugmentationConfigSchema | None = Field(
        None, description="Augmentation config used"
    )
    multiplier: int = Field(..., description="Augmentation multiplier")
    total_original_images: int = Field(..., description="Original image count")
    total_augmented_images: int = Field(..., description="Augmented image count")
    dataset_path: str | None = Field(None, description="Dataset path on disk")
    error_message: str | None = Field(None, description="Error message if failed")
    created_at: datetime = Field(..., description="Creation timestamp")
    completed_at: datetime | None = Field(None, description="Completion timestamp")
