"""ASV benchmarks for skill-level file operation tools.

Measures the performance of:
- ReadFile with binary detection
- WriteFile with atomic writes
- EditFile with permission preservation
- DeleteFile operations
- Registration helpers
"""

from __future__ import annotations

import shutil
import sys
import tempfile
from pathlib import Path

try:
    from cleveragents.skills.builtins.file_ops import register_skill_file_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.file_ops import register_skill_file_tools
    from cleveragents.tool.registry import ToolRegistry
    from cleveragents.tool.runner import ToolRunner


class SkillFileToolSuite:
    """Benchmark skill-level file tool operations."""

    def setup(self) -> None:
        self.sandbox = tempfile.mkdtemp()
        self.registry = ToolRegistry()
        register_skill_file_tools(self.registry)
        self.runner = ToolRunner(self.registry)
        # Create test files
        (Path(self.sandbox) / "read_test.txt").write_text("hello benchmark\n" * 100)
        (Path(self.sandbox) / "edit_test.txt").write_text(
            "original content here\n" * 10
        )

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

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

    def time_skill_file_write(self) -> None:
        self.runner.execute(
            "skill/file-write",
            {
                "path": "bench_write.txt",
                "content": "benchmark data",
                "sandbox_root": self.sandbox,
            },
        )

    def time_skill_file_edit(self) -> None:
        # Reset file content first
        (Path(self.sandbox) / "edit_test.txt").write_text(
            "original content here\n" * 10
        )
        self.runner.execute(
            "skill/file-edit",
            {
                "path": "edit_test.txt",
                "old_text": "original",
                "new_text": "modified",
                "sandbox_root": self.sandbox,
            },
        )

    def time_skill_file_delete(self) -> None:
        # Create file to delete
        (Path(self.sandbox) / "delete_me.txt").write_text("bye")
        self.runner.execute(
            "skill/file-delete",
            {"path": "delete_me.txt", "sandbox_root": self.sandbox},
        )


class SkillFileRegistrationSuite:
    """Benchmark skill file tool registration."""

    def time_register_skill_file_tools(self) -> None:
        registry = ToolRegistry()
        register_skill_file_tools(registry)
