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.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 表单定义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;

    @Override
    public Response<FormDefinitionDTO> create(@Validated @RequestBody FormDefinitionDTO dto) {
        return super.create(dto);
    }

    @Override
    public Response<FormDefinitionDTO> update(@PathVariable Long id, @Validated @RequestBody FormDefinitionDTO dto) {
        return super.update(id, dto);
    }

    @Override
    public Response<Void> delete(@PathVariable Long id) {
        return super.delete(id);
    }

    @Override
    public Response<FormDefinitionDTO> findById(@PathVariable Long id) {
        return super.findById(id);
    }

    @Override
    public Response<List<FormDefinitionDTO>> findAll() {
        return super.findAll();
    }

    @Override
    public Response<Page<FormDefinitionDTO>> page(FormDefinitionQuery query) {
        return super.page(query);
    }

    @Override
    public Response<List<FormDefinitionDTO>> findAll(FormDefinitionQuery query) {
        return super.findAll(query);
    }

    @Override
    public CompletableFuture<Response<Void>> batchProcess(List<FormDefinitionDTO> dtos) {
        return super.batchProcess(dtos);
    }

    @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) {
        // 暂不支持导出
    }
}

