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

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.PostResource;
import cn.stylefeng.workflow.api.entity.instance.RuntimeInstance;
import cn.stylefeng.workflow.instance.api.WorkflowControlApi;
import cn.stylefeng.workflow.instance.api.pojo.request.WorkflowChangeStatusRequest;
import cn.stylefeng.workflow.instance.api.pojo.request.WorkflowFinishNodeRequest;
import cn.stylefeng.workflow.instance.api.pojo.request.WorkflowSaveDraftRequest;
import cn.stylefeng.workflow.instance.api.pojo.request.WorkflowStartRequest;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@ApiResource(name = "流程控制相关的API")
public class WorkflowControlController {

    @Resource
    private WorkflowControlApi workflowControlApi;

    @PostResource(name = "发起流程接口", path = "/workflowControl/start")
    public ResponseData<RuntimeInstance> start(@RequestBody @Validated WorkflowStartRequest workflowStartRequest) {
        RuntimeInstance result = workflowControlApi.start(workflowStartRequest);
        return new SuccessResponseData<>(result);
    }

    @PostResource(name = "保存流程草稿", path = "/workflowControl/saveDraft")
    public ResponseData<?> saveDraft(@RequestBody @Validated WorkflowSaveDraftRequest workflowSaveDraftRequest) {
        workflowControlApi.saveDraft(workflowSaveDraftRequest);
        return new SuccessResponseData<>();
    }

    @PostResource(name = "完成运行中节点", path = "/workflowControl/finishNode")
    public ResponseData<?> finishNode(@RequestBody @Validated WorkflowFinishNodeRequest workflowFinishNodeRequest) {
        workflowControlApi.finishNode(workflowFinishNodeRequest);
        return new SuccessResponseData<>();
    }

    @PostResource(name = "挂起流程", path = "/workflowControl/suspend")
    public ResponseData<?> suspend(@RequestBody @Validated WorkflowChangeStatusRequest workflowSuspendRequest) {
        workflowControlApi.suspend(workflowSuspendRequest);
        return new SuccessResponseData<>();
    }

    @PostResource(name = "激活流程", path = "/workflowControl/active")
    public ResponseData<?> active(@RequestBody @Validated WorkflowChangeStatusRequest workflowSuspendRequest) {
        workflowControlApi.active(workflowSuspendRequest);
        return new SuccessResponseData<>();
    }

    @PostResource(name = "作废流程", path = "/workflowControl/invalid")
    public ResponseData<?> invalid(@RequestBody @Validated WorkflowChangeStatusRequest workflowSuspendRequest) {
        workflowControlApi.invalid(workflowSuspendRequest);
        return new SuccessResponseData<>();
    }

}
