package cn.axzo.workflow.starter.api;

import cn.axzo.workflow.common.annotation.InvokeMode;
import cn.axzo.workflow.common.model.request.bpmn.RestBpmnProcessVariable;
import cn.axzo.workflow.common.model.request.bpmn.process.BpmnProcessInstanceAbortDTO;
import cn.axzo.workflow.common.model.request.bpmn.process.BpmnProcessInstanceCreateDTO;
import cn.axzo.workflow.common.model.request.bpmn.process.BpmnProcessInstanceQueryDTO;
import cn.axzo.workflow.common.model.request.bpmn.task.BpmnTaskAuditDTO;
import cn.axzo.workflow.common.model.response.bpmn.BatchOperationResultVO;
import cn.axzo.workflow.common.model.response.bpmn.process.BpmnProcessInstanceVO;
import cn.axzo.workflow.common.util.ThreadUtil;
import cn.axzo.workflow.starter.feign.ext.WorkflowEngineStarterFeignConfiguration;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Nullable;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

import static cn.axzo.workflow.common.enums.RpcInvokeModeEnum.ASYNC;
import static cn.axzo.workflow.common.enums.RpcInvokeModeEnum.SYNC;


/**
 * 模拟生成的受限访问的接口定义
 *
 * @author wangli
 * @since 2024/5/28 16:12
 */
@FeignClient(name = "workflow-engine-starter",
        url = "${axzo.service.workflow-engine:workflow-engine:8080}",
        configuration = WorkflowEngineStarterFeignConfiguration.class)
public interface WorkflowCoreService {

    @PostMapping("/api/process/instance/create")
    @InvokeMode(SYNC)
    String createProcessInstance(@Validated @RequestBody BpmnProcessInstanceCreateDTO dto);

    @Operation(summary = "同意，MQ 触发规则:1. 当前审批任务会依次触发 process-task-completed 和 process-task-deleted 事件(如果有下一级审批,则会触发第 2.1 点中的事件,如果当前审核任务最后一级审批,则会触发第 2.2 点中的事件)，2.1. 下一级审批任务会依次触发 process-task-assigned 和 process-task-created 事件，2.2. 流程实例正常结束会触发 process-instance-completed 事件")
    @PostMapping("/api/process/task/approve")
    Boolean approveTask(@Validated @RequestBody BpmnTaskAuditDTO dto);

    @Operation(summary = "获得流程实例")
    @GetMapping("/api/process/instance/get")
    BpmnProcessInstanceVO getProcessInstanceVO(@Validated @RequestBody BpmnProcessInstanceQueryDTO dto);

    @Operation(summary = "获取指定流程实例的流程变量")
    @GetMapping("/api/process/instance/cooperation-org")
    Map<String, Object> getProcessVariables(@NotBlank(message = "流程实例 ID 不能为空") @RequestParam String processInstanceId,
                                            @Nullable @RequestParam(required = false) String tenantId);

    @Operation(summary = "为指定流程新增变量")
    @GetMapping("/api/process/variable/create/{executionId}")
    void createVariable(@PathVariable @NotBlank(message = "流程实例 ID 不能为空") String executionId,
                        @RequestBody @Validated RestBpmnProcessVariable restVariable);

    @Operation(summary = "批量中止流程实例")
    @DeleteMapping("/api/process/instance/batch/abort")
    BatchOperationResultVO batchAbortProcessInstance(@Validated @RequestBody List<BpmnProcessInstanceAbortDTO> dtos);

    default WorkflowCoreService sync() {
        ThreadUtil.set(SYNC);
        return this;
    }

    default WorkflowCoreService async() {
        ThreadUtil.set(ASYNC);
        return this;
    }

}
