package com.qqchen.deploy.backend.workflow.api;

import com.qqchen.deploy.backend.framework.api.Response;
import com.qqchen.deploy.backend.framework.controller.BaseController;
import com.qqchen.deploy.backend.workflow.dto.FormDefinitionDTO;
import com.qqchen.deploy.backend.workflow.dto.query.FormDefinitionQuery;
import com.qqchen.deploy.backend.workflow.entity.FormDefinition;
import com.qqchen.deploy.backend.workflow.service.IFormDefinitionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 表单定义API控制器
 *
 * @author qqchen
 * @date 2025-10-24
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/forms/definitions")
@Tag(name = "表单定义管理", description = "表单定义管理相关接口")
public class FormDefinitionApiController extends BaseController<FormDefinition, FormDefinitionDTO, Long, FormDefinitionQuery> {

    @Resource
    private IFormDefinitionService formDefinitionService;

    @Operation(summary = "发布表单")
    @PostMapping("/{id}/publish")
    public Response<FormDefinitionDTO> publish(
            @Parameter(description = "表单定义ID", required = true) @PathVariable Long id
    ) {
        FormDefinitionDTO result = formDefinitionService.publish(id);
        return Response.success(result);
    }

    @Operation(summary = "根据key获取最新版本表单")
    @GetMapping("/latest/{key}")
    public Response<FormDefinitionDTO> getLatestByKey(
            @Parameter(description = "表单标识", required = true) @PathVariable String key
    ) {
        FormDefinitionDTO result = formDefinitionService.getLatestByKey(key);
        return Response.success(result);
    }

    @Override
    protected void exportData(jakarta.servlet.http.HttpServletResponse response, java.util.List<FormDefinitionDTO> data) {
        // 暂不支持导出
    }
}

