"""ASV benchmarks for LSP registry lookup performance.

Measures the performance of:
- LspServerConfig creation
- Registry register and get operations
- Registry list with filters (namespace, language)
- Registry remove
- Tool adapter spec generation
"""

from __future__ import annotations

import importlib
import sys
from pathlib import Path

# Ensure the local *source* tree is importable
_SRC = str(Path(__file__).resolve().parents[1] / "src")
if _SRC not in sys.path:
    sys.path.insert(0, _SRC)

# Force-reload the top-level package
import cleveragents  # noqa: E402

importlib.reload(cleveragents)

from cleveragents.lsp.models import (  # noqa: E402
    LspCapability,
    LspServerConfig,
)
from cleveragents.lsp.registry import LspRegistry  # noqa: E402
from cleveragents.lsp.tool_adapter import LspToolAdapter  # noqa: E402


def _make_config(
    name: str,
    command: str = "stub-cmd",
    languages: list[str] | None = None,
    capabilities: list[LspCapability] | None = None,
) -> LspServerConfig:
    return LspServerConfig(
        name=name,
        command=command,
        languages=languages or [],
        capabilities=capabilities or [],
    )


class TimeLspConfigCreation:
    """Benchmark LspServerConfig creation."""

    def time_create_minimal(self) -> None:
        LspServerConfig(name="local/test", command="test-cmd")

    def time_create_with_languages(self) -> None:
        LspServerConfig(
            name="local/test",
            command="test-cmd",
            languages=["python", "javascript", "typescript"],
        )

    def time_create_with_capabilities(self) -> None:
        LspServerConfig(
            name="local/test",
            command="test-cmd",
            capabilities=list(LspCapability),
        )


class TimeLspRegistryOperations:
    """Benchmark registry register, get, and remove."""

    def setup(self) -> None:
        self.registry = LspRegistry()
        for i in range(100):
            ns = "local" if i % 2 == 0 else "remote"
            lang = "python" if i % 3 == 0 else "typescript"
            self.registry.register(
                _make_config(
                    f"{ns}/server-{i}",
                    languages=[lang],
                    capabilities=[LspCapability.DIAGNOSTICS],
                )
            )

    def time_get_existing(self) -> None:
        self.registry.get("local/server-0")

    def time_get_missing(self) -> None:
        self.registry.get("local/nonexistent")

    def time_list_all(self) -> None:
        self.registry.list_servers()

    def time_list_with_namespace_filter(self) -> None:
        self.registry.list_servers(namespace="local")

    def time_list_with_language_filter(self) -> None:
        self.registry.list_servers(language="python")

    def time_contains_check(self) -> None:
        _ = "local/server-0" in self.registry


class TimeLspToolAdapter:
    """Benchmark tool adapter spec generation."""

    def setup(self) -> None:
        self.config_few = _make_config(
            "local/test",
            capabilities=[LspCapability.DIAGNOSTICS, LspCapability.COMPLETIONS],
        )
        self.config_all = _make_config(
            "local/test-all",
            capabilities=list(LspCapability),
        )
        self.adapter = LspToolAdapter()

    def time_generate_few_specs(self) -> None:
        self.adapter.generate_tool_specs(self.config_few)

    def time_generate_all_specs(self) -> None:
        self.adapter.generate_tool_specs(self.config_all)
