"""ASV benchmarks for UKO Analyzer Plugin Framework.

Measures the performance of:
- UKOTriple construction overhead
- AnalyzerRegistry registration and lookup
- PythonAnalyzer.analyze() on various source sizes
- MarkdownAnalyzer.analyze() on various document sizes
"""

from __future__ import annotations

import importlib
import sys
from pathlib import Path

# Ensure the local *source* tree is importable even when ASV has an
# older build of the package installed.
_SRC = str(Path(__file__).resolve().parents[1] / "src")
if _SRC not in sys.path:
    sys.path.insert(0, _SRC)

# Force-reload so ASV picks up the source tree version.
import cleveragents  # noqa: E402

importlib.reload(cleveragents)

from cleveragents.domain.models.acms.analyzers import (  # noqa: E402
    AnalyzerRegistry,
    UKOTriple,
)
from cleveragents.domain.models.acms.markdown_analyzer import (  # noqa: E402
    MarkdownAnalyzer,
)
from cleveragents.domain.models.acms.python_analyzer import (  # noqa: E402
    PythonAnalyzer,
)

# ---------------------------------------------------------------------------
# Sample content
# ---------------------------------------------------------------------------

_SMALL_PYTHON = 'import os\n\ndef hello():\n    """Say hello."""\n    pass\n'

_MEDIUM_PYTHON = (
    '"""Module docstring."""\n\nimport os\nfrom pathlib import Path\n\n'
    + "\n\n".join(
        f'class Cls{i}:\n    """Class {i}."""\n'
        f"    def method_{i}(self):\n"
        f'        """Method {i}."""\n        pass\n'
        for i in range(20)
    )
)

_SMALL_MARKDOWN = "# Hello\n\nWorld\n"

_MEDIUM_MARKDOWN = "# Document\n\n" + "\n".join(
    f"## Section {i}\n\nParagraph text for section {i}.\n\n"
    f"```python\nprint({i})\n```\n\n"
    f"[Link {i}](https://example.com/{i})\n"
    for i in range(20)
)


# ---------------------------------------------------------------------------
# UKOTriple benchmarks
# ---------------------------------------------------------------------------


class TripleConstructionSuite:
    """Benchmark UKOTriple object creation overhead."""

    timeout = 60

    def time_create_object_property_triple(self) -> None:
        UKOTriple(
            subject_uri="uko://code/module/foo",
            predicate="rdf:type",
            object_uri="uko-code:Module",
        )

    def time_create_data_property_triple(self) -> None:
        UKOTriple(
            subject_uri="uko://code/class/Bar",
            predicate="rdfs:label",
            object_value="Bar",
            confidence=0.9,
        )


# ---------------------------------------------------------------------------
# AnalyzerRegistry benchmarks
# ---------------------------------------------------------------------------


class RegistrySuite:
    """Benchmark AnalyzerRegistry operations."""

    timeout = 60

    def setup(self) -> None:
        self.registry = AnalyzerRegistry()
        self.py = PythonAnalyzer()
        self.md = MarkdownAnalyzer()
        self.registry.register(self.py)
        self.registry.register(self.md)

    def time_lookup_registered_extension(self) -> None:
        self.registry.get_for_extension(".py")

    def time_lookup_unregistered_extension(self) -> None:
        self.registry.get_for_extension(".rs")

    def time_list_extensions(self) -> None:
        self.registry.list_extensions()

    def time_list_analyzers(self) -> None:
        self.registry.list_analyzers()


# ---------------------------------------------------------------------------
# PythonAnalyzer benchmarks
# ---------------------------------------------------------------------------


class PythonAnalyzerSuite:
    """Benchmark PythonAnalyzer.analyze() performance."""

    timeout = 60

    def setup(self) -> None:
        self.analyzer = PythonAnalyzer()

    def time_analyze_small_python(self) -> None:
        self.analyzer.analyze(_SMALL_PYTHON, "src/small.py")

    def time_analyze_medium_python(self) -> None:
        self.analyzer.analyze(_MEDIUM_PYTHON, "src/medium.py")


# ---------------------------------------------------------------------------
# MarkdownAnalyzer benchmarks
# ---------------------------------------------------------------------------


class MarkdownAnalyzerSuite:
    """Benchmark MarkdownAnalyzer.analyze() performance."""

    timeout = 60

    def setup(self) -> None:
        self.analyzer = MarkdownAnalyzer()

    def time_analyze_small_markdown(self) -> None:
        self.analyzer.analyze(_SMALL_MARKDOWN, "docs/small.md")

    def time_analyze_medium_markdown(self) -> None:
        self.analyzer.analyze(_MEDIUM_MARKDOWN, "docs/medium.md")
