"""
Agent manifest runtime models.

Defines runtime environment configuration, inspired by Docker Compose patterns.
"""

from pydantic import BaseModel, Field

from typing import Literal, Optional


class ResourceLimits(BaseModel):
    """Resource limits configuration."""

    memory: Optional[str] = Field(None, description="Memory limit (e.g., 2Gi, 512Mi)")
    cpu: Optional[str] = Field(None, description="CPU limit (e.g., 2.0, 0.5)")


class ResourceConfig(BaseModel):
    """Resource configuration with limits and reservations."""

    limits: Optional[ResourceLimits] = Field(None, description="Maximum resource limits")
    reservations: Optional[ResourceLimits] = Field(None, description="Minimum reserved resources")


class GPUConfig(BaseModel):
    """GPU configuration."""

    count: int = Field(1, ge=0, description="Number of GPUs")
    driver: Optional[str] = Field(None, description="GPU driver (e.g., nvidia)")
    capabilities: list[str] = Field(
        default_factory=list,
        description="Required GPU capabilities (e.g., compute, utility)",
    )


class HealthCheck(BaseModel):
    """
    Health check configuration (Docker-inspired).

    Used to verify agent readiness.
    """

    test: list[str] = Field(
        ...,
        description="Health check command (e.g., ['CMD', 'python', '-c', 'import agent'])",
    )
    interval: str = Field("30s", description="Check interval")
    timeout: str = Field("10s", description="Check timeout")
    retries: int = Field(3, ge=1, description="Number of retries before unhealthy")
    start_period: Optional[str] = Field(None, description="Initial grace period")


class Volume(BaseModel):
    """
    Volume mount configuration (Docker-inspired).

    Supports bind mounts and named volumes.
    """

    type: Literal["bind", "volume"] = Field(..., description="Volume type")
    source: str = Field(..., description="Source path or volume name")
    target: str = Field(..., description="Target mount path in container")
    read_only: bool = Field(False, description="Mount as read-only")


class EnvFile(BaseModel):
    """Environment file configuration."""

    path: str = Field(..., description="Path to environment file")
    required: bool = Field(True, description="Whether the file is required")


class RuntimeConfig(BaseModel):
    """
    Runtime configuration block (Docker Compose-inspired).

    Defines the execution environment for an agent.
    """

    # Language & Version
    language: str = Field("python", description="Runtime language")
    version: Optional[str] = Field(None, description="Language version constraint (e.g., >=3.10)")

    # Dependencies
    dependencies: list[str] = Field(
        default_factory=list,
        description="Package dependencies (pip requirements format)",
    )

    # Environment variables
    env: dict[str, str] = Field(
        default_factory=dict,
        description="Environment variables",
    )

    # Environment files
    env_file: list[EnvFile] = Field(
        default_factory=list,
        description="Environment files to load",
    )

    # Resource configuration
    resources: Optional[ResourceConfig] = Field(None, description="Resource limits and reservations")

    # GPU configuration
    gpus: Optional[GPUConfig] = Field(None, description="GPU configuration")

    # Health check
    healthcheck: Optional[HealthCheck] = Field(None, description="Health check configuration")

    # Working directory
    working_dir: Optional[str] = Field(None, description="Working directory path")

    # Volumes
    volumes: list[Volume] = Field(default_factory=list, description="Volume mounts")
