package com.example.demo.test;

import java.util.List;

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 com.example.demo.model.TaskQueue;
import com.example.demo.service.TaskQueueService;

/**
 * 任务队列功能测试
 */
@SpringBootTest
@ActiveProfiles("test")
public class TaskQueueTest {

    @Autowired
    private TaskQueueService taskQueueService;

    @Test
    public void testAddTaskToQueue() {
        // 测试添加任务到队列
        String username = "testuser";
        String taskId = "test_task_001";
        
        try {
            TaskQueue taskQueue = taskQueueService.addTextToVideoTask(username, taskId);
            assert taskQueue != null;
            assert taskQueue.getUsername().equals(username);
            assert taskQueue.getTaskId().equals(taskId);
            assert taskQueue.getTaskType() == TaskQueue.TaskType.TEXT_TO_VIDEO;
            assert taskQueue.getStatus() == TaskQueue.QueueStatus.PENDING;
            
            System.out.println("✅ 添加任务到队列测试通过");
        } catch (Exception e) {
            System.out.println("❌ 添加任务到队列测试失败: " + e.getMessage());
        }
    }

    @Test
    public void testMaxTasksLimit() {
        // 测试用户任务数量限制
        String username = "testuser2";
        
        try {
            // 添加3个任务（达到限制）
            for (int i = 1; i <= 3; i++) {
                String taskId = "test_task_" + String.format("%03d", i);
                taskQueueService.addTextToVideoTask(username, taskId);
            }
            
            // 尝试添加第4个任务，应该失败
            try {
                taskQueueService.addTextToVideoTask(username, "test_task_004");
                System.out.println("❌ 任务数量限制测试失败：应该抛出异常");
            } catch (RuntimeException e) {
                if (e.getMessage().contains("队列已满")) {
                    System.out.println("✅ 任务数量限制测试通过");
                } else {
                    System.out.println("❌ 任务数量限制测试失败：异常消息不正确");
                }
            }
        } catch (Exception e) {
            System.out.println("❌ 任务数量限制测试失败: " + e.getMessage());
        }
    }

    @Test
    public void testGetUserTaskQueue() {
        // 测试获取用户任务队列
        String username = "testuser3";
        
        try {
            // 添加一个任务
            taskQueueService.addTextToVideoTask(username, "test_task_005");
            
            // 获取用户任务队列
            List<TaskQueue> taskQueue = taskQueueService.getUserTaskQueue(username);
            assert taskQueue != null;
            assert taskQueue.size() >= 1;
            
            System.out.println("✅ 获取用户任务队列测试通过");
        } catch (Exception e) {
            System.out.println("❌ 获取用户任务队列测试失败: " + e.getMessage());
        }
    }

    @Test
    public void testCancelTask() {
        // 测试取消任务
        String username = "testuser4";
        String taskId = "test_task_006";
        
        try {
            // 添加任务
            taskQueueService.addTextToVideoTask(username, taskId);
            
            // 取消任务
            boolean cancelled = taskQueueService.cancelTask(taskId, username);
            assert cancelled;
            
            System.out.println("✅ 取消任务测试通过");
        } catch (Exception e) {
            System.out.println("❌ 取消任务测试失败: " + e.getMessage());
        }
    }

    @Test
    public void testTaskQueueStats() {
        // 测试任务队列统计
        String username = "testuser5";
        
        try {
            // 添加几个任务
            taskQueueService.addTextToVideoTask(username, "test_task_007");
            taskQueueService.addImageToVideoTask(username, "test_task_008");
            
            // 获取统计信息
            long totalCount = taskQueueService.getUserTaskCount(username);
            assert totalCount >= 2;
            
            System.out.println("✅ 任务队列统计测试通过");
        } catch (Exception e) {
            System.out.println("❌ 任务队列统计测试失败: " + e.getMessage());
        }
    }
}


