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.WorkflowCategoryDTO;
import com.qqchen.deploy.backend.workflow.dto.WorkflowDefinitionDTO;
import com.qqchen.deploy.backend.workflow.entity.WorkflowDefinition;
import com.qqchen.deploy.backend.workflow.dto.query.WorkflowDefinitionQuery;
import com.qqchen.deploy.backend.workflow.service.IWorkflowDefinitionService;
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 jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 工作流定义控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/workflow/definition")
@Tag(name = "工作流定义管理", description = "工作流定义管理相关接口")
public class WorkflowDefinitionApiController extends BaseController<WorkflowDefinition, WorkflowDefinitionDTO, Long, WorkflowDefinitionQuery> {

    @Resource
    private IWorkflowDefinitionService workflowDefinitionService;


    @Operation(summary = "保存工作流设计")
    @PostMapping("/design")
    public Response<WorkflowDefinitionDTO> saveWorkflowDesign(@RequestBody WorkflowDefinitionDTO dto) throws Exception {
        return Response.success(workflowDefinitionService.saveWorkflowDesign(dto));
    }

    @GetMapping("/published")
    public Response<List<WorkflowDefinitionDTO>> findPublishedWorkflow() {
        return Response.success(workflowDefinitionService.findPublishedWorkflow());
    }

    @PostMapping("/{workflowDefinitionId}/published")
    public Response<Void> publishedWorkflowDesign(@PathVariable Long workflowDefinitionId) {
        workflowDefinitionService.publishedWorkflowDesign(workflowDefinitionId);
        return Response.success();
    }


    @Operation(summary = "禁用工作流")
    @PostMapping("/{id}/disable")
    public Response<Void> disable(
        @Parameter(description = "工作流定义ID", required = true) @PathVariable Long id
    ) {
        workflowDefinitionService.disable(id);
        return Response.success();
    }

    @Operation(summary = "启用工作流")
    @PostMapping("/{id}/enable")
    public Response<Void> enable(
        @Parameter(description = "工作流定义ID", required = true) @PathVariable Long id
    ) {
        workflowDefinitionService.enable(id);
        return Response.success();
    }


    @Operation(summary = "启用工作流")
    @GetMapping("/categories")
    public Response<List<WorkflowCategoryDTO>> getWorkflowCategories() {
        return Response.success(workflowDefinitionService.getWorkflowCategories());
    }


    @Override
    protected void exportData(HttpServletResponse response, List<WorkflowDefinitionDTO> data) {

    }
}
