"""ASV benchmarks for DetailLevelMap operations.

Measures the performance of DetailLevelMap operations including:
- Named level resolution (string lookups)
- Integer depth resolution (clamping)
- Effective level map computation (inheritance merge)
- DetailLevelMapBuilder insertions and build
- VocabularyRegistry lookups

Based on specification.md DetailDepth and DetailLevelMap sections.
"""

from __future__ import annotations

import sys
from pathlib import Path

_SRC = str(Path(__file__).resolve().parents[1] / "src")
if _SRC not in sys.path:
    sys.path.insert(0, _SRC)

from cleveragents.acms.uko.detail_level_maps import (  # noqa: E402
    CODE_DETAIL_LEVEL_MAP,
    OO_DETAIL_LEVEL_MAP,
    DetailLevelMapBuilder,
    build_effective_map,
)
from cleveragents.acms.uko.vocabularies import (  # noqa: E402
    get_func_vocabulary,
    get_oo_vocabulary,
    get_proc_vocabulary,
)
from cleveragents.acms.uko.vocabulary_registry import VocabularyRegistry  # noqa: E402


class DetailLevelMapResolutionSuite:
    """Benchmark DetailLevelMap.resolve() for named and integer depths."""

    timeout = 60

    def setup(self) -> None:
        """Prepare maps for benchmarking."""
        self.code_map = CODE_DETAIL_LEVEL_MAP
        self.oo_map = OO_DETAIL_LEVEL_MAP

    def time_resolve_named_level_code(self) -> None:
        """Benchmark resolving a named level in the code map."""
        self.code_map.resolve("SIGNATURES")

    def time_resolve_named_level_oo(self) -> None:
        """Benchmark resolving a named level in the OO map."""
        self.oo_map.resolve("CLASS_HIERARCHY")

    def time_resolve_integer_depth(self) -> None:
        """Benchmark resolving an integer depth."""
        self.code_map.resolve(4)

    def time_resolve_integer_clamped(self) -> None:
        """Benchmark resolving an integer that exceeds max_depth."""
        self.code_map.resolve(100)

    def time_resolve_inherited_level(self) -> None:
        """Benchmark resolving a level inherited from parent."""
        self.oo_map.resolve("MODULE_LISTING")

    def time_effective_levels_code(self) -> None:
        """Benchmark computing effective levels for code map."""
        self.code_map.effective_levels()

    def time_effective_levels_oo(self) -> None:
        """Benchmark computing effective levels for OO map (with parent)."""
        self.oo_map.effective_levels()


class DetailLevelMapBuilderSuite:
    """Benchmark DetailLevelMapBuilder operations."""

    timeout = 60

    def setup(self) -> None:
        """Prepare parent map."""
        self.code_map = CODE_DETAIL_LEVEL_MAP

    def time_build_with_single_insertion(self) -> None:
        """Benchmark building a map with one insertion."""
        builder = DetailLevelMapBuilder(self.code_map, "bench:")
        builder.insert_after("MEMBER_LISTING", "BENCH_LEVEL")
        builder.build()

    def time_build_with_two_insertions(self) -> None:
        """Benchmark building a map with two insertions (OO pattern)."""
        builder = DetailLevelMapBuilder(self.code_map, "bench:")
        builder.insert_after("MEMBER_LISTING", "BENCH_A")
        builder.insert_after("SIGNATURES_WITH_DOCS", "BENCH_B")
        builder.build()

    def time_build_effective_map_no_insertions(self) -> None:
        """Benchmark build_effective_map with empty insertions."""
        build_effective_map(self.code_map, [])

    def time_build_effective_map_with_insertions(self) -> None:
        """Benchmark build_effective_map with two insertions."""
        build_effective_map(
            self.code_map,
            [("MEMBER_LISTING", "X"), ("SIGNATURES_WITH_DOCS", "Y")],
        )


class VocabularyRegistrySuite:
    """Benchmark VocabularyRegistry operations."""

    timeout = 60

    def setup(self) -> None:
        """Build and populate the registry."""
        self.registry = VocabularyRegistry(
            vocabularies=(
                get_oo_vocabulary(),
                get_func_vocabulary(),
                get_proc_vocabulary(),
            )
        )

    def time_get_by_prefix(self) -> None:
        """Benchmark looking up vocabulary by prefix."""
        self.registry.get_by_prefix("uko-oo:")

    def time_get_by_iri(self) -> None:
        """Benchmark looking up vocabulary by IRI."""
        self.registry.get_by_iri("https://cleveragents.ai/ontology/uko/oo#")

    def time_list_prefixes(self) -> None:
        """Benchmark listing all prefixes."""
        self.registry.list_prefixes()

    def time_list_all(self) -> None:
        """Benchmark listing all vocabularies."""
        self.registry.list_all()

    def time_contains_check(self) -> None:
        """Benchmark __contains__ check."""
        _ = "uko-oo:" in self.registry
