package com.gxwebsoft.house;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gxwebsoft.house.entity.HouseInfo;
import com.gxwebsoft.house.service.HouseInfoService;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 房产海报生成测试
 */
@SpringBootTest
@ActiveProfiles("dev")
public class HousePosterTest {

    @Resource
    private HouseInfoService houseInfoService;

    @Test
    public void testGeneratePoster() throws Exception {
        // 创建测试房源信息
        HouseInfo houseInfo = new HouseInfo();
        houseInfo.setHouseId(1);
        houseInfo.setHouseTitle("精装修三室两厅，拎包入住");
        houseInfo.setHouseType("3室2厅1卫");
        houseInfo.setExtent("120㎡");
        houseInfo.setFloor("15/30层");
        houseInfo.setRent(new BigDecimal("3500"));
        houseInfo.setMonthlyRent(new BigDecimal("3500"));
        houseInfo.setAddress("深圳市南山区科技园南区");
        houseInfo.setPhone("13800138000");
        houseInfo.setHouseLabel("近地铁,精装修,家电齐全");
        houseInfo.setTenantId(1);

        // 创建测试图片文件数据（使用您提供的格式）
        JSONArray filesArray = new JSONArray();
        
        JSONObject file1 = new JSONObject();
        file1.put("size", 53148);
        file1.put("type", "image");
        file1.put("url", "https://oss.wsdns.cn/20250507/3a2f69042a6e41f2882030d7059a4247.jpg?x-oss-process=image/resize,w_1680/quality,Q_90");
        file1.put("status", "success");
        file1.put("message", "");
        filesArray.add(file1);

        JSONObject file2 = new JSONObject();
        file2.put("size", 35301);
        file2.put("type", "image");
        file2.put("url", "https://oss.wsdns.cn/20250507/b375176af0b1403da6c4ff66ea4bc503.jpg?x-oss-process=image/resize,w_1680/quality,Q_90");
        file2.put("status", "success");
        file2.put("message", "");
        filesArray.add(file2);

        houseInfo.setFiles(filesArray.toJSONString());

        // 生成海报
        String posterUrl = houseInfoService.generatePoster(houseInfo);
        
        System.out.println("生成的海报URL: " + posterUrl);
        
        // 验证结果
        assert posterUrl != null : "海报生成失败，返回null";
        assert posterUrl.contains("/poster/") : "海报URL格式不正确";
        assert posterUrl.contains(".jpg") : "海报文件格式不正确";

        System.out.println("房产海报生成测试通过！");
        System.out.println("海报包含小程序码，扫码可查看房源详情页：sub_pages/house/detail/" + houseInfo.getHouseId());
    }

    @Test
    public void testGeneratePosterWithMinimalData() throws Exception {
        // 测试最小数据集
        HouseInfo houseInfo = new HouseInfo();
        houseInfo.setHouseId(2);
        houseInfo.setHouseTitle("简单房源");
        houseInfo.setTenantId(1);

        // 只有一张图片
        JSONArray filesArray = new JSONArray();
        JSONObject file = new JSONObject();
        file.put("size", 53148);
        file.put("type", "image");
        file.put("url", "https://oss.wsdns.cn/20250507/3a2f69042a6e41f2882030d7059a4247.jpg?x-oss-process=image/resize,w_1680/quality,Q_90");
        file.put("status", "success");
        file.put("message", "");
        filesArray.add(file);

        houseInfo.setFiles(filesArray.toJSONString());

        // 生成海报
        String posterUrl = houseInfoService.generatePoster(houseInfo);
        
        System.out.println("最小数据海报URL: " + posterUrl);
        
        // 验证结果
        assert posterUrl != null : "最小数据海报生成失败";
        
        System.out.println("最小数据房产海报生成测试通过！");
    }

    @Test
    public void testGeneratePosterWithEmptyFiles() throws Exception {
        // 测试空文件数据
        HouseInfo houseInfo = new HouseInfo();
        houseInfo.setHouseId(3);
        houseInfo.setHouseTitle("无图片房源");
        houseInfo.setTenantId(1);
        houseInfo.setFiles("[]"); // 空数组

        // 生成海报
        String posterUrl = houseInfoService.generatePoster(houseInfo);
        
        System.out.println("空文件海报URL: " + posterUrl);
        
        // 验证结果 - 应该返回null
        assert posterUrl == null : "空文件应该返回null";
        
        System.out.println("空文件房产海报测试通过！");
    }

    @Test
    public void testGeneratePosterWithNullHouseInfo() throws Exception {
        // 测试null房源信息
        String posterUrl = houseInfoService.generatePoster(null);
        
        // 验证结果 - 应该返回null
        assert posterUrl == null : "null房源信息应该返回null";
        
        System.out.println("null房源信息测试通过！");
    }
}
