package tech.easyflow.ai.node;

import com.easyagents.flow.core.chain.Chain;
import com.easyagents.flow.core.chain.runtime.ChainExecutor;
import com.easyagents.flow.core.node.BaseNode;
import tech.easyflow.ai.entity.Workflow;
import tech.easyflow.ai.service.WorkflowService;
import tech.easyflow.common.util.SpringContextUtil;

import java.util.Map;

public class WorkflowNode extends BaseNode {

    private String workflowId;

    public WorkflowNode() {
    }

    public WorkflowNode(String workflowId) {
        this.workflowId = workflowId;
    }

    @Override
    public Map<String, Object> execute(Chain chain) {

        Map<String, Object> params = chain.getState().resolveParameters(this);
        WorkflowService service = SpringContextUtil.getBean(WorkflowService.class);
        Workflow workflow = service.getById(workflowId);
        if (workflow == null) {
            throw new RuntimeException("工作流不存在：" + workflowId);
        }
        ChainExecutor executor = SpringContextUtil.getBean(ChainExecutor.class);
        return executor.execute(workflowId, params);
    }

    public String getWorkflowId() {
        return workflowId;
    }

    public void setWorkflowId(String workflowId) {
        this.workflowId = workflowId;
    }
}
