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

import cn.axzo.workflow.common.model.request.form.definition.FormDefinitionUpdateDTO;
import cn.axzo.workflow.common.model.response.form.definition.FormDefinitionVO;
import cn.axzo.workflow.core.service.FormDefinitionService;
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.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.GetMapping;
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/19 16:45
 */

@Tag(name = "运维管理 - 表单定义")
@Slf4j
@RequestMapping("/web/v1/api/form/definition")
@RestController
@ErrorReporter
@Validated
public class FormDefinitionController {

    @Resource
    private FormDefinitionService formDefinitionService;
    @Resource
    private FormModelService formModelService;

    @Operation(summary = "更新表单定义内容")
    @PutMapping("/update")
    public CommonResponse<Boolean> updateFormDefinition(@Validated @RequestBody FormDefinitionUpdateDTO dto) {
        formDefinitionService.updateFormDefinition(dto);
        return success(true);
    }


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


}
