package tech.easyflow.agent.runtime.asynctool;

import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import tech.easyflow.agent.enums.AgentToolType;
import tech.easyflow.agent.runtime.tool.AgentToolExecutionResult;
import tech.easyflow.agent.runtime.tool.WorkflowToolExecutor;
import tech.easyflow.ai.entity.Workflow;

import java.util.Map;

/**
 * Workflow 异步工具子能力实现。
 */
public class WorkflowAsyncSubTools extends AbstractAgentAsyncSubTools {

    private final Workflow workflow;
    private final String toolName;
    private final String displayName;
    private final WorkflowToolExecutor workflowToolExecutor;

    /**
     * 创建 Workflow 异步工具子能力。
     *
     * @param workflow 工作流快照
     * @param toolName runtime 工具名
     * @param displayName 用户可见名称
     * @param workflowToolExecutor Workflow 执行器
     * @param taskStore 任务存储
     * @param taskExecutor 后台执行器
     */
    public WorkflowAsyncSubTools(Workflow workflow,
                                 String toolName,
                                 String displayName,
                                 WorkflowToolExecutor workflowToolExecutor,
                                 AgentAsyncToolTaskStore taskStore,
                                 ThreadPoolTaskExecutor taskExecutor) {
        super(taskStore, taskExecutor);
        this.workflow = workflow;
        this.toolName = toolName;
        this.displayName = displayName;
        this.workflowToolExecutor = workflowToolExecutor;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected String toolType() {
        return AgentToolType.WORKFLOW.name();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected String toolName() {
        return toolName;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected String displayName() {
        return displayName;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected String businessId() {
        return workflow == null || workflow.getId() == null ? null : String.valueOf(workflow.getId());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected AgentToolExecutionResult executeBusiness(Map<String, Object> arguments) {
        return workflowToolExecutor.execute(workflow, arguments);
    }
}
