"""ASV benchmarks for skill-level search and directory tools.

Measures the performance of:
- ListDir with filters
- Glob pattern matching
- Grep regex searching
"""

from __future__ import annotations

import shutil
import sys
import tempfile
from pathlib import Path

try:
    from cleveragents.skills.builtins.search_ops import (
        register_skill_search_tools,
    )
    from cleveragents.tool.registry import ToolRegistry
    from cleveragents.tool.runner import ToolRunner
except ModuleNotFoundError:
    sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
    from cleveragents.skills.builtins.search_ops import (
        register_skill_search_tools,
    )
    from cleveragents.tool.registry import ToolRegistry
    from cleveragents.tool.runner import ToolRunner


class SkillSearchToolSuite:
    """Benchmark skill-level search tool operations."""

    def setup(self) -> None:
        self.sandbox = tempfile.mkdtemp()
        self.registry = ToolRegistry()
        register_skill_search_tools(self.registry)
        self.runner = ToolRunner(self.registry)
        # Create test file tree
        sub = Path(self.sandbox) / "src"
        sub.mkdir()
        for i in range(20):
            (sub / f"module_{i}.py").write_text(
                f"def function_{i}():\n    return {i}\n"
            )
        docs = Path(self.sandbox) / "docs"
        docs.mkdir()
        for i in range(5):
            (docs / f"guide_{i}.md").write_text(f"# Guide {i}\nContent for guide {i}\n")

    def teardown(self) -> None:
        shutil.rmtree(self.sandbox, ignore_errors=True)

    def time_list_dir(self) -> None:
        self.runner.execute(
            "skill/list-dir",
            {"path": "src", "sandbox_root": self.sandbox},
        )

    def time_glob_py_files(self) -> None:
        self.runner.execute(
            "skill/glob",
            {
                "path": ".",
                "pattern": "**/*.py",
                "sandbox_root": self.sandbox,
            },
        )

    def time_grep_function(self) -> None:
        self.runner.execute(
            "skill/grep",
            {
                "path": ".",
                "pattern": "def function",
                "sandbox_root": self.sandbox,
            },
        )

    def time_grep_with_context(self) -> None:
        self.runner.execute(
            "skill/grep",
            {
                "path": ".",
                "pattern": "def function",
                "context_lines": 2,
                "sandbox_root": self.sandbox,
            },
        )


class SkillSearchRegistrationSuite:
    """Benchmark skill search tool registration."""

    def time_register_skill_search_tools(self) -> None:
        registry = ToolRegistry()
        register_skill_search_tools(registry)
