"""Container infrastructure resource type definitions for the Resource Registry.

Contains the 7 built-in container infrastructure type entries
(container-runtime, container-image, container-mount, container-exec-env,
container-port, container-volume, container-network) that are appended
to ``BUILTIN_TYPES`` in ``_resource_registry_data``.

Defined per ADR-039: Container Resource Types and the specification's
built-in handler table (~line 25096).

.. note::

    This is an **internal** module (``_``-prefixed).  External code
    should import ``BUILTIN_TYPES`` from ``_resource_registry_data``
    instead.
"""

from __future__ import annotations

from typing import Any

__all__ = ["CONTAINER_INFRA_TYPES"]

# Handler references per spec line 25096-25114.  These handler classes
# do not exist yet — the references are forward declarations that will
# be resolved when the container runtime integration is implemented.
_RUNTIME_HANDLER = "cleveragents.resource.handlers.container:ContainerRuntimeHandler"
_IMAGE_HANDLER = "cleveragents.resource.handlers.container:ContainerImageHandler"
_CHILD_HANDLER = "cleveragents.resource.handlers.container:ContainerChildHandler"
_VOLUME_HANDLER = "cleveragents.resource.handlers.container:ContainerVolumeHandler"
_NETWORK_HANDLER = "cleveragents.resource.handlers.container:ContainerNetworkHandler"

