package cn.stylefeng.workflow.instance.modular.manage.controller;

import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.rule.pojo.request.BaseRequest;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.GetResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource;
import cn.stylefeng.workflow.api.entity.instance.RuntimeInstance;
import cn.stylefeng.workflow.api.entity.instance.RuntimeNode;
import cn.stylefeng.workflow.api.enums.NodeStatusEnum;
import cn.stylefeng.workflow.instance.api.pojo.request.RuntimeInstanceRequest;
import cn.stylefeng.workflow.instance.modular.manage.pojo.request.PredictRequest;
import cn.stylefeng.workflow.instance.modular.manage.pojo.request.RuntimeNodeRequest;
import cn.stylefeng.workflow.instance.modular.manage.pojo.vo.PredictResponse;
import cn.stylefeng.workflow.instance.modular.manage.pojo.vo.RuntimeNodeEdgeResult;
import cn.stylefeng.workflow.instance.modular.manage.service.RuntimeInstanceService;
import cn.stylefeng.workflow.instance.modular.manage.service.RuntimeNodeService;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@ApiResource(name = "流程实例控制器")
public class RuntimeInstanceController {

    @Resource
    private RuntimeInstanceService runtimeInstanceService;

    @Resource
    private RuntimeNodeService runtimeNodeService;

    @PostResource(name = "获取流程实例列表（带分页）", path = "/runtimeInstance/page")
    public ResponseData<PageResult<RuntimeInstance>> page(@RequestBody RuntimeInstanceRequest runtimeInstanceRequest) {
        return new SuccessResponseData<>(runtimeInstanceService.findPage(runtimeInstanceRequest));
    }

    @GetResource(name = "获取流程实例详情", path = "/runtimeInstance/detail")
    public ResponseData<RuntimeInstance> detail(@Validated(BaseRequest.detail.class) RuntimeInstanceRequest runtimeInstanceRequest) {
        return new SuccessResponseData<>(runtimeInstanceService.detail(runtimeInstanceRequest));
    }

    @GetResource(name = "获取流程实例进行中的流程节点和流程连线", path = "/runtimeInstance/getRuntimeNodeEdgeList")
    public ResponseData<RuntimeNodeEdgeResult> getRuntimeNodeEdgeList(
            @Validated(BaseRequest.detail.class) RuntimeInstanceRequest runtimeInstanceRequest) {
        RuntimeNodeEdgeResult result = runtimeInstanceService.getRuntimeNodeEdgeList(runtimeInstanceRequest.getInstanceId());
        return new SuccessResponseData<>(result);
    }

    @GetResource(name = "获取流程实例，已经走做的运行时节点", path = "/runtimeInstance/getPassedRuntimeNodeList")
    public ResponseData<List<RuntimeNode>> getPassedRuntimeNodeList(
            @Validated(RuntimeInstanceRequest.getPassedRuntimeNodeList.class) RuntimeInstanceRequest runtimeInstanceRequest) {

        RuntimeNodeRequest runtimeNodeRequest = new RuntimeNodeRequest();
        runtimeNodeRequest.setInstanceId(runtimeInstanceRequest.getInstanceId());
        runtimeNodeRequest.setNodeStatus(NodeStatusEnum.FINISH.getStatus());
        List<RuntimeNode> nodeList = runtimeNodeService.getInstanceFinishedNodeList(runtimeNodeRequest);

        return new SuccessResponseData<>(nodeList);
    }

    @PostResource(name = "流程预测", path = "/runtimeInstance/predict")
    public ResponseData<List<PredictResponse>> predict(@RequestBody @Validated PredictRequest predictRequest) {
        List<PredictResponse> predictResponse = this.runtimeInstanceService.processPredict(predictRequest);
        return new SuccessResponseData<>(predictResponse);
    }

    @GetResource(name = "获取流程的摘要详情", path = "/runtimeInstance/getSummaryDetail")
    public ResponseData<RuntimeInstance> getSummaryDetail(@Validated(BaseRequest.detail.class) RuntimeInstanceRequest runtimeInstanceRequest) {
        return new SuccessResponseData<>(runtimeInstanceService.getSummaryDetail(runtimeInstanceRequest));
    }

}