package com.rj.scheduler;

import com.rj.entity.VideoSynthesisLog;
import com.rj.mapper.VideoSynthesisLogMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 视频合成状态检查定时器测试类
 * 
 * @author rj
 * @date 2025-01-30
 */
@SpringBootTest
@ActiveProfiles("test")
public class VideoSynthesisStatusSchedulerTest {

    @Autowired
    private VideoSynthesisStatusScheduler videoSynthesisStatusScheduler;

    @Autowired
    private VideoSynthesisLogMapper videoSynthesisLogMapper;

    /**
     * 测试查询PENDING状态的任务
     */
    @Test
    public void testGetPendingTasks() {
        // 这里可以添加一些测试数据，然后验证查询功能
        System.out.println("测试查询PENDING状态的任务...");
        
        // 手动执行定时器方法进行测试
        videoSynthesisStatusScheduler.checkVideoSynthesisStatus();
    }

    /**
     * 测试手动创建PENDING状态的任务（用于测试）
     */
    @Test
    public void testCreatePendingTask() {
        VideoSynthesisLog testTask = new VideoSynthesisLog();
        testTask.setRequestId("test-request-" + System.currentTimeMillis());
        testTask.setTaskId("test-task-" + System.currentTimeMillis());
        testTask.setModel("test-model");
        testTask.setImageUrl("http://test.com/image.jpg");
        testTask.setAudioUrl("http://test.com/audio.mp3");
        testTask.setTaskStatus("PENDING");
        testTask.setRequestTime(LocalDateTime.now());
        testTask.setCreatedAt(LocalDateTime.now());
        testTask.setUpdatedAt(LocalDateTime.now());
        
        int result = videoSynthesisLogMapper.insert(testTask);
        System.out.println("创建测试任务结果: " + (result > 0 ? "成功" : "失败"));
    }

    /**
     * 测试查询所有PENDING状态的任务
     */
    @Test
    public void testQueryPendingTasks() {
        List<VideoSynthesisLog> pendingTasks = videoSynthesisLogMapper.selectList(
            new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<VideoSynthesisLog>()
                .eq("task_status", "PENDING")
        );
        
        System.out.println("找到PENDING状态的任务数量: " + pendingTasks.size());
        for (VideoSynthesisLog task : pendingTasks) {
            System.out.println("任务ID: " + task.getTaskId() + ", 状态: " + task.getTaskStatus());
        }
    }

    /**
     * 测试视频转存功能（模拟API响应）
     */
    @Test
    public void testVideoTransfer() {
        // 模拟API响应数据
        String mockResponse = "{\n" +
            "  \"request_id\":\"f2c33c8b-4bd8-46d5-b035-7a1cdeaaedc1\",\n" +
            "  \"output\":{\n" +
            "    \"task_id\":\"3c692370-6572-4aba-b03a-534cb9866ef3\",\n" +
            "    \"task_status\":\"SUCCEEDED\",\n" +
            "    \"submit_time\":\"2025-10-07 20:31:19.337\",\n" +
            "    \"scheduled_time\":\"2025-10-07 20:31:27.368\",\n" +
            "    \"end_time\":\"2025-10-07 20:32:00.900\",\n" +
            "    \"results\":{\n" +
            "      \"video_url\":\"http://dashscope-result-sh.oss-cn-shanghai.aliyuncs.com/1d/3c/20251007/5cf64d65/3c692370-6572-4aba-b03a-534cb9866ef3.mp4?Expires=1759926720&OSSAccessKeyId=LTAI5tKPD3TMqf2Lna1fASuh&Signature=s2WX6hQ%2BLBDWWmW%2F3sbxEKf%2BUu0%3D\"\n" +
            "    }\n" +
            "  },\n" +
            "  \"usage\":{\n" +
            "    \"video_duration\":10.86,\n" +
            "    \"video_ratio\":\"standard\"\n" +
            "  }\n" +
            "}";
        
        System.out.println("模拟API响应测试:");
        System.out.println(mockResponse);
        
        // 这里可以添加更多测试逻辑
        System.out.println("测试完成");
    }
}
