"""Alignment-related schemas."""

import datetime
from uuid import UUID
from pydantic import Field
from .common import BaseResponse, BaseSchema


class AlignmentBase(BaseSchema):
    """Base schema for alignment data."""

    name: str = Field(
        ..., description="Name of the alignment", min_length=1, max_length=255
    )
    description: str | None = Field(None, description="Description of the alignment")
    from_version: str | None = Field(
        None, description="Identifier of the alignment start point"
    )
    to_version: str | None = Field(
        None, description="Identifier of the alignment end point"
    )


class AlignmentCreateRequest(AlignmentBase):
    """Request schema for creating an alignment."""

    alignment_type_id: int = Field(..., description="ID of the alignment type")


class AlignmentUpdateRequest(BaseSchema):
    """Request schema for updating an alignment."""

    name: str | None = Field(
        None, description="New name of the alignment", min_length=1, max_length=255
    )
    description: str | None = Field(
        None, description="New description of the alignment"
    )
    from_version: str | None = Field(
        None, description="New identifier of the alignment start point"
    )
    to_version: str | None = Field(
        None, description="New identifier of the alignment end point"
    )


class AlignmentMetrics(BaseSchema):
    """Metrics for alignment data."""

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


class AlignmentResponse(AlignmentBase, BaseResponse):
    """Response schema for alignment data."""

    id: UUID = Field(..., description="Unique identifier for the alignment")
    alignment_type_id: int = Field(..., description="ID of the alignment type")
    status: str = Field(..., description="Status of the alignment")
    created_at: datetime.datetime = Field(..., description="Creation timestamp")
    created_by: UUID = Field(..., description="User UID who created the alignment")
    updated_at: datetime.datetime | None = Field(
        None, description="Last update timestamp"
    )
    metrics: AlignmentMetrics | None = Field(
        None, description="Metrics for the alignment"
    )


class AlignmentTypeBase(BaseSchema):
    """Base schema for alignment type data."""

    name: str = Field(
        ..., description="Name of the alignment type", min_length=1, max_length=255
    )


class AlignmentTypeCreateRequest(AlignmentTypeBase):
    """Request schema for creating an alignment type."""

    pass


class AlignmentTypeResponse(AlignmentTypeBase, BaseResponse):
    """Response schema for alignment type data."""

    id: int = Field(..., description="Unique identifier for the alignment type")


class AlignmentExecuteRequest(BaseSchema):
    """Request schema to execute alignment type operations."""

    map_findings: bool = Field(
        default=True, description="Whether to run finding mapping"
    )
    store_finding_dependencies: bool = Field(
        default=True,
        description="Whether to store finding dependencies for impact assessment",
    )
    dryrun_commands: bool = Field(
        default=False,
        description="Whether to output the commands that would be executed without actually executing them",
    )


class AlignmentExecuteResponse(BaseResponse):
    """Response schema for alignment type execution."""

    alignment_id: UUID = Field(..., description="Alignment identifier")
    alignment_type: str = Field(..., description="Alignment type name")
    operations: list[str] = Field(
        default_factory=list, description="Operations executed"
    )

    pass
