"""Built-in tool registration helpers.

Provides convenience functions to register all built-in file, git, and
subplan tools into a ``ToolRegistry``, optionally wrapped with changeset
capture.

Also exports :class:`BuiltinAdapter`, a formal adapter implementing the
discover / register / activate / deactivate lifecycle pattern for all
built-in tools.
"""

from __future__ import annotations

from cleveragents.tool.builtins.adapter import BuiltinAdapter
from cleveragents.tool.builtins.changeset import (
    ChangeSet,
    ChangeSetCapture,
    ChangeSetEntry,
)
from cleveragents.tool.builtins.file_tools import (
    ALL_FILE_TOOLS,
    FILE_DELETE_SPEC,
    FILE_EDIT_SPEC,
    FILE_LIST_SPEC,
    FILE_READ_SPEC,
    FILE_SEARCH_SPEC,
    FILE_WRITE_SPEC,
    validate_path,
)
from cleveragents.tool.builtins.git_tools import (
    ALL_GIT_TOOLS,
    GIT_BLAME_SPEC,
    GIT_DIFF_SPEC,
    GIT_LOG_SPEC,
    GIT_STATUS_SPEC,
    validate_repo_path,
)
from cleveragents.tool.builtins.subplan_tool import (
    ALL_SUBPLAN_TOOLS,
    PLAN_SUBPLAN_SPEC,
    SubplanPayload,
    make_plan_subplan_spec,
)
from cleveragents.tool.registry import ToolRegistry


def register_file_tools(registry: ToolRegistry) -> None:
    """Register all 6 built-in file tools into *registry*."""
    for spec in ALL_FILE_TOOLS:
        registry.register(spec)


def register_file_tools_with_changeset(
    registry: ToolRegistry,
    capture: ChangeSetCapture,
) -> None:
    """Register wrapped versions of file tools that record changes.

    Accepts optional *resource_id* and *sandbox_root* via the
    ``ChangeSetCapture`` constructor.
    """
    for spec in ALL_FILE_TOOLS:
        wrapped = capture.wrap_tool(spec)
        registry.register(wrapped)


def register_git_tools(registry: ToolRegistry) -> None:
    """Register all 4 built-in git tools into *registry*."""
    for spec in ALL_GIT_TOOLS:
        registry.register(spec)


def register_subplan_tool(registry: ToolRegistry) -> None:
    """Register the ``builtin/plan-subplan`` tool into *registry*."""
    for spec in ALL_SUBPLAN_TOOLS:
        registry.register(spec)


__all__ = [
    "ALL_FILE_TOOLS",
    "ALL_GIT_TOOLS",
    "ALL_SUBPLAN_TOOLS",
    "FILE_DELETE_SPEC",
    "FILE_EDIT_SPEC",
    "FILE_LIST_SPEC",
    "FILE_READ_SPEC",
    "FILE_SEARCH_SPEC",
    "FILE_WRITE_SPEC",
    "GIT_BLAME_SPEC",
    "GIT_DIFF_SPEC",
    "GIT_LOG_SPEC",
    "GIT_STATUS_SPEC",
    "PLAN_SUBPLAN_SPEC",
    "BuiltinAdapter",
    "ChangeSet",
    "ChangeSetCapture",
    "ChangeSetEntry",
    "SubplanPayload",
    "make_plan_subplan_spec",
    "register_file_tools",
    "register_file_tools_with_changeset",
    "register_git_tools",
    "register_subplan_tool",
    "validate_path",
    "validate_repo_path",
]
