package com.infoepoch.pms.agent.platform.agent.adapter.out.tool.support;

import org.springframework.ai.chat.model.ToolContext;
import org.springframework.ai.tool.ToolCallback;
import org.springframework.ai.tool.definition.ToolDefinition;
import org.springframework.ai.tool.metadata.ToolMetadata;

import java.nio.file.Files;
import java.nio.file.Path;

/**
 * 带工作区目录校验的工具回调装饰器。
 */
public final class GuardedWorkspaceToolCallback implements ToolCallback {

    private final Path workspaceRoot;
    private final ToolCallback delegate;

    public GuardedWorkspaceToolCallback(Path workspaceRoot, ToolCallback delegate) {
        this.workspaceRoot = workspaceRoot;
        this.delegate = delegate;
    }

    @Override
    public ToolDefinition getToolDefinition() {
        return delegate.getToolDefinition();
    }

    @Override
    public ToolMetadata getToolMetadata() {
        return delegate.getToolMetadata();
    }

    @Override
    public String call(String toolInput) {
        return call(toolInput, null);
    }

    @Override
    public String call(String toolInput, ToolContext toolContext) {
        if (!Files.isDirectory(workspaceRoot)) {
            return "Error: Workspace root does not exist - " + workspaceRoot;
        }
        return toolContext == null ? delegate.call(toolInput) : delegate.call(toolInput, toolContext);
    }
}
