"""
Pydantic schemas for FirewallRule model.

Request and response schemas for network security rules.
"""

from datetime import datetime
from typing import Optional
from uuid import UUID

from pydantic import BaseModel, Field


class FirewallRuleBase(BaseModel):
    """Base schema with shared FirewallRule fields."""

    infrastructure_id: Optional[UUID] = Field(None, description="Reference to the infrastructure this rule applies to")
    rule_name: Optional[str] = Field(None, description="Name of the firewall rule")
    source_cidr: Optional[str] = Field(None, description="Source CIDR notation")
    destination_cidr: Optional[str] = Field(None, description="Destination CIDR notation")
    port: Optional[int] = Field(None, description="Port number")
    protocol: Optional[str] = Field(None, description="Protocol: tcp, udp, icmp")
    action: Optional[str] = Field(None, description="Action: allow, deny, drop")
    rule_order: Optional[int] = Field(None, description="Order of the rule in the firewall")
    notes: Optional[str] = Field(None, description="Additional notes")
    created_by: Optional[str] = Field(None, description="Who created the rule")


class FirewallRuleCreate(FirewallRuleBase):
    """Schema for creating a new FirewallRule."""
    pass


class FirewallRuleUpdate(BaseModel):
    """Schema for updating an existing FirewallRule. All fields are optional."""

    infrastructure_id: Optional[UUID] = Field(None, description="Reference to the infrastructure this rule applies to")
    rule_name: Optional[str] = Field(None, description="Name of the firewall rule")
    source_cidr: Optional[str] = Field(None, description="Source CIDR notation")
    destination_cidr: Optional[str] = Field(None, description="Destination CIDR notation")
    port: Optional[int] = Field(None, description="Port number")
    protocol: Optional[str] = Field(None, description="Protocol: tcp, udp, icmp")
    action: Optional[str] = Field(None, description="Action: allow, deny, drop")
    rule_order: Optional[int] = Field(None, description="Order of the rule in the firewall")
    notes: Optional[str] = Field(None, description="Additional notes")
    created_by: Optional[str] = Field(None, description="Who created the rule")


class FirewallRuleResponse(FirewallRuleBase):
    """Schema for FirewallRule responses with ID and timestamps."""

    id: UUID = Field(..., description="Unique identifier for the firewall rule")
    created_at: datetime = Field(..., description="Timestamp when the firewall rule was created")
    updated_at: datetime = Field(..., description="Timestamp when the firewall rule was last updated")

    model_config = {"from_attributes": True}
