package com.rj.controller;

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.mock.web.MockMultipartFile;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.web.multipart.MultipartFile;

/**
 * 图像检测控制器模型参数测试
 */
@SpringBootTest
@ActiveProfiles("test")
public class FaceDetectControllerModelTest {

    @Autowired
    private IFaceDetectLogService faceDetectLogService;

    /**
     * 测试带模型参数的图像检测
     */
    @Test
    public void testDetectFaceWithModel() {
        System.out.println("=== 测试带模型参数的图像检测 ===");
        
        String imageUrl = "https://example.com/test-image.jpg";
        String ownerName = "张三";
        String ownerPhone = "13800138000";
        String avatarName = "测试头像";
        String model = "liveportrait-detect-v2";
        
        try {
            // 调用图像检测服务
            com.rj.entity.FaceDetectLog detectLog = faceDetectLogService.detectFaceAndSave(imageUrl, ownerName, ownerPhone, avatarName, model);
            
            System.out.println("图像检测结果:");
            System.out.println("- 请求ID: " + detectLog.getRequestId());
            System.out.println("- 模型: " + detectLog.getModel());
            System.out.println("- 图片URL: " + detectLog.getImageUrl());
            System.out.println("- 所属人: " + detectLog.getOwnerName());
            System.out.println("- 电话: " + detectLog.getOwnerPhone());
            System.out.println("- 头像名称: " + detectLog.getAvatarName());
            System.out.println("- 是否成功: " + detectLog.getSuccess());
            System.out.println("- 图像数量: " + detectLog.getFaceCount());
            
            if (detectLog.getSuccess()) {
                System.out.println("✅ 图像检测成功");
            } else {
                System.out.println("❌ 图像检测失败: " + detectLog.getErrorMessage());
            }
            
        } catch (Exception e) {
            System.out.println("❌ 测试异常: " + e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 测试默认模型的图像检测
     */
    @Test
    public void testDetectFaceWithDefaultModel() {
        System.out.println("=== 测试默认模型的图像检测 ===");
        
        String imageUrl = "https://example.com/test-image.jpg";
        String ownerName = "李四";
        String ownerPhone = "13900139000";
        String avatarName = "默认头像";
        String model = null; // 使用默认模型
        
        try {
            // 调用图像检测服务
            com.rj.entity.FaceDetectLog detectLog = faceDetectLogService.detectFaceAndSave(imageUrl, ownerName, ownerPhone, avatarName, model);
            
            System.out.println("图像检测结果:");
            System.out.println("- 请求ID: " + detectLog.getRequestId());
            System.out.println("- 模型: " + detectLog.getModel());
            System.out.println("- 图片URL: " + detectLog.getImageUrl());
            System.out.println("- 所属人: " + detectLog.getOwnerName());
            System.out.println("- 电话: " + detectLog.getOwnerPhone());
            System.out.println("- 头像名称: " + detectLog.getAvatarName());
            System.out.println("- 是否成功: " + detectLog.getSuccess());
            System.out.println("- 图像数量: " + detectLog.getFaceCount());
            
            // 验证默认模型
            if ("liveportrait-detect".equals(detectLog.getModel())) {
                System.out.println("✅ 默认模型设置正确");
            } else {
                System.out.println("❌ 默认模型设置错误: " + detectLog.getModel());
            }
            
        } catch (Exception e) {
            System.out.println("❌ 测试异常: " + e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 测试模型参数验证
     */
    @Test
    public void testModelParameterValidation() {
        System.out.println("=== 测试模型参数验证 ===");
        
        String imageUrl = "https://example.com/test-image.jpg";
        String ownerName = "王五";
        String ownerPhone = "13700137000";
        String avatarName = "验证头像";
        
        // 测试不同的模型参数
        String[] testModels = {
            "liveportrait-detect",
            "liveportrait-detect-v2",
            "custom-model",
            "",
            null
        };
        
        for (String model : testModels) {
            System.out.println("测试模型: " + (model == null ? "null" : "'" + model + "'"));
            
            try {
                com.rj.entity.FaceDetectLog detectLog = faceDetectLogService.detectFaceAndSave(imageUrl, ownerName, ownerPhone, avatarName, model);
                
                String actualModel = detectLog.getModel();
                System.out.println("实际使用的模型: " + actualModel);
                
                // 验证模型设置逻辑
                if (model == null || model.trim().isEmpty()) {
                    if ("liveportrait-detect".equals(actualModel)) {
                        System.out.println("✅ 空模型参数使用默认值正确");
                    } else {
                        System.out.println("❌ 空模型参数使用默认值错误");
                    }
                } else {
                    if (model.equals(actualModel)) {
                        System.out.println("✅ 自定义模型设置正确");
                    } else {
                        System.out.println("❌ 自定义模型设置错误");
                    }
                }
                
            } catch (Exception e) {
                System.out.println("❌ 测试异常: " + e.getMessage());
            }
            
            System.out.println("---");
        }
    }

    /**
     * 测试控制器接口参数
     */
    @Test
    public void testControllerParameters() {
        System.out.println("=== 测试控制器接口参数 ===");
        
        // 创建模拟文件
        byte[] fileContent = "test image content".getBytes();
        MultipartFile mockFile = new MockMultipartFile(
            "file", 
            "test-image.jpg", 
            "image/jpeg", 
            fileContent
        );
        
        String userId = "user123";
        String ownerName = "赵六";
        String ownerPhone = "13600136000";
        String avatarName = "控制器测试头像";
        String model = "liveportrait-detect-v3";
        int expiresInSeconds = 3600;
        
        System.out.println("测试参数:");
        System.out.println("- 文件名: " + mockFile.getOriginalFilename());
        System.out.println("- 文件大小: " + mockFile.getSize() + " bytes");
        System.out.println("- 用户ID: " + userId);
        System.out.println("- 所属人: " + ownerName);
        System.out.println("- 电话: " + ownerPhone);
        System.out.println("- 头像名称: " + avatarName);
        System.out.println("- 模型: " + model);
        System.out.println("- 有效期: " + expiresInSeconds + " 秒");
        
        try {
            // 这里只是验证参数传递，实际调用需要完整的Spring环境
            System.out.println("✅ 控制器参数设置正确");
            System.out.println("注意: 实际测试需要完整的Spring Boot环境和MinIO配置");
            
        } catch (Exception e) {
            System.out.println("❌ 测试异常: " + e.getMessage());
            e.printStackTrace();
        }
    }
}
