"""ASV benchmarks for resource type inheritance resolution engine."""

from __future__ import annotations

from typing import Any

from cleveragents.resource.inheritance import (
    find_subtypes,
    is_subtype_of,
    resolve_fields,
    resolve_inheritance_chain,
)


def _build_linear_registry(depth: int) -> tuple[dict[str, Any], str]:
    """Build a linear inheritance chain of *depth* levels.

    Returns ``(registry, leaf_name)`` where the chain is::

        leaf -> level-(depth-2) -> ... -> level-0 -> root
    """
    registry: dict[str, Any] = {"root": {}}
    prev = "root"
    for i in range(depth - 1):
        name = f"level-{i}"
        registry[name] = {"inherits": prev}
        prev = name
    return registry, prev


def _build_wide_registry(width: int) -> dict[str, Any]:
    """Build a registry with *width* direct children of a single ancestor."""
    registry: dict[str, Any] = {"ancestor": {}}
    for i in range(width):
        registry[f"child-{i}"] = {"inherits": "ancestor"}
    return registry


def _build_field_registry(depth: int) -> tuple[dict[str, Any], str]:
    """Build a registry with mergeable fields at each level."""
    registry: dict[str, Any] = {
        "root": {
            "sandbox_strategy": "copy_on_write",
            "resource_kind": "physical",
            "cli_args": [{"name": "base-arg", "type": "string"}],
            "child_types": ["root-child"],
        },
    }
    prev = "root"
    for i in range(depth - 1):
        name = f"level-{i}"
        registry[name] = {
            "inherits": prev,
            "resource_kind": f"kind-{i}",
            "cli_args": [{"name": f"arg-{i}", "type": "string"}],
            "child_types": [f"ct-{i}"],
        }
        prev = name
    return registry, prev


# ---------------------------------------------------------------------------
# Chain resolution benchmarks
# ---------------------------------------------------------------------------


class InheritanceChainResolutionSuite:
    """Benchmark resolve_inheritance_chain at various depths."""

    def setup(self) -> None:
        self.reg_1, self.leaf_1 = _build_linear_registry(1)
        self.reg_3, self.leaf_3 = _build_linear_registry(3)
        self.reg_5, self.leaf_5 = _build_linear_registry(5)

    def time_resolve_chain_1_level(self) -> None:
        resolve_inheritance_chain(self.leaf_1, self.reg_1)

    def time_resolve_chain_3_level(self) -> None:
        resolve_inheritance_chain(self.leaf_3, self.reg_3)

    def time_resolve_chain_5_level(self) -> None:
        resolve_inheritance_chain(self.leaf_5, self.reg_5)


# ---------------------------------------------------------------------------
# Polymorphic subtype check benchmarks
# ---------------------------------------------------------------------------


class IsSubtypeOfSuite:
    """Benchmark is_subtype_of at various depths."""

    def setup(self) -> None:
        self.reg_1, self.leaf_1 = _build_linear_registry(1)
        self.reg_3, self.leaf_3 = _build_linear_registry(3)
        self.reg_5, self.leaf_5 = _build_linear_registry(5)

    def time_is_subtype_identity(self) -> None:
        is_subtype_of("root", "root", self.reg_1)

    def time_is_subtype_1_level(self) -> None:
        is_subtype_of(self.leaf_1, "root", self.reg_1)

    def time_is_subtype_3_level(self) -> None:
        is_subtype_of(self.leaf_3, "root", self.reg_3)

    def time_is_subtype_5_level(self) -> None:
        is_subtype_of(self.leaf_5, "root", self.reg_5)

    def time_is_subtype_negative(self) -> None:
        is_subtype_of("root", self.leaf_5, self.reg_5)


# ---------------------------------------------------------------------------
# Field resolution benchmarks
# ---------------------------------------------------------------------------


class ResolveFieldsSuite:
    """Benchmark resolve_fields with field merging at various depths."""

    def setup(self) -> None:
        self.reg_1, self.leaf_1 = _build_field_registry(1)
        self.reg_3, self.leaf_3 = _build_field_registry(3)
        self.reg_5, self.leaf_5 = _build_field_registry(5)

    def time_resolve_fields_1_level(self) -> None:
        resolve_fields(self.leaf_1, self.reg_1)

    def time_resolve_fields_3_level(self) -> None:
        resolve_fields(self.leaf_3, self.reg_3)

    def time_resolve_fields_5_level(self) -> None:
        resolve_fields(self.leaf_5, self.reg_5)


# ---------------------------------------------------------------------------
# find_subtypes benchmarks
# ---------------------------------------------------------------------------


class FindSubtypesSuite:
    """Benchmark find_subtypes with varying registry widths."""

    def setup(self) -> None:
        self.reg_10 = _build_wide_registry(10)
        self.reg_50 = _build_wide_registry(50)
        self.reg_200 = _build_wide_registry(200)

    def time_find_subtypes_10(self) -> None:
        find_subtypes("ancestor", self.reg_10)

    def time_find_subtypes_50(self) -> None:
        find_subtypes("ancestor", self.reg_50)

    def time_find_subtypes_200(self) -> None:
        find_subtypes("ancestor", self.reg_200)
