"""Write file tool.

Pure function executor for writing file contents.
"""

from wafer_core.rollouts.dtypes import (
    Tool,
    ToolCall,
    ToolFunction,
    ToolFunctionParameter,
    ToolResult,
)
from wafer_core.tools.file_tools.utils import expand_path


# ── Tool Definition ──────────────────────────────────────────────────────────

WRITE_TOOL = Tool(
    type="function",
    function=ToolFunction(
        name="write",
        description="Write content to a file. Creates the file if it doesn't exist, overwrites if it does. Automatically creates parent directories.",
        parameters=ToolFunctionParameter(
            type="object",
            properties={
                "path": {"type": "string", "description": "Path to the file to write (relative or absolute)"},
                "content": {"type": "string", "description": "Content to write to the file"},
            }
        ),
        required=["path", "content"]
    )
)


# ── Pure Function Executor ───────────────────────────────────────────────────

async def exec_write(tool_call: ToolCall) -> ToolResult:
    """Write content to file (pure function - no instance state needed)."""
    path_str = tool_call.args["path"]
    content = tool_call.args["content"]

    abs_path = expand_path(path_str)

    # Create parent directories
    abs_path.parent.mkdir(parents=True, exist_ok=True)

    # Write file
    abs_path.write_text(content, encoding="utf-8")

    return ToolResult(
        tool_call_id=tool_call.id,
        is_error=False,
        content=f"Successfully wrote {len(content)} bytes to {path_str}"
    )
