package cn.axzo.workflow.client.feign.bpmn;

import cn.axzo.workflow.common.model.request.bpmn.definition.BpmnProcessDefinitionUpdateDTO;
import cn.axzo.workflow.common.model.request.bpmn.model.BpmnModelUpdateDTO;
import cn.axzo.workflow.common.model.request.bpmn.process.BpmnProcessDefinitionPageDTO;
import cn.axzo.workflow.common.model.response.BpmPageResult;
import cn.axzo.workflow.common.model.response.bpmn.process.BpmnProcessDefinitionVO;
import cn.azxo.framework.common.model.CommonResponse;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * 流程定义 API
 *
 * @author wangli
 * @since 2023/9/21 16:25
 */
@FeignClient(name = "workflow-engine", url = "${axzo.service.workflow-engine:http://workflow-engine:8080}")
public interface ProcessDefinitionApi {


    /**
     * 获取活跃的流程定义分页
     */
    @GetMapping("/api/process/definition/page")
    CommonResponse<BpmPageResult<BpmnProcessDefinitionVO>> getProcessDefinitionPage(@Validated @RequestBody BpmnProcessDefinitionPageDTO dto);

    /**
     * 通过模型 ID 更新流程定义
     *
     * @param dto
     * @return
     */
    @PutMapping("/api/process/definition/update")
    CommonResponse<Boolean> updateProcessDefinition(@Validated @RequestBody BpmnProcessDefinitionUpdateDTO dto);

    /**
     * 获得指定定义编号对应的流程定义内容
     *
     * @param processDefinitionId 编号
     * @return 流程定义
     */
    @GetMapping("/api/process/definition/get")
    CommonResponse<BpmnProcessDefinitionVO> getProcessDefinition(@NotBlank(message = "流程定义 ID 不能为空") @RequestParam String processDefinitionId);

    /**
     * 获得 deploymentId 对应的 ProcessDefinition
     *
     * @param deploymentId 部署编号
     * @return 流程定义
     */
    @GetMapping("/api/process/definition/getByDeploymentId")
    CommonResponse<BpmnProcessDefinitionVO> getProcessDefinitionByDeploymentId(
            @NotBlank(message = "流程部署 ID 不能为空") @RequestParam String deploymentId);


    /**
     * 获得流程定义标识对应的激活的流程定义
     *
     * @param key 流程定义的标识
     * @return 流程定义
     */
    @GetMapping("/api/process/definition/active/getByKey")
    CommonResponse<BpmnProcessDefinitionVO> getActiveProcessDefinitionByKey(@NotBlank(message = "模型定义KEY不能为空") @RequestParam String key);


    /**
     * 挂起/激活流程，
     * 激活：SuspensionState.ACTIVE.getStateCode()
     * 挂起：SuspensionState.SUSPENDED.getStateCode()
     * {@see SuspensionState}
     */
    @PutMapping("/api/process/definition/state/update")
    CommonResponse<Boolean> getActiveProcessDefinitionByKey(@NotBlank(message = "流程定义ID不能为空") @RequestParam String processDefinitionId,
                                                            @NotNull(message = "状态不能为空") @RequestParam Integer state);

    /**
     * 获取指定模型的定义 ID
     *
     * @return 流程定义ID
     */
    @GetMapping("/api/process/definition/active/getByCategory")
    CommonResponse<String> getActiveProcessDefinitionId(@RequestParam(required = false) String tenantId,
                                                        @NotBlank(message = "分类不能为空") @RequestParam(required = false) String category);

    /**
     * 获取指定模型激活的流程定义 JSON 模型
     *
     * @return 流程定义ID
     */
    @GetMapping("/api/process/definition/active/json/model")
    CommonResponse<BpmnModelUpdateDTO> getActiveProcessDefinitionJsonModel(@NotBlank(message = "模型 ID 不能为空") @RequestParam(required = false) String modelId,
                                                                           @NotBlank(message = "分类不能为空") @RequestParam(required = false) String category,
                                                                           @RequestParam(required = false) String tenantId);

    /**
     * 删除流程部署及定义
     *
     * @param deploymentId 流程定义部署 ID
     * @param cascade      是否级联参数定义对应的流程实例及 job 等管理内容
     * @return
     */
    @GetMapping("/api/process/definition/detele")
    CommonResponse<Void> delete(@NotBlank(message = "流程定义部署 ID 不能为空") String deploymentId,
                                @RequestParam(required = false) Boolean cascade);
}
