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.WorkflowHistoricalInstancesDTO;
import com.qqchen.deploy.backend.workflow.dto.WorkflowInstanceDTO;
import com.qqchen.deploy.backend.workflow.dto.WorkflowInstanceStartRequest;
import com.qqchen.deploy.backend.workflow.dto.WorkflowTemplateWithInstancesDTO;
import com.qqchen.deploy.backend.workflow.entity.WorkflowInstance;
import com.qqchen.deploy.backend.workflow.dto.query.WorkflowDefinitionQuery;
import com.qqchen.deploy.backend.workflow.dto.query.WorkflowHistoricalInstancesQuery;
import com.qqchen.deploy.backend.workflow.dto.query.WorkflowInstanceQuery;
import com.qqchen.deploy.backend.workflow.service.IWorkflowInstanceService;
import io.swagger.v3.oas.annotations.Operation;
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.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 工作流定义控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/workflow/instance")
@Tag(name = "工作流实例管理", description = "工作流定义实例相关接口")
public class WorkflowInstanceApiController extends BaseController<WorkflowInstance, WorkflowInstanceDTO, Long, WorkflowInstanceQuery> {

    @Resource
    private IWorkflowInstanceService workflowInstanceService;

    @Operation(summary = "启动工作流")
    @PostMapping("/start")
    public Response<Void> startWorkflow(@RequestBody WorkflowInstanceStartRequest request) {
        workflowInstanceService.startWorkflow(request);
        return Response.success();
    }

    @Operation(summary = "查询已发布流程模板及其最近实例")
    @GetMapping("/templates-with-instances")
    public Response<Page<WorkflowTemplateWithInstancesDTO>> getTemplatesWithInstances(WorkflowDefinitionQuery query) {
        return Response.success(workflowInstanceService.findTemplatesWithRecentInstances(query));
    }

    @Operation(summary = "分页查询历史实例")
    @GetMapping("/historical-instances")
    public Response<Page<WorkflowHistoricalInstancesDTO>> historicalInstances(WorkflowHistoricalInstancesQuery query) {
        return Response.success(workflowInstanceService.historicalInstances(query));
    }

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

    }
}
