package tech.easyflow.admin.controller.ai;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.easyagents.flow.core.chain.ChainDefinition;
import com.easyagents.flow.core.chain.Node;
import com.easyagents.flow.core.node.ConfirmNode;
import com.easyagents.flow.core.node.EndNode;
import com.easyagents.flow.core.node.StartNode;
import com.easyagents.flow.core.parser.ChainParser;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tech.easyflow.ai.easyagentsflow.service.WorkflowDatacenterContentService;
import tech.easyflow.ai.entity.Workflow;
import tech.easyflow.ai.service.WorkflowService;
import tech.easyflow.common.domain.Result;
import tech.easyflow.common.web.exceptions.BusinessException;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping("/api/v1/workflowNode")
@RestController
public class WorkFlowNodeController {

    @Resource
    private WorkflowService workflowService;
    @Resource
    private ChainParser chainParser;
    @Resource
    private WorkflowDatacenterContentService workflowDatacenterContentService;

    @GetMapping("/getChainParams")
    public Result<?> getChainParams(String currentId, String workflowId) {
        if (workflowId.equals(currentId)) {
            throw new BusinessException("工作流不能作为自身子节点");
        }
        JSONObject nodeData = new JSONObject();
        Workflow workflow = workflowService.getById(workflowId);
        if (workflow == null) {
            throw new BusinessException("工作流不存在: " + workflowId);
        }
        nodeData.put("workflowId", workflow.getId());
        nodeData.put("workflowName", workflow.getTitle());

        ChainDefinition definition = chainParser.parse(workflowDatacenterContentService.prepareContent(workflow.getContent()));
        List<Node> nodes = definition.getNodes();
        JSONArray inputs = new JSONArray();
        JSONArray outputs = new JSONArray();
        for (Node node : nodes) {
            if (node instanceof StartNode) {
                inputs = JSON.parseArray(JSON.toJSONString(node.getParameters()));
                handleArray(inputs);
            }
            if (node instanceof EndNode) {
                outputs = JSON.parseArray(JSON.toJSONString(((EndNode) node).getOutputDefs()));
                handleArray(outputs);
            }
            if (node instanceof ConfirmNode) {
                throw new BusinessException("工作流存在【确认节点】，暂不支持作为子节点");
            }
        }
        nodeData.put("parameters", inputs);
        nodeData.put("outputDefs", outputs);
        return Result.ok(nodeData);
    }

    private void handleArray(JSONArray array) {
        if (array != null) {
            for (Object o : array) {
                JSONObject obj = (JSONObject) o;
                obj.put("id", IdUtil.simpleUUID());
                obj.put("nameDisabled", true);
                obj.put("dataTypeDisabled", true);
                obj.put("deleteDisabled", true);
                obj.put("addChildDisabled", true);
                obj.put("refType", "ref");
                JSONArray children = obj.getJSONArray("children");
                if (children != null) {
                    handleArray(children);
                }
            }
        }
    }
}
