"""Bucket-related schemas."""

import datetime
from uuid import UUID
from typing import Any
from ..model import Field
from ..enums import BucketStatus
from .common import BaseResponse, BaseSchema, BaseEventCreateRequest, BaseEventResponse


class BucketMetrics(BaseSchema):
    """Metrics for bucket data."""

    findings_count: int = Field(0, description="Number of findings in the bucket")
    command_groups_count: int = Field(
        0, description="Number of command groups in the bucket"
    )
    commands_count: int = Field(0, description="Number of commands in the bucket")


class BucketTaskSnapshot(BaseSchema):
    """Task snapshot metrics for command groups in a bucket."""

    total: int = Field(0, description="Total number of command groups")
    approved: int = Field(0, description="Number of task-approved command groups")
    raised: int = Field(0, description="Number of task-raised command groups")
    pending: int = Field(0, description="Number of task-pending command groups")
    rejected: int = Field(0, description="Number of task-rejected command groups")
    pending_with_rejection: int | None = Field(
        None,
        description="Number of task-pending groups with at least one rejected command",
    )


class BucketCreateRequest(BaseSchema):
    """Request schema for creating a bucket."""

    alignment_id: UUID = Field(..., description="ID of the associated alignment")
    bucket_type: str = Field(
        ..., description="Type of the bucket", min_length=1, max_length=64
    )
    bucket_name: str = Field(
        ..., description="Name of the bucket", min_length=1, max_length=256
    )


class BucketResponse(BaseResponse):
    """Response schema for bucket data."""

    id: UUID = Field(..., description="Unique identifier for the bucket")
    alignment_id: UUID = Field(..., description="ID of the associated alignment")
    bucket_type: str = Field(..., description="Type of the bucket")
    bucket_name: str = Field(..., description="Name of the bucket")
    bucket_ordinal: int = Field(
        ..., description="Ordinal of the bucket within its alignment"
    )
    status: BucketStatus = Field(..., description="Current status of the bucket")
    created_at: datetime.datetime = Field(..., description="Creation timestamp")
    updated_at: datetime.datetime | None = Field(
        None, description="Last update timestamp"
    )
    metrics: BucketMetrics | None = Field(None, description="Metrics for the bucket")
    task_snapshot: BucketTaskSnapshot | None = Field(
        None,
        description="Task snapshot metrics for command groups in this bucket",
    )


class BucketStatusUpdateRequest(BaseSchema):
    """Request schema for updating bucket status."""

    status: BucketStatus = Field(..., description="New status for the bucket")


class BucketEventCreateRequest(BaseEventCreateRequest):
    """Request schema for creating a bucket event."""

    pass


class BucketEventResponse(BaseEventResponse):
    """Response schema for bucket event data."""

    bucket_id: UUID = Field(..., description="ID of the associated bucket")


class BucketDetailResponse(BucketResponse):
    """Comprehensive response schema for bucket data including findings and commands."""

    findings: list[dict[str, Any]] = Field(
        default_factory=list, description="All findings associated with this bucket"
    )
    command_groups: list[dict[str, Any]] = Field(
        default_factory=list,
        description="All command groups with commands for this bucket",
    )
    command_findings_mappings: list[dict[str, Any]] = Field(
        default_factory=list,
        description="All command-findings mappings for this bucket",
    )
    findings_count: int = Field(0, description="Total number of findings")
    command_groups_count: int = Field(0, description="Total number of command groups")
    total_commands_count: int = Field(0, description="Total number of commands")
    command_findings_mappings_count: int = Field(
        0, description="Total number of command-findings mappings"
    )
