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.WorkflowNodeDefinitionDTO;
import com.qqchen.deploy.backend.workflow.dto.WorkflowNodeTypeDefinedDTO;
import com.qqchen.deploy.backend.workflow.entity.WorkflowNodeDefinition;
import com.qqchen.deploy.backend.workflow.enums.NodeCategoryEnums;
import com.qqchen.deploy.backend.workflow.dto.query.WorkflowNodeDefinitionQuery;
import com.qqchen.deploy.backend.workflow.service.IWorkflowNodeDefinitionService;
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/node-definition")
@Tag(name = "工作流节点定义管理", description = "工作流节点定义管理相关接口")
public class WorkflowNodeDefinitionApiController extends BaseController<WorkflowNodeDefinition, WorkflowNodeDefinitionDTO, Long, WorkflowNodeDefinitionQuery> {

    @Resource
    private IWorkflowNodeDefinitionService workflowNodeDefinitionService;


    @Operation(summary = "根据节点类型获取节点定义")
    @GetMapping("/defined")
    public Response<List<WorkflowNodeTypeDefinedDTO>> defined() {
        return Response.success(workflowNodeDefinitionService.defined());
    }


    @Operation(summary = "根据节点类型获取节点定义")
    @GetMapping("/type/{type}")
    public Response<WorkflowNodeDefinitionDTO> getByType(
        @Parameter(description = "节点类型", required = true) @PathVariable String type
    ) {
        return Response.success(workflowNodeDefinitionService.getByType(type));
    }

    @Operation(summary = "根据分类获取节点定义列表")
    @GetMapping("/category/{category}")
    public Response<List<WorkflowNodeDefinitionDTO>> listByCategory(
        @Parameter(description = "节点分类", required = true) @PathVariable NodeCategoryEnums category
    ) {
        return Response.success(workflowNodeDefinitionService.listByCategory(category));
    }

    @Operation(summary = "获取所有启用的节点定义")
    @GetMapping("/enabled")
    public Response<List<WorkflowNodeDefinitionDTO>> listAllEnabled() {
        return Response.success(workflowNodeDefinitionService.listAllEnabled());
    }

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

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

    @Override
    protected void exportData(HttpServletResponse response, List<WorkflowNodeDefinitionDTO> data) {
        // 暂不支持导出
    }
} 