CONTAINER_INFRA_TYPES: list[dict[str, Any]] = [
    # ------------------------------------------------------------------
    # container-runtime: top-level runtime (Docker, Podman, etc.)
    # ------------------------------------------------------------------
    {
        "name": "container-runtime",
        "description": (
            "A container runtime engine (Docker, Podman, containerd, etc.)."
        ),
        "resource_kind": "physical",
        "sandbox_strategy": "none",
        "user_addable": True,
        "built_in": True,
        "cli_args": [
            {
                "name": "engine-type",
                "type": "string",
                "required": True,
                "description": (
                    "Container engine type "
                    "(docker, podman, containerd, lxc, lxd, cri-o, other)."
                ),
            },
            {
                "name": "socket-path",
                "type": "path",
                "required": False,
                "description": "Path to the container engine socket.",
                "default": None,
            },
            {
                "name": "endpoint",
                "type": "string",
                "required": False,
                "description": "Remote endpoint URL for the engine API.",
                "default": None,
            },
        ],
        "parent_types": [],
        "child_types": [
            "container-image",
            "container-instance",
            "container-volume",
            "container-network",
        ],
        "auto_discovery": {
            "enabled": True,
            "scan_depth": 1,
            "rules": [
                {"type": "container-image", "pattern": "images/*"},
                {"type": "container-instance", "pattern": "containers/*"},
                {"type": "container-volume", "pattern": "volumes/*"},
                {"type": "container-network", "pattern": "networks/*"},
                {"type": "container-image", "pattern": "docker-compose.yml"},
                {"type": "container-image", "pattern": "docker-compose.yaml"},
                {"type": "container-image", "pattern": "compose.yml"},
                {"type": "container-image", "pattern": "compose.yaml"},
            ],
        },
        "handler": _RUNTIME_HANDLER,
        "capabilities": {
            "read": True,
            "write": False,
            "sandbox": False,
            "checkpoint": False,
        },
    },
    # ------------------------------------------------------------------
    # container-image: immutable container image (tag + digest)
    # ------------------------------------------------------------------
    {
        "name": "container-image",
        "description": ("A container image identified by tag and/or digest."),
        "resource_kind": "physical",
        "sandbox_strategy": "none",
        "user_addable": True,
        "built_in": True,
        "cli_args": [
            {
                "name": "image-ref",
                "type": "string",
                "required": True,
                "description": (
                    "Image reference (e.g. ubuntu:22.04, ghcr.io/org/app:latest)."
                ),
            },
            {
                "name": "digest",
                "type": "string",
                "required": False,
                "description": "Image digest (sha256:...).",
                "default": None,
            },
        ],
        "parent_types": ["container-runtime"],
        "child_types": ["container-instance"],
        "auto_discovery": {
            "enabled": True,
            "scan_depth": 1,
            "rules": [
                {"type": "container-image", "pattern": "Dockerfile"},
                {"type": "container-image", "pattern": "Dockerfile.*"},
                {"type": "container-image", "pattern": "*.dockerfile"},
            ],
        },
        "handler": _IMAGE_HANDLER,
        "capabilities": {
            "read": True,
            "write": False,
            "sandbox": False,
            "checkpoint": False,
        },
    },
    # ------------------------------------------------------------------
    # container-mount: bind mount or volume mount inside a container
    # ------------------------------------------------------------------
    {
        "name": "container-mount",
        "description": ("A bind mount or volume mount inside a container instance."),
        "resource_kind": "physical",
        "sandbox_strategy": "snapshot",
        "user_addable": False,
        "built_in": True,
        "cli_args": [],
        "parent_types": ["container-instance"],
        "child_types": ["fs-directory", "fs-file"],
        "handler": _CHILD_HANDLER,
        "capabilities": {
            "read": True,
            "write": True,
            "sandbox": True,
            "checkpoint": True,
        },
    },
    # ------------------------------------------------------------------
    # container-exec-env: execution environment inside a container
    # ------------------------------------------------------------------
    {
        "name": "container-exec-env",
        "description": (
            "Execution environment variables and configuration "
            "inside a container instance."
        ),
        "resource_kind": "physical",
        "sandbox_strategy": "snapshot",
        "user_addable": False,
        "built_in": True,
        "cli_args": [],
        "parent_types": ["container-instance"],
        "child_types": [],
        "handler": _CHILD_HANDLER,
        "capabilities": {
            "read": True,
            "write": False,
            "sandbox": True,
            "checkpoint": True,
        },
    },
    # ------------------------------------------------------------------
    # container-port: port mapping on a container
    # ------------------------------------------------------------------
    {
        "name": "container-port",
        "description": (
            "A port mapping between host and container (e.g. 8080:80/tcp)."
        ),
        "resource_kind": "physical",
        "sandbox_strategy": "snapshot",
        "user_addable": False,
        "built_in": True,
        "cli_args": [],
        "parent_types": ["container-instance"],
        "child_types": [],
        "handler": _CHILD_HANDLER,
        "capabilities": {
            "read": True,
            "write": False,
            "sandbox": True,
            "checkpoint": True,
        },
    },
    # ------------------------------------------------------------------
    # container-volume: named Docker/Podman volume
    # ------------------------------------------------------------------
    {
        "name": "container-volume",
        "description": ("A named container volume managed by the runtime engine."),
        "resource_kind": "physical",
        "sandbox_strategy": "snapshot",
        "user_addable": True,
        "built_in": True,
        "cli_args": [
            {
                "name": "volume-name",
                "type": "string",
                "required": True,
                "description": "Name of the volume.",
            },
            {
                "name": "driver",
                "type": "string",
                "required": False,
                "description": "Volume driver (e.g. local, nfs).",
                "default": None,
            },
        ],
        "parent_types": ["container-runtime"],
        "child_types": ["fs-directory"],
        "handler": _VOLUME_HANDLER,
        "capabilities": {
            "read": True,
            "write": True,
            "sandbox": True,
            "checkpoint": True,
        },
    },
    # ------------------------------------------------------------------
    # container-network: container network configuration
    # ------------------------------------------------------------------
    {
        "name": "container-network",
        "description": (
            "A container network (bridge, host, overlay, macvlan, or none)."
        ),
        "resource_kind": "physical",
        "sandbox_strategy": "none",
        "user_addable": False,
        "built_in": True,
        "cli_args": [],
        "parent_types": ["container-runtime"],
        "child_types": [],
        "handler": _NETWORK_HANDLER,
        "capabilities": {
            "read": True,
            "write": False,
            "sandbox": False,
            "checkpoint": False,
        },
    },
]
