package com.lingyue.parse.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lingyue.common.domain.AjaxResult;
import com.lingyue.parse.service.ParseTaskCenterService;
import com.lingyue.parse.vo.ParseTaskCenterVO;
import com.lingyue.parse.vo.ParseTaskStatisticsVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 解析任务中心控制器
 */
@RestController
@RequestMapping("/api/v1/tasks")
@RequiredArgsConstructor
@Tag(name = "解析任务中心", description = "解析任务列表、详情、统计管理接口")
public class ParseTaskCenterController {

    private final ParseTaskCenterService taskCenterService;

    @GetMapping("/list")
    @Operation(summary = "查询解析任务列表")
    @Parameter(name = "status", description = "任务状态，可选: pending, processing, completed, failed")
    @Parameter(name = "pageNum", description = "页码", example = "1")
    @Parameter(name = "pageSize", description = "每页数量", example = "10")
    public AjaxResult<?> getTaskList(
            @RequestParam(value = "status", required = false) String status,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {

        Page<ParseTaskCenterVO> page = taskCenterService.getTaskList(status, pageNum, pageSize);
        return AjaxResult.success(page);
    }

    @GetMapping("/{taskId}/detail")
    @Operation(summary = "查询解析任务详情")
    public AjaxResult<?> getTaskDetail(@PathVariable("taskId") String taskId) {
        ParseTaskCenterVO detail = taskCenterService.getTaskDetail(taskId);
        if (detail == null) {
            return AjaxResult.error("任务不存在");
        }
        return AjaxResult.success(detail);
    }

    @GetMapping("/by-document/{documentId}")
    @Operation(summary = "根据文档ID查询解析任务详情")
    public AjaxResult<?> getTaskDetailByDocumentId(
            @PathVariable("documentId") String documentId) {

        ParseTaskCenterVO detail = taskCenterService.getTaskDetailByDocumentId(documentId);
        if (detail == null) {
            return AjaxResult.error("任务不存在");
        }
        return AjaxResult.success(detail);
    }

    @GetMapping("/statistics")
    @Operation(summary = "查询解析任务统计信息")
    public AjaxResult<ParseTaskStatisticsVO> getTaskStatistics() {
        ParseTaskStatisticsVO statistics = taskCenterService.getTaskStatistics();
        return AjaxResult.success(statistics);
    }

    @DeleteMapping("/{taskId}")
    @Operation(summary = "删除解析任务", description = "仅允许删除已完成或失败的任务")
    public AjaxResult<?> deleteTask(@PathVariable("taskId") String taskId) {
        try {
            boolean deleted = taskCenterService.deleteTask(taskId);
            if (!deleted) {
                return AjaxResult.error("任务不存在");
            }
            return AjaxResult.success("删除成功");
        } catch (IllegalStateException e) {
            return AjaxResult.error(e.getMessage());
        } catch (Exception e) {
            return AjaxResult.error("删除失败: " + e.getMessage());
        }
    }
}


