"""Impact Assessment-related schemas."""

import datetime
from typing import Any
from uuid import UUID
from ..model import Field
from .common import BaseResponse, BaseSchema


class ImpactAssessmentResponse(BaseResponse):
    """Response schema for impact assessment data."""

    id: int = Field(..., description="Unique identifier for the assessment")
    finding_id: UUID = Field(..., description="ID of the associated finding")
    created_at: datetime.datetime = Field(
        ...,
        description="Creation timestamp",
        validation_alias="timestamp",
    )


# Dependency Graph schemas
class DependencyNodeResponse(BaseSchema):
    """Response schema for a dependency graph node."""

    item_type: str = Field(..., description="Item type UUID")
    version: str = Field(..., description="Item type version")
    item_id: str = Field(..., description="Item UUID")


class DependencyEdgeResponse(BaseSchema):
    """Response schema for a dependency graph edge."""

    source_item_id: str = Field(..., description="Source item UUID")
    target_item_id: str = Field(..., description="Target item UUID")


class DependencyGraphResponse(BaseSchema):
    """Response schema for a dependency graph."""

    nodes: list[DependencyNodeResponse] = Field(..., description="List of graph nodes")
    edges: list[DependencyEdgeResponse] = Field(..., description="List of graph edges")


# Composite response schemas


class DependencySummaryEntry(BaseSchema):
    """Schema for summarizing dependencies by item type."""

    item_type: str = Field(..., description="Item type UUID")
    version: str = Field(..., description="Item type version")
    item_count: int = Field(..., description="Number of items impacted")


class ImpactAssessmentWithDependenciesResponse(BaseSchema):
    """Response schema for impact assessment with dependencies."""

    assessment: ImpactAssessmentResponse = Field(
        ..., description="Impact assessment data"
    )
    dependencies_summary: list[DependencySummaryEntry] | None = Field(
        default=None, description="List of dependencies"
    )
    dependencies_graph: DependencyGraphResponse | None = Field(
        None,
        description="Dependency graph showing relationships between impacted items",
    )


class FindingDependencyGraphRequest(BaseSchema):
    """Request schema for filtering finding dependency graphs."""

    finding_ids: list[UUID] | None = Field(
        None, description="Optional list of finding IDs to filter by"
    )


# Command Graph schemas
class CommandNodeResponse(BaseSchema):
    """Response schema for a command graph node."""

    command_id: str = Field(..., description="Command UUID")
    item_type_id: str = Field(..., description="Item type UUID")
    version: str = Field(..., description="Item type version")
    item_id: str = Field(..., description="Item UUID")
    data: dict[str, Any] | list[dict[str, Any]] = Field(
        default_factory=dict, description="Additional node data"
    )
    status: str | None = Field(..., description="Node status")


class CommandEdgeResponse(BaseSchema):
    """Response schema for a command graph edge."""

    source_command_id: str = Field(..., description="Source command UUID")
    source_item_id: str = Field(..., description="Source item UUID")
    target_command_id: str = Field(..., description="Target command UUID")
    target_item_id: str = Field(..., description="Target item UUID")
    status: str | None = Field(..., description="Edge status")


class CommandGraphResponse(BaseSchema):
    """Response schema for a command graph."""

    nodes: list[CommandNodeResponse] = Field(..., description="List of graph nodes")
    edges: list[CommandEdgeResponse] = Field(..., description="List of graph edges")
