package com.rj.service;

import com.rj.entity.FaceDetectLog;
import com.rj.service.impl.FaceDetectLogServiceImpl;
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 FaceDetectImageCountTest {

    @Autowired
    private FaceDetectLogServiceImpl faceDetectLogService;

    /**
     * 测试设置图片数量
     */
    @Test
    public void testSetImageCount() {
        System.out.println("=== 测试设置图片数量 ===");
        
        // 创建测试数据
        FaceDetectLog faceDetectLog = new FaceDetectLog();
        faceDetectLog.setId(java.util.UUID.randomUUID().toString());
        faceDetectLog.setRequestId(java.util.UUID.randomUUID().toString());
        faceDetectLog.setModel("liveportrait-detect");
        faceDetectLog.setImageUrl("https://example.com/test-image.jpg");
        faceDetectLog.setOwnerName("张三");
        faceDetectLog.setOwnerPhone("13800138000");
        faceDetectLog.setAvatarName("测试头像");
        faceDetectLog.setRequestTime(java.time.LocalDateTime.now());
        faceDetectLog.setResponseTime(java.time.LocalDateTime.now());
        faceDetectLog.setStatusCode(200);
        faceDetectLog.setSuccess(true);
        faceDetectLog.setFaceCount(1);
        
        // 设置图片数量
        Integer imageCount = 1;
        
        // 保存到数据库
        boolean saved = faceDetectLogService.saveFaceDetectLog(faceDetectLog);
        
        if (saved) {
            System.out.println("✅ 图像检测日志保存成功");
        } else {
            System.out.println("❌ 图像检测日志保存失败");
        }
    }

    /**
     * 测试解析JSON中的image_count
     */
    @Test
    public void testParseImageCountFromJson() {
        System.out.println("=== 测试解析JSON中的image_count ===");
        
        // 模拟API响应JSON
        String jsonResponse = """
            {
                "output": {
                    "message": "",
                    "pass": true
                },
                "usage": {
                    "image_count": 1
                },
                "request_id": "a4529f04-2fbb-4818-8e6c-3627945c95fc"
            }
            """;
        
        System.out.println("模拟API响应: " + jsonResponse);
        
        // 验证JSON解析逻辑
        try {
            com.fasterxml.jackson.databind.ObjectMapper objectMapper = new com.fasterxml.jackson.databind.ObjectMapper();
            com.fasterxml.jackson.databind.JsonNode jsonNode = objectMapper.readTree(jsonResponse);
            
            if (jsonNode.has("usage")) {
                com.fasterxml.jackson.databind.JsonNode usage = jsonNode.get("usage");
                if (usage.has("image_count")) {
                    int imageCount = usage.get("image_count").asInt();
                    System.out.println("✅ 成功解析image_count: " + imageCount);
                    
                    // 验证值是否正确
                    if (imageCount == 1) {
                        System.out.println("✅ image_count值正确");
                    } else {
                        System.out.println("❌ image_count值错误: " + imageCount);
                    }
                } else {
                    System.out.println("❌ usage中没有image_count字段");
                }
            } else {
                System.out.println("❌ JSON中没有usage字段");
            }
            
        } catch (Exception e) {
            System.out.println("❌ JSON解析异常: " + e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 测试不同image_count值
     */
    @Test
    public void testDifferentImageCountValues() {
        System.out.println("=== 测试不同image_count值 ===");
        
        Integer[] testValues = {1, 2, 3, 0, null};
        
        for (Integer imageCount : testValues) {
            System.out.println("测试image_count值: " + imageCount);
            
            FaceDetectLog faceDetectLog = new FaceDetectLog();
            faceDetectLog.setId(java.util.UUID.randomUUID().toString());
            faceDetectLog.setRequestId(java.util.UUID.randomUUID().toString());
            faceDetectLog.setModel("liveportrait-detect");
            faceDetectLog.setImageUrl("https://example.com/test-image.jpg");
            faceDetectLog.setRequestTime(java.time.LocalDateTime.now());
            faceDetectLog.setResponseTime(java.time.LocalDateTime.now());
            faceDetectLog.setStatusCode(200);
            faceDetectLog.setSuccess(true);
            faceDetectLog.setFaceCount(1);


            System.out.println("---");
        }
    }

    /**
     * 测试完整的API响应解析流程
     */
    @Test
    public void testCompleteApiResponseParsing() {
        System.out.println("=== 测试完整的API响应解析流程 ===");
        
        // 模拟完整的API响应
        String jsonResponse = """
            {
                "output": {
                    "message": "检测成功",
                    "pass": true
                },
                "usage": {
                    "image_count": 1
                },
                "request_id": "a4529f04-2fbb-4818-8e6c-3627945c95fc"
            }
            """;
        
        System.out.println("完整API响应: " + jsonResponse);
        
        try {
            com.fasterxml.jackson.databind.ObjectMapper objectMapper = new com.fasterxml.jackson.databind.ObjectMapper();
            com.fasterxml.jackson.databind.JsonNode jsonNode = objectMapper.readTree(jsonResponse);
            
            // 模拟解析逻辑
            FaceDetectLog faceDetectLog = new FaceDetectLog();
            
            // 解析output
            if (jsonNode.has("output")) {
                com.fasterxml.jackson.databind.JsonNode output = jsonNode.get("output");
                
                if (output.has("pass")) {
                    boolean pass = output.get("pass").asBoolean();
                    int faceCount = pass ? 1 : 0;
                    faceDetectLog.setFaceCount(faceCount);
                    System.out.println("✅ 解析pass字段: " + pass + ", 图像数量: " + faceCount);
                }
                
                if (output.has("message")) {
                    String message = output.get("message").asText();
                    System.out.println("✅ 解析message字段: " + message);
                }
            }
            
            // 解析usage
            if (jsonNode.has("usage")) {
                com.fasterxml.jackson.databind.JsonNode usage = jsonNode.get("usage");
                
                if (usage.has("image_count")) {
                    int imageCount = usage.get("image_count").asInt();
                    System.out.println("✅ 解析image_count字段: " + imageCount);
                }
            }
            
            // 解析request_id
            if (jsonNode.has("request_id")) {
                String requestId = jsonNode.get("request_id").asText();
                System.out.println("✅ 解析request_id字段: " + requestId);
            }
            
            System.out.println("✅ 完整API响应解析成功");
            System.out.println("最终结果:");
            System.out.println("- 图像数量: " + faceDetectLog.getFaceCount());
            
        } catch (Exception e) {
            System.out.println("❌ 解析异常: " + e.getMessage());
            e.printStackTrace();
        }
    }
}






















































