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;

import java.time.LocalDateTime;
import java.util.UUID;

/**
 * 图像检测边界框字段测试
 */
@SpringBootTest
@ActiveProfiles("test")
public class FaceDetectBboxTest {

    @Autowired
    private FaceDetectLogServiceImpl faceDetectLogService;

    /**
     * 测试设置图像区域坐标
     */
    @Test
    public void testSetFaceBbox() {
        System.out.println("=== 测试设置图像区域坐标 ===");
        
        // 创建测试数据
        FaceDetectLog faceDetectLog = new FaceDetectLog();
        faceDetectLog.setId(UUID.randomUUID().toString());
        faceDetectLog.setRequestId(UUID.randomUUID().toString());
        faceDetectLog.setModel("liveportrait-detect");
        faceDetectLog.setImageUrl("https://example.com/test-image.jpg");
        faceDetectLog.setOwnerName("张三");
        faceDetectLog.setOwnerPhone("13800138000");
        faceDetectLog.setAvatarName("测试头像");
        faceDetectLog.setRequestTime(LocalDateTime.now());
        faceDetectLog.setResponseTime(LocalDateTime.now());
        faceDetectLog.setStatusCode(200);
        faceDetectLog.setSuccess(true);
        faceDetectLog.setFaceCount(1);
        
        // 设置图像区域坐标 [x1, y1, x2, y2]
        String faceBbox = "[302,286,610,593]";
        faceDetectLog.setFaceBbox(faceBbox);
        
        // 设置动态区域坐标 [x1, y1, x2, y2]
        String extBbox = "[71,9,840,778]";
        faceDetectLog.setExtBbox(extBbox);
        
        // 保存到数据库
        boolean saved = faceDetectLogService.saveFaceDetectLog(faceDetectLog);
        
        if (saved) {
            System.out.println("✅ 图像检测日志保存成功");
            System.out.println("图像区域坐标: " + faceDetectLog.getFaceBbox());
            System.out.println("动态区域坐标: " + faceDetectLog.getExtBbox());
        } else {
            System.out.println("❌ 图像检测日志保存失败");
        }
    }

    /**
     * 测试解析JSON格式的坐标数据
     */
    @Test
    public void testParseBboxFromJson() {
        System.out.println("=== 测试解析JSON格式的坐标数据 ===");
        
        // 模拟API响应中的坐标数据
        String faceBboxJson = "[302,286,610,593]";
        String extBboxJson = "[71,9,840,778]";
        
        FaceDetectLog faceDetectLog = new FaceDetectLog();
        faceDetectLog.setId(UUID.randomUUID().toString());
        faceDetectLog.setRequestId(UUID.randomUUID().toString());
        faceDetectLog.setModel("liveportrait-detect");
        faceDetectLog.setImageUrl("https://example.com/test-image.jpg");
        faceDetectLog.setRequestTime(LocalDateTime.now());
        faceDetectLog.setResponseTime(LocalDateTime.now());
        faceDetectLog.setStatusCode(200);
        faceDetectLog.setSuccess(true);
        faceDetectLog.setFaceCount(1);
        
        // 设置坐标数据
        faceDetectLog.setFaceBbox(faceBboxJson);
        faceDetectLog.setExtBbox(extBboxJson);
        
        // 验证坐标数据
        System.out.println("图像区域坐标: " + faceDetectLog.getFaceBbox());
        System.out.println("动态区域坐标: " + faceDetectLog.getExtBbox());
        
        // 验证坐标格式
        if (faceDetectLog.getFaceBbox() != null && faceDetectLog.getFaceBbox().startsWith("[") && faceDetectLog.getFaceBbox().endsWith("]")) {
            System.out.println("✅ 图像区域坐标格式正确");
        } else {
            System.out.println("❌ 图像区域坐标格式错误");
        }
        
        if (faceDetectLog.getExtBbox() != null && faceDetectLog.getExtBbox().startsWith("[") && faceDetectLog.getExtBbox().endsWith("]")) {
            System.out.println("✅ 动态区域坐标格式正确");
        } else {
            System.out.println("❌ 动态区域坐标格式错误");
        }
    }

    /**
     * 测试坐标数据的边界情况
     */
    @Test
    public void testBboxBoundaryCases() {
        System.out.println("=== 测试坐标数据的边界情况 ===");
        
        FaceDetectLog faceDetectLog = new FaceDetectLog();
        faceDetectLog.setId(UUID.randomUUID().toString());
        faceDetectLog.setRequestId(UUID.randomUUID().toString());
        faceDetectLog.setModel("liveportrait-detect");
        faceDetectLog.setImageUrl("https://example.com/test-image.jpg");
        faceDetectLog.setRequestTime(LocalDateTime.now());
        faceDetectLog.setResponseTime(LocalDateTime.now());
        faceDetectLog.setStatusCode(200);
        faceDetectLog.setSuccess(true);
        faceDetectLog.setFaceCount(0);
        
        // 测试空坐标
        faceDetectLog.setFaceBbox(null);
        faceDetectLog.setExtBbox(null);
        System.out.println("空坐标测试: faceBbox=" + faceDetectLog.getFaceBbox() + ", extBbox=" + faceDetectLog.getExtBbox());
        
        // 测试空字符串
        faceDetectLog.setFaceBbox("");
        faceDetectLog.setExtBbox("");
        System.out.println("空字符串测试: faceBbox='" + faceDetectLog.getFaceBbox() + "', extBbox='" + faceDetectLog.getExtBbox() + "'");
        
        // 测试有效坐标
        faceDetectLog.setFaceBbox("[0,0,100,100]");
        faceDetectLog.setExtBbox("[0,0,200,200]");
        System.out.println("有效坐标测试: faceBbox=" + faceDetectLog.getFaceBbox() + ", extBbox=" + faceDetectLog.getExtBbox());
        
        System.out.println("✅ 坐标数据边界情况测试完成");
    }
}
