package com.lingyue.parse.controller;

import com.lingyue.common.domain.AjaxResult;
import com.lingyue.parse.service.TaskProgressService;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 任务进度更新控制器
 * 内部使用，供其他服务更新任务进度
 * 
 * @author lingyue
 * @since 2026-01-22
 */
@Slf4j
@RestController
@RequestMapping("/api/internal/task-progress")
@RequiredArgsConstructor
@Tag(name = "任务进度（内部）", description = "内部任务进度更新接口")
public class TaskProgressController {

    private final TaskProgressService taskProgressService;

    /**
     * 更新结构化解析进度
     */
    @PostMapping("/structured/{documentId}")
    @Operation(summary = "更新结构化解析进度")
    public AjaxResult<?> updateStructuredProgress(
            @PathVariable @Parameter(description = "文档ID") String documentId,
            @RequestBody Map<String, Object> request) {
        
        String status = (String) request.get("status");
        Integer progress = (Integer) request.get("progress");
        Integer elementCount = (Integer) request.get("elementCount");
        Integer imageCount = (Integer) request.get("imageCount");
        Integer tableCount = (Integer) request.get("tableCount");
        
        taskProgressService.updateStructuredProgress(documentId, status, progress, 
                elementCount, imageCount, tableCount);
        
        return AjaxResult.success();
    }

    /**
     * 更新 NER 进度
     */
    @PostMapping("/ner/{documentId}")
    @Operation(summary = "更新NER进度")
    public AjaxResult<?> updateNerProgress(
            @PathVariable @Parameter(description = "文档ID") String documentId,
            @RequestBody Map<String, Object> request) {
        
        String status = (String) request.get("status");
        Integer progress = (Integer) request.get("progress");
        String nerTaskId = (String) request.get("nerTaskId");
        Integer entityCount = (Integer) request.get("entityCount");
        Integer relationCount = (Integer) request.get("relationCount");
        
        taskProgressService.updateNerProgress(documentId, status, progress, 
                nerTaskId, entityCount, relationCount);
        
        return AjaxResult.success();
    }

    /**
     * 更新图构建进度
     */
    @PostMapping("/graph/{documentId}")
    @Operation(summary = "更新图构建进度")
    public AjaxResult<?> updateGraphProgress(
            @PathVariable @Parameter(description = "文档ID") String documentId,
            @RequestBody Map<String, Object> request) {
        
        String status = (String) request.get("status");
        Integer progress = (Integer) request.get("progress");
        
        taskProgressService.updateGraphProgress(documentId, status, progress);
        
        return AjaxResult.success();
    }

    /**
     * 标记任务失败
     */
    @PostMapping("/fail/{documentId}")
    @Operation(summary = "标记任务失败")
    public AjaxResult<?> markFailed(
            @PathVariable @Parameter(description = "文档ID") String documentId,
            @RequestBody Map<String, Object> request) {
        
        String stage = (String) request.get("stage");
        String errorMessage = (String) request.get("errorMessage");
        
        taskProgressService.markFailed(documentId, stage, errorMessage);
        
        return AjaxResult.success();
    }
}
