package com.ycwl.basic.pricing.mapper;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ycwl.basic.pricing.dto.BundleProductItem;
import com.ycwl.basic.pricing.entity.PriceBundleConfig;
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.*;

/**
 * 一口价配置JSON序列化测试
 */
class PriceBundleConfigJsonTest {

    private final ObjectMapper objectMapper = new ObjectMapper();

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

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

        List<BundleProductItem> items = Arrays.asList(item1, item2);

        // 测试序列化
        String json = objectMapper.writeValueAsString(items);
        System.out.println("序列化结果: " + json);
        
        assertNotNull(json);
        assertTrue(json.contains("PHOTO_PRINT"));
        assertTrue(json.contains("6寸照片"));
        assertTrue(json.contains("VLOG_VIDEO"));

        // 测试反序列化
        List<BundleProductItem> deserializedItems = objectMapper.readValue(json,
                objectMapper.getTypeFactory().constructCollectionType(List.class, BundleProductItem.class));
        
        assertNotNull(deserializedItems);
        assertEquals(2, deserializedItems.size());
        assertEquals("PHOTO_PRINT", deserializedItems.get(0).getType());
        assertEquals("6寸照片", deserializedItems.get(0).getSubType());
        assertEquals(20, deserializedItems.get(0).getQuantity());
    }

    @Test
    void testPriceBundleConfigFieldsNotNull() {
        // 创建测试配置
        PriceBundleConfig config = new PriceBundleConfig();
        config.setBundleName("测试套餐");
        config.setBundlePrice(new BigDecimal("99.00"));
        
        // 创建包含商品
        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);
        
        config.setIncludedProducts(List.of(includedItem));
        config.setExcludedProducts(List.of(excludedItem));
        config.setIsActive(true);

        // 验证字段不为null
        assertNotNull(config.getIncludedProducts());
        assertNotNull(config.getExcludedProducts());
        assertEquals(1, config.getIncludedProducts().size());
        assertEquals(1, config.getExcludedProducts().size());
        
        System.out.println("包含商品数量: " + config.getIncludedProducts().size());
        System.out.println("排除商品数量: " + config.getExcludedProducts().size());
        System.out.println("包含商品: " + config.getIncludedProducts().get(0).getType());
        System.out.println("排除商品: " + config.getExcludedProducts().get(0).getType());
    }

    @Test
    void testEmptyListSerialization() throws JsonProcessingException {
        // 测试空列表的序列化
        PriceBundleConfig config = new PriceBundleConfig();
        config.setBundleName("测试套餐");
        config.setBundlePrice(new BigDecimal("99.00"));
        config.setIncludedProducts(List.of());  // 空列表
        config.setExcludedProducts(null);  // null值
        
        assertNotNull(config.getIncludedProducts());
        assertEquals(0, config.getIncludedProducts().size());
        assertNull(config.getExcludedProducts());
        
        System.out.println("空列表测试通过");
    }
}