package com.rj.controller;

import com.rj.entity.FaceDetectLog;
import com.rj.service.IFaceDetectLogService;
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 org.springframework.transaction.annotation.Transactional;

import static org.junit.jupiter.api.Assertions.*;

/**
 * 图像检测控制器集成测试
 */
@SpringBootTest
@ActiveProfiles("test")
@Transactional
public class FaceDetectControllerIntegrationTest {
    
    
    @Autowired
    private IFaceDetectLogService faceDetectLogService;
    
    @Test
    public void testDetectFaceWithValidUrl() {
        // 使用测试图片URL
        String testImageUrl = "http://101.35.52.237:19006/api/v1/download-shared-object/aHR0cDovLzEyNy4wLjAuMTo5MDA1L3NyYy1iYXNlLWF1ZGlvLXZpZGVvLyVFNSVCRSVBRSVFNCVCRiVBMSVFNSU5QiVCRSVFNyU4OSU4N18yMDI1MTAwNTEyMDIwNV81Nl80OS5qcGc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD0wRUE4RlhHQUlTN1FQSk9VQUZIOSUyRjIwMjUxMDA1JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MTAwNVQwNDE3NDVaJlgtQW16LUV4cGlyZXM9NDMyMDAmWC1BbXotU2VjdXJpdHktVG9rZW49ZXlKaGJHY2lPaUpJVXpVeE1pSXNJblI1Y0NJNklrcFhWQ0o5LmV5SmhZMk5sYzNOTFpYa2lPaUl3UlVFNFJsaEhRVWxUTjFGUVNrOVZRVVpJT1NJc0ltVjRjQ0k2TVRjMU9UWTNPVGN4TkN3aWNHRnlaVzUwSWpvaWJXbHVhVzloWkcxcGJpSjkuLWhSOFRobUpXUFNQdGVQM2V4TzdITmlkUHQ1RXBDeWRJMVV2cG0yQXJxczJsR0hBcnBTeEZWZEdJY1ZOUHdKNlF2QUZjbUJQOWQtQmtFampyUXpGaGcmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JnZlcnNpb25JZD1udWxsJlgtQW16LVNpZ25hdHVyZT1mN2NjODUxNWFiYzgxZDFlNDZmNjg1YzBkMzAxMzM0MjAyZjM5NmIxODMzMzY4MGM0OWE3ZGYxYmQ5ODk5ZWM4";
        
        try {
            // 执行图像检测
            FaceDetectLog result = faceDetectLogService.detectFaceAndSave(testImageUrl);
            
            // 验证结果
            assertNotNull(result);
            assertNotNull(result.getRequestId());
            assertEquals("liveportrait-detect", result.getModel());
            assertEquals(testImageUrl, result.getImageUrl());
            assertNotNull(result.getRequestTime());
            
            System.out.println("图像检测测试结果:");
            System.out.println("请求ID: " + result.getRequestId());
            System.out.println("成功状态: " + result.getSuccess());
            System.out.println("图像数量: " + result.getFaceCount());
            System.out.println("处理时间: " + result.getProcessingTimeMs() + "ms");
            System.out.println("HTTP状态码: " + result.getStatusCode());
            
            if (!result.getSuccess()) {
                System.out.println("错误信息: " + result.getErrorMessage());
            }
            
            // 验证响应数据包含pass字段
            if (result.getResponseData() != null) {
                System.out.println("响应数据: " + result.getResponseData());
            }
            
        } catch (Exception e) {
            System.err.println("图像检测测试失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
    
    @Test
    public void testDetectFaceWithInvalidUrl() {
        // 使用无效的图片URL
        String invalidImageUrl = "https://invalid-url.com/nonexistent.jpg";
        
        try {
            // 执行图像检测
            FaceDetectLog result = faceDetectLogService.detectFaceAndSave(invalidImageUrl);
            
            // 验证结果
            assertNotNull(result);
            assertNotNull(result.getRequestId());
            assertEquals("liveportrait-detect", result.getModel());
            assertEquals(invalidImageUrl, result.getImageUrl());
            
            System.out.println("无效URL测试结果:");
            System.out.println("请求ID: " + result.getRequestId());
            System.out.println("成功状态: " + result.getSuccess());
            System.out.println("错误信息: " + result.getErrorMessage());
            
        } catch (Exception e) {
            System.err.println("无效URL测试失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
    
    @Test
    public void testPassFieldLogic() {
        // 测试pass字段逻辑
        System.out.println("=== 测试pass字段逻辑 ===");
        
        // 模拟响应数据
        String mockResponseData = "{\"output\":{\"message\":\"\",\"pass\":true},\"usage\":{\"image_count\":1},\"request_id\":\"ee4a54ca-c190-4594-990e-d9546cbbd5ed\"}";
        
        try {
            // 创建测试日志
            FaceDetectLog testLog = new FaceDetectLog();
            testLog.setRequestId("test-pass-logic-" + System.currentTimeMillis());
            testLog.setModel("liveportrait-detect");
            testLog.setImageUrl("https://example.com/test.jpg");
            testLog.setRequestTime(java.time.LocalDateTime.now());
            testLog.setResponseTime(java.time.LocalDateTime.now());
            testLog.setStatusCode(200);
            testLog.setResponseData(mockResponseData);
            testLog.setProcessingTimeMs(1500L);
            
            // 解析响应数据中的pass字段
            com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper();
            com.fasterxml.jackson.databind.JsonNode jsonResponse = mapper.readTree(mockResponseData);
            
            boolean isSuccess = false;
            if (jsonResponse.has("output") && jsonResponse.get("output").has("pass")) {
                isSuccess = jsonResponse.get("output").get("pass").asBoolean();
                testLog.setSuccess(isSuccess);
                
                System.out.println("解析到的pass值: " + isSuccess);
                System.out.println("设置的成功状态: " + testLog.getSuccess());
            }
            
            // 验证pass字段解析
            assertTrue(isSuccess, "pass字段应该为true");
            assertTrue(testLog.getSuccess(), "成功状态应该为true");
            
            System.out.println("pass字段逻辑测试通过");
            
        } catch (Exception e) {
            System.err.println("pass字段逻辑测试失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
    
    @Test
    public void testSaveFaceDetectLog() {
        // 创建测试日志
        FaceDetectLog testLog = new FaceDetectLog();
        testLog.setRequestId("test-request-id-" + System.currentTimeMillis());
        testLog.setModel("liveportrait-detect");
        testLog.setImageUrl("https://example.com/test.jpg");
        testLog.setRequestTime(java.time.LocalDateTime.now());
        testLog.setResponseTime(java.time.LocalDateTime.now());
        testLog.setStatusCode(200);
        testLog.setSuccess(true);
        testLog.setFaceCount(1);
        testLog.setResponseData("{\"output\":{\"faces\":[{\"bbox\":[100,100,200,200]}]}}");
        testLog.setProcessingTimeMs(1500L);
        
        // 保存日志
        boolean saved = faceDetectLogService.saveFaceDetectLog(testLog);
        
        // 验证保存结果
        assertTrue(saved);
        System.out.println("日志保存测试成功，请求ID: " + testLog.getRequestId());
    }
}
