"""ASV benchmarks for built-in file tools.

Measures the performance of:
- File read/write/edit/delete operations
- File list and search operations
- ChangeSet capture overhead
- Tool registration helpers
"""

from __future__ import annotations

import shutil
import sys
import tempfile
from pathlib import Path

try:
    from cleveragents.tool.builtins import (
        register_file_tools,
        register_file_tools_with_changeset,
    )
    from cleveragents.tool.builtins.changeset import ChangeSetCapture
    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.tool.builtins import (
        register_file_tools,
        register_file_tools_with_changeset,
    )
    from cleveragents.tool.builtins.changeset import ChangeSetCapture
    from cleveragents.tool.registry import ToolRegistry
    from cleveragents.tool.runner import ToolRunner


class FileToolSuite:
    """Benchmark built-in file tool operations."""

    def setup(self) -> None:
        self.sandbox = tempfile.mkdtemp()
        self.registry = ToolRegistry()
        register_file_tools(self.registry)
        self.runner = ToolRunner(self.registry)
        # Create test files
        (Path(self.sandbox) / "read_test.txt").write_text("hello benchmark\n" * 100)
        sub = Path(self.sandbox) / "sub"
        sub.mkdir()
        for i in range(10):
            (sub / f"file_{i}.txt").write_text(f"content {i}\n")

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

    def time_file_read(self) -> None:
        self.runner.execute(
            "builtin/file-read",
            {"path": "read_test.txt", "sandbox_root": self.sandbox},
        )

    def time_file_write(self) -> None:
        self.runner.execute(
            "builtin/file-write",
            {
                "path": "bench_write.txt",
                "content": "bench",
                "sandbox_root": self.sandbox,
            },
        )

    def time_file_list(self) -> None:
        self.runner.execute(
            "builtin/file-list",
            {"path": "sub", "sandbox_root": self.sandbox},
        )

    def time_file_search(self) -> None:
        self.runner.execute(
            "builtin/file-search",
            {"path": "sub", "pattern": "content", "sandbox_root": self.sandbox},
        )


class ChangeSetCaptureSuite:
    """Benchmark changeset capture overhead."""

    def setup(self) -> None:
        self.sandbox = tempfile.mkdtemp()
        self.capture = ChangeSetCapture(plan_id="bench-plan")
        self.registry = ToolRegistry()
        register_file_tools_with_changeset(self.registry, self.capture)
        self.runner = ToolRunner(self.registry)

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

    def time_write_with_capture(self) -> None:
        self.runner.execute(
            "builtin/file-write",
            {"path": "captured.txt", "content": "data", "sandbox_root": self.sandbox},
        )

    def time_get_changeset(self) -> None:
        self.capture.get_changeset()


class RegistrationSuite:
    """Benchmark tool registration helpers."""

    def time_register_file_tools(self) -> None:
        registry = ToolRegistry()
        register_file_tools(registry)

    def time_register_with_changeset(self) -> None:
        registry = ToolRegistry()
        capture = ChangeSetCapture(plan_id="bench")
        register_file_tools_with_changeset(registry, capture)
