package com.ycwl.basic.pricing.entity;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ycwl.basic.pricing.dto.BundleProductItem;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

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

/**
 * 验证PriceBundleConfig新数据结构的测试
 */
class PriceBundleConfigStructureTest {

    private final ObjectMapper objectMapper = new ObjectMapper();

    @Test
    void testNewDataStructure() throws JsonProcessingException {
        // 创建测试数据
        BundleProductItem includedItem = new BundleProductItem();
        includedItem.setType("PHOTO_PRINT");
        includedItem.setSubType("6寸照片");
        includedItem.setQuantity(20);

        BundleProductItem excludedItem = new BundleProductItem();
        excludedItem.setType("VLOG_VIDEO");
        excludedItem.setProductId("video_001");
        excludedItem.setQuantity(1);

        List<BundleProductItem> includedProducts = List.of(includedItem);
        List<BundleProductItem> excludedProducts = List.of(excludedItem);

        // 创建实体
        PriceBundleConfig config = new PriceBundleConfig();
        config.setBundleName("全家福套餐");
        config.setBundlePrice(new BigDecimal("99.00"));
        config.setIncludedProducts(includedProducts);
        config.setExcludedProducts(excludedProducts);
        config.setDescription("包含20张6寸照片打印");
        config.setIsActive(true);

        // 验证数据结构
        assertNotNull(config.getIncludedProducts());
        assertNotNull(config.getExcludedProducts());
        assertEquals(1, config.getIncludedProducts().size());
        assertEquals(1, config.getExcludedProducts().size());
        
        // 验证包含商品
        BundleProductItem included = config.getIncludedProducts().get(0);
        assertEquals("PHOTO_PRINT", included.getType());
        assertEquals("6寸照片", included.getSubType());
        assertEquals(20, included.getQuantity());
        
        // 验证排除商品
        BundleProductItem excluded = config.getExcludedProducts().get(0);
        assertEquals("VLOG_VIDEO", excluded.getType());
        assertEquals("video_001", excluded.getProductId());
        assertEquals(1, excluded.getQuantity());

        // 验证JSON序列化
        String includedJson = objectMapper.writeValueAsString(config.getIncludedProducts());
        String excludedJson = objectMapper.writeValueAsString(config.getExcludedProducts());
        
        System.out.println("Included Products JSON: " + includedJson);
        System.out.println("Excluded Products JSON: " + excludedJson);
        
        // 验证能正确反序列化
        List<BundleProductItem> deserializedIncluded = objectMapper.readValue(includedJson, 
            objectMapper.getTypeFactory().constructCollectionType(List.class, BundleProductItem.class));
        assertEquals(1, deserializedIncluded.size());
        assertEquals("PHOTO_PRINT", deserializedIncluded.get(0).getType());
    }
    
    @Test
    void testFrontendExpectedFormat() throws JsonProcessingException {
        // 测试前端期望的JSON格式
        String expectedJson = "[{\"type\":\"PHOTO_PRINT\",\"subType\":\"6寸照片\",\"quantity\":20}]";
        
        List<BundleProductItem> items = objectMapper.readValue(expectedJson, 
            objectMapper.getTypeFactory().constructCollectionType(List.class, BundleProductItem.class));
        
        assertEquals(1, items.size());
        assertEquals("PHOTO_PRINT", items.get(0).getType());
        assertEquals("6寸照片", items.get(0).getSubType());
        assertEquals(20, items.get(0).getQuantity());
    }
}