package com.example.demo.test;

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

/**
 * 积分冻结功能测试
 */
@SpringBootTest
@ActiveProfiles("test")
public class PointsFreezeTest {

    @Autowired
    private UserService userService;

    @Autowired
    private TaskQueueService taskQueueService;

    @Test
    public void testFreezePoints() {
        // 测试冻结积分
        String username = "testuser";
        String taskId = "test_task_001";
        
        try {
            // 冻结积分
            PointsFreezeRecord record = userService.freezePoints(username, taskId, 
                PointsFreezeRecord.TaskType.TEXT_TO_VIDEO, 80, "测试冻结积分");
            
            assert record != null;
            assert record.getUsername().equals(username);
            assert record.getTaskId().equals(taskId);
            assert record.getFreezePoints() == 80;
            assert record.getStatus() == PointsFreezeRecord.FreezeStatus.FROZEN;
            
            System.out.println("✅ 冻结积分测试通过");
        } catch (Exception e) {
            System.out.println("❌ 冻结积分测试失败: " + e.getMessage());
        }
    }

    @Test
    public void testDeductFrozenPoints() {
        // 测试扣除冻结积分
        String username = "testuser2";
        String taskId = "test_task_002";
        
        try {
            // 先冻结积分
            userService.freezePoints(username, taskId, 
                PointsFreezeRecord.TaskType.TEXT_TO_VIDEO, 80, "测试扣除积分");
            
            // 扣除冻结积分
            userService.deductFrozenPoints(taskId);
            
            System.out.println("✅ 扣除冻结积分测试通过");
        } catch (Exception e) {
            System.out.println("❌ 扣除冻结积分测试失败: " + e.getMessage());
        }
    }

    @Test
    public void testReturnFrozenPoints() {
        // 测试返还冻结积分
        String username = "testuser3";
        String taskId = "test_task_003";
        
        try {
            // 先冻结积分
            userService.freezePoints(username, taskId, 
                PointsFreezeRecord.TaskType.IMAGE_TO_VIDEO, 90, "测试返还积分");
            
            // 返还冻结积分
            userService.returnFrozenPoints(taskId);
            
            System.out.println("✅ 返还冻结积分测试通过");
        } catch (Exception e) {
            System.out.println("❌ 返还冻结积分测试失败: " + e.getMessage());
        }
    }

    @Test
    public void testTaskQueueWithPointsFreeze() {
        // 测试任务队列与积分冻结集成
        String username = "testuser4";
        String taskId = "test_task_004";
        
        try {
            // 添加任务到队列（会自动冻结积分）
            taskQueueService.addTextToVideoTask(username, taskId);
            
            System.out.println("✅ 任务队列积分冻结集成测试通过");
        } catch (Exception e) {
            System.out.println("❌ 任务队列积分冻结集成测试失败: " + e.getMessage());
        }
    }

    @Test
    public void testAvailablePointsCalculation() {
        // 测试可用积分计算
        String username = "testuser5";
        
        try {
            Integer availablePoints = userService.getAvailablePoints(username);
            Integer frozenPoints = userService.getFrozenPoints(username);
            
            assert availablePoints != null;
            assert frozenPoints != null;
            
            System.out.println("✅ 可用积分计算测试通过");
            System.out.println("   可用积分: " + availablePoints);
            System.out.println("   冻结积分: " + frozenPoints);
        } catch (Exception e) {
            System.out.println("❌ 可用积分计算测试失败: " + e.getMessage());
        }
    }
}


