"""ASV benchmarks for UKO Layer 3 technology vocabulary operations.

Measures the performance of:
- build_detail_level_map with insertions (Python: 3 insertions, 15-level map)
- build_detail_level_map without insertions (TS/RS/Java: identity, 12-level map)
- resolve_detail_level lookups (direct hit, parent fallback)
- UKOVocabulary construction (frozen Pydantic model with nested tuples)
- ProvenanceInfo construction

Based on specification.md Layer 3 Technology-Specific Specializations.
"""

from __future__ import annotations

import sys
from datetime import UTC, datetime
from pathlib import Path

try:
    from cleveragents.acms.uko.layer3_java import JAVA_DETAIL_LEVELS
    from cleveragents.acms.uko.layer3_py import PYTHON_DETAIL_LEVELS
    from cleveragents.acms.uko.layer3_rs import RUST_DETAIL_LEVELS
    from cleveragents.acms.uko.layer3_ts import TYPESCRIPT_DETAIL_LEVELS
    from cleveragents.acms.uko.vocabulary import (
        OO_EFFECTIVE_LEVELS,
        ProvenanceInfo,
        UKOClass,
        UKOVocabulary,
        build_detail_level_map,
        resolve_detail_level,
    )
except ModuleNotFoundError:
    src_path = str(Path(__file__).resolve().parents[1] / "src")
    if src_path not in sys.path:
        sys.path.insert(0, src_path)
    from cleveragents.acms.uko.layer3_java import JAVA_DETAIL_LEVELS
    from cleveragents.acms.uko.layer3_py import PYTHON_DETAIL_LEVELS
    from cleveragents.acms.uko.layer3_rs import RUST_DETAIL_LEVELS
    from cleveragents.acms.uko.layer3_ts import TYPESCRIPT_DETAIL_LEVELS
    from cleveragents.acms.uko.vocabulary import (
        OO_EFFECTIVE_LEVELS,
        ProvenanceInfo,
        UKOClass,
        UKOVocabulary,
        build_detail_level_map,
        resolve_detail_level,
    )


class BuildDetailLevelMapSuite:
    """Benchmark build_detail_level_map for Layer 3 maps."""

    timeout = 60

    def setup(self) -> None:
        """Prepare parent levels and insertions."""
        self.oo_levels = OO_EFFECTIVE_LEVELS
        self.py_insertions: tuple[tuple[str, int], ...] = (
            ("DECORATED_SIGNATURES", 6),
            ("TYPE_STUBS", 9),
            ("WITH_TESTS", 12),
        )
        self.empty_insertions: tuple[tuple[str, int], ...] = ()

    def time_build_with_insertions(self) -> None:
        """Build Python 15-level map (3 insertions into 12 parents)."""
        build_detail_level_map(self.oo_levels, self.py_insertions)

    def time_build_identity(self) -> None:
        """Build identity map (no insertions -- TS/RS/Java pattern)."""
        build_detail_level_map(self.oo_levels, self.empty_insertions)

    def track_python_map_size(self) -> int:
        """Track number of levels in the Python effective map."""
        return len(build_detail_level_map(self.oo_levels, self.py_insertions))

    def track_identity_map_size(self) -> int:
        """Track number of levels in the identity map."""
        return len(build_detail_level_map(self.oo_levels, self.empty_insertions))


class ResolveDetailLevelSuite:
    """Benchmark resolve_detail_level lookups."""

    timeout = 60

    def setup(self) -> None:
        """Prepare maps for resolution."""
        self.py_levels = PYTHON_DETAIL_LEVELS
        self.ts_levels = TYPESCRIPT_DETAIL_LEVELS
        self.rs_levels = RUST_DETAIL_LEVELS
        self.java_levels = JAVA_DETAIL_LEVELS
        self.parent_levels = OO_EFFECTIVE_LEVELS
        self.child_only_map: tuple[tuple[str, int], ...] = (("CHILD_ONLY", 0),)

    def time_resolve_first_level(self) -> None:
        """Resolve MODULE_LISTING (first entry -- best case)."""
        resolve_detail_level("MODULE_LISTING", self.py_levels)

    def time_resolve_last_level(self) -> None:
        """Resolve WITH_TESTS (last entry -- worst case)."""
        resolve_detail_level("WITH_TESTS", self.py_levels)

    def time_resolve_inserted_level(self) -> None:
        """Resolve DECORATED_SIGNATURES (inserted level)."""
        resolve_detail_level("DECORATED_SIGNATURES", self.py_levels)

    def time_resolve_with_parent_fallback(self) -> None:
        """Resolve via parent fallback (name not in child map)."""
        resolve_detail_level("MODULE_LISTING", self.child_only_map, self.parent_levels)

    def time_resolve_ts(self) -> None:
        """Resolve SIGNATURES in TypeScript map."""
        resolve_detail_level("SIGNATURES", self.ts_levels)

    def time_resolve_rs(self) -> None:
        """Resolve SIGNATURES in Rust map."""
        resolve_detail_level("SIGNATURES", self.rs_levels)

    def time_resolve_java(self) -> None:
        """Resolve SIGNATURES in Java map."""
        resolve_detail_level("SIGNATURES", self.java_levels)


class VocabularyConstructionSuite:
    """Benchmark UKOVocabulary and ProvenanceInfo construction."""

    timeout = 60

    def setup(self) -> None:
        """Pre-compute a fixed datetime to avoid syscall noise."""
        self.fixed_datetime = datetime.now(tz=UTC)

    def time_provenance_info(self) -> None:
        """Construct a ProvenanceInfo instance."""
        ProvenanceInfo(
            source_resource="res-01",
            source_path="src/main.py",
            valid_from=self.fixed_datetime,
        )

    def time_uko_class(self) -> None:
        """Construct a UKOClass instance."""
        UKOClass(
            uri="https://example.com/Test",
            label="Test",
            subclass_of=("https://example.com/Parent",),
        )

    def time_uko_vocabulary(self) -> None:
        """Construct a minimal UKOVocabulary instance."""
        UKOVocabulary(
            namespace="https://example.com/test#",
            prefix="test:",
            label="Test Vocabulary",
        )
