package cn.axzo.workflow.server.controller.web.form;

import cn.axzo.workflow.common.model.request.bpmn.model.BpmnModelSearchDTO;
import cn.axzo.workflow.common.model.request.form.model.FormModelCreateDTO;
import cn.axzo.workflow.common.model.request.form.model.FormModelUpdateDTO;
import cn.axzo.workflow.common.model.response.BpmPageResult;
import cn.axzo.workflow.common.model.response.form.model.FormModelBaseVO;
import cn.axzo.workflow.core.service.FormModelService;
import cn.axzo.workflow.server.common.annotation.ErrorReporter;
import cn.azxo.framework.common.model.CommonResponse;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
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.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;

import static cn.azxo.framework.common.model.CommonResponse.success;

/**
 * 表单模型相关控制器
 *
 * @author wangli
 * @since 2023/7/25 10:16
 */
@Tag(name = "运维管理 - 表单模型")
@Slf4j
@RequestMapping("/web/v1/api/form/model")
@RestController
@ErrorReporter
@Validated
public class FormModelController {

    @Resource
    private FormModelService formModelService;

    @Operation(summary = "表单模型分页")
    @GetMapping("/page")
    public CommonResponse<BpmPageResult<FormModelBaseVO>> page(@Validated @RequestBody BpmnModelSearchDTO dto) {
        return success(formModelService.getModelPage(dto));
    }

    @Operation(summary = "创建表单模型")
    @PostMapping("/create")
    public CommonResponse<String> create(@Validated @RequestBody FormModelCreateDTO dto) {
        return success(formModelService.createFormModel(dto));
    }

    @Operation(summary = "修改表单模型")
    @PutMapping("/update")
    public CommonResponse<Void> update(@Validated @RequestBody FormModelUpdateDTO dto) {
        formModelService.updateFormModel(dto);
        return success();
    }

    @Operation(summary = "通过表单模型 ID 删除表单模型")
    @DeleteMapping("/delete")
    public CommonResponse<Boolean> deleteById(
            @ApiParam(name = "表单模型 ID") @NotBlank(message = "表单模型 ID 不能为空") @RequestParam String formModelId,
            @RequestParam String tenantId) {
        formModelService.deleteFormModelById(formModelId, tenantId);
        return success(true);
    }

    @Operation(summary = "通过表单模型 KEY 删除表单模型")
    @DeleteMapping("/deleteByKey")
    public CommonResponse<Boolean> deleteByKey(
            @ApiParam(name = "表单模型 KEY") @NotBlank(message = "表单模型 KEY 不能为空") @RequestParam String formModelKey,
            @NotBlank(message = "租户不能为空") @RequestParam String tenantId) {
        formModelService.deployFormModelByKey(formModelKey, tenantId);
        return success(true);
    }

    @Operation(summary = "获取指定表单模型 ID 的内容")
    @GetMapping("/get")
    public CommonResponse<FormModelBaseVO> getById(@NotBlank(message = "表单模型 ID 不能为空") @RequestParam String formModelId,
                                                   @NotBlank(message = "租户不能为空") @RequestParam String tenantId) {
        return success(formModelService.getById(formModelId, tenantId));
    }

    @Operation(summary = "获取指定表单模型 KEy 的内容")
    @GetMapping("/getByKey")
    public CommonResponse<FormModelBaseVO> getByKey(@NotBlank(message = "表单模型 KEY 不能为空") @RequestParam String formModelKey,
                                                    @NotBlank(message = "租户不能为空") @RequestParam String tenantId) {
        return success(formModelService.getByKey(formModelKey, tenantId));
    }

    @Operation(summary = "通过表单模型 ID 发布")
    @PostMapping("/deploy")
    public CommonResponse<String> deployById(@NotBlank(message = "模型 ID 不能为空") @RequestParam String formModelId,
                                             @NotBlank(message = "租户不能为空") @RequestParam String tenantId) {
        return success(formModelService.deployFormModelById(formModelId, tenantId));
    }

    @Operation(summary = "通过表单模型 KEY 发布")
    @PostMapping("/deployByKey")
    public CommonResponse<String> deployByKey(@NotBlank(message = "模型 KEY 不能为空") @RequestParam String formModelKey,
                                              @NotBlank(message = "租户不能为空") @RequestParam String tenantId) {
        return success(formModelService.deployFormModelByKey(formModelKey, tenantId));
    }

}
