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

import com.qqchen.deploy.backend.framework.api.Response;
import com.qqchen.deploy.backend.workflow.dto.request.ApprovalTaskRequest;
import com.qqchen.deploy.backend.workflow.dto.response.ApprovalTaskDTO;
import com.qqchen.deploy.backend.workflow.service.IApprovalTaskService;
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.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 审批任务管理接口
 *
 * @author qqchen
 * @date 2025-10-23
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/approval-tasks")
@Tag(name = "审批任务管理", description = "审批任务相关接口")
public class ApprovalTaskApiController {

    @Resource
    private IApprovalTaskService approvalTaskService;

    @Operation(summary = "查询我的待办审批任务")
    @GetMapping("/my-tasks")
    public Response<List<ApprovalTaskDTO>> getMyTasks(
            @Parameter(description = "用户名", required = true) @RequestParam String username
    ) {
        List<ApprovalTaskDTO> tasks = approvalTaskService.getMyTasks(username);
        return Response.success(tasks);
    }

    @Operation(summary = "查询流程实例的审批任务")
    @GetMapping("/process/{processInstanceId}")
    public Response<List<ApprovalTaskDTO>> getTasksByProcessInstance(
            @Parameter(description = "流程实例ID", required = true) 
            @PathVariable String processInstanceId
    ) {
        List<ApprovalTaskDTO> tasks = approvalTaskService.getTasksByProcessInstance(processInstanceId);
        return Response.success(tasks);
    }

    @Operation(summary = "查询审批任务详情")
    @GetMapping("/{taskId}")
    public Response<ApprovalTaskDTO> getTaskById(
            @Parameter(description = "任务ID", required = true) 
            @PathVariable String taskId
    ) {
        ApprovalTaskDTO task = approvalTaskService.getTaskById(taskId);
        return Response.success(task);
    }

    @Operation(summary = "完成审批任务（通过/拒绝）")
    @PostMapping("/complete")
    public Response<Void> completeTask(
            @Valid @RequestBody ApprovalTaskRequest request
    ) {
        approvalTaskService.completeTask(request);
        return Response.success();
    }
}

