"""ASV benchmarks for UKO ontology loader performance.

Suites:
- UKOLoadSuite: time_load_ttl, time_parse_prefixes, time_parse_nodes
- UKOValidationSuite: time_validate_ontology, time_resolve_inheritance
- UKOLayerSuite: time_get_layer_0, time_get_layer_3
"""

from __future__ import annotations

import importlib
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)

import cleveragents  # noqa: E402

importlib.reload(cleveragents)

from cleveragents.application.services.uko_loader import UKOLoader  # noqa: E402

_TTL_PATH = Path(__file__).resolve().parents[1] / "docs" / "ontology" / "uko.ttl"
_TTL_TEXT = _TTL_PATH.read_text(encoding="utf-8")


class UKOLoadSuite:
    """Benchmark TTL loading and parsing throughput."""

    def setup(self) -> None:
        self.loader = UKOLoader()

    def time_load_ttl(self) -> None:
        """Benchmark loading uko.ttl from disk."""
        self.loader.load(_TTL_PATH)

    def time_parse_prefixes(self) -> None:
        """Benchmark prefix-line parsing."""
        self.loader._parse_prefixes(_TTL_TEXT)

    def time_parse_nodes(self) -> None:
        """Benchmark full string parsing."""
        self.loader.load_from_string(_TTL_TEXT)


class UKOValidationSuite:
    """Benchmark validation and inheritance resolution."""

    def setup(self) -> None:
        self.loader = UKOLoader()
        self.ontology = self.loader.load(_TTL_PATH)

    def time_validate_ontology(self) -> None:
        """Benchmark ontology validation."""
        self.loader.validate(self.ontology)

    def time_resolve_inheritance(self) -> None:
        """Benchmark uko-oo:Class inheritance chain resolution."""
        self.loader.resolve_inheritance(
            self.ontology,
            "https://cleveragents.ai/ontology/uko/oo#Class",
        )


class UKOLayerSuite:
    """Benchmark layer-filtered node retrieval."""

    def setup(self) -> None:
        self.loader = UKOLoader()
        self.ontology = self.loader.load(_TTL_PATH)

    def time_get_layer_0(self) -> None:
        """Benchmark Layer 0 node retrieval."""
        self.loader.get_layer_nodes(self.ontology, 0)

    def time_get_layer_3(self) -> None:
        """Benchmark Layer 3 node retrieval."""
        self.loader.get_layer_nodes(self.ontology, 3)
