"""
Pydantic models for guardrail API responses.

This module defines data structures for guardrail operations including
retrieving applicable guardrails for projects and agents within the Airia platform.
"""

from typing import List, Optional

from pydantic import BaseModel, Field


class GuardrailFilter(BaseModel):
    """Represents a base filter definition within a guardrail.

    Filters define the specific detection or processing rules that a guardrail
    applies to content. Each filter has a type indicating what kind of content
    it processes (e.g., PII, PCI, moderation).

    Attributes:
        enabled: Whether the filter is currently enabled
        guardrail_id: The unique identifier of the parent guardrail
        tenant_id: The tenant identifier this filter belongs to
        filter_type: The type of filter (e.g., Pci, Pii, Phi, Moderation, PromptInjection)
    """

    enabled: bool
    guardrail_id: str = Field(alias="guardrailId")
    tenant_id: str = Field(alias="tenantId")
    filter_type: str = Field(alias="filterType")


class GuardrailAssignment(BaseModel):
    """Represents a guardrail assignment definition.

    Assignments define which entities (tenants, projects, agents, etc.) a
    guardrail is applied to. This allows for granular control over guardrail
    application across the organization hierarchy.

    Attributes:
        entity_type: The type of entity (e.g., Tenant, Project, Agent, Model, Datasource)
        entity_id: The unique identifier of the assigned entity
        project_id: Optional project identifier if the entity belongs to a project
    """

    entity_type: str = Field(alias="entityType")
    entity_id: str = Field(alias="entityId")
    project_id: Optional[str] = Field(None, alias="projectId")


class GuardrailTarget(BaseModel):
    """Guardrail target definition.

    Targets specify how guardrails are applied to different entities,
    including whether they apply to input, output, or both, and whether
    they represent inclusions or exclusions.

    Attributes:
        entity_type: The type of entity (e.g., Tenant, Project, Agent, Model)
        target_type: Where the guardrail applies (None, Input, Output, Both)
        is_exception: Whether assignments represent exclusions rather than inclusions.
            When true, the guardrail applies to all entities EXCEPT those in the
            assignment list. When false (default), the guardrail applies ONLY to
            entities in the assignment list.
    """

    entity_type: str = Field(alias="entityType")
    target_type: str = Field(alias="targetType")
    is_exception: bool = Field(alias="isException")


class Guardrail(BaseModel):
    """Represents a guardrail definition.

    A guardrail is a security or content moderation mechanism that can be
    applied to pipelines, agents, and other entities. Guardrails contain
    filters for detecting specific content types and assignments that
    determine where they are applied.

    Attributes:
        id: The unique identifier of the guardrail
        name: The friendly display name of the guardrail
        description: A description of what the guardrail does
        enabled: Whether the guardrail is currently enabled
        apply_datasource: Whether the guardrail should be applied to datasources
        apply_to_tools: Whether the guardrail should be applied to tool responses
        filters: List of filters that define what the guardrail detects
        guardrail_assignments: List of entity assignments for the guardrail
        guardrail_targets: List of target configurations for the guardrail
    """

    id: str
    name: str
    description: str
    enabled: bool
    apply_datasource: bool = Field(alias="applyDatasource")
    apply_to_tools: bool = Field(alias="applyToTools")
    filters: List[GuardrailFilter]
    guardrail_assignments: List[GuardrailAssignment] = Field(
        alias="guardrailAssignments"
    )
    guardrail_targets: List[GuardrailTarget] = Field(alias="guardrailTargets")
