package shop.alien.second.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.SecondVideoTask;
import shop.alien.second.service.VideoModerationService;

/**
 * 视频审核控制器示例
 */
@Slf4j
@RestController
@RequestMapping("/video/moderation")
@RequiredArgsConstructor
public class VideoModerationController {
    
    private final VideoModerationService videoModerationService;
    
    /**
     * 提交视频审核任务
     * 
     * @param videoUrl 视频URL
     * @return 任务ID
     */
    @PostMapping("/submit")
    public String submitVideoModerationTask(@RequestParam String videoUrl) {
        try {
            String taskId = videoModerationService.submitVideoModerationTask(videoUrl);
            return "任务提交成功，任务ID: " + taskId;
        } catch (Exception e) {
            log.error("提交视频审核任务失败", e);
            return "任务提交失败: " + e.getMessage();
        }
    }
    
    /**
     * 查询视频审核结果
     * 
     * @param taskId 任务ID
     * @return 审核结果
     */
    @GetMapping("/result")
    public Object getVideoModerationResult(@RequestParam String taskId) {
        try {
            return videoModerationService.queryVideoModerationResult(taskId);
        } catch (Exception e) {
            log.error("查询视频审核结果失败", e);
            return "查询失败: " + e.getMessage();
        }
    }


    /**
     * 处理单个视频审核任务
     *
     * @param taskId 任务ID
     * @return 处理结果
     */
    @GetMapping("/processTask")
    public boolean processTask(@RequestParam("taskId") String taskId) {
        try {
            // 根据任务ID查询任务详情
            SecondVideoTask task = videoModerationService.getTaskByTaskId(taskId);
            if (task == null) {
                log.error("未找到任务ID为 {} 的视频审核任务", taskId);
                return false;
            }

            // 处理任务
            return videoModerationService.processTask(task);
        } catch (Exception e) {
            log.error("处理视频审核任务时发生异常，任务ID: {}", taskId, e);
            return false;
        }
    }
}