package com.ycwl.basic.pricing.handler;

import com.ycwl.basic.pricing.dto.BundleProductItem;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

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

/**
 * 自定义TypeHandler测试
 */
class BundleProductListTypeHandlerTest {

    private final BundleProductListTypeHandler handler = new BundleProductListTypeHandler();

    @Test
    void testSetNonNullParameter() throws SQLException {
        // 创建测试数据
        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);

        // Mock PreparedStatement
        PreparedStatement ps = Mockito.mock(PreparedStatement.class);

        // 测试序列化
        assertDoesNotThrow(() -> handler.setNonNullParameter(ps, 1, items, null));

        // 验证setString被调用
        verify(ps, times(1)).setString(eq(1), any(String.class));
        
        System.out.println("序列化测试通过");
    }

    @Test
    void testGetNullableResultFromResultSet() throws SQLException {
        // Mock ResultSet
        ResultSet rs = Mockito.mock(ResultSet.class);
        
        // 测试正常JSON
        String json = "[{\"type\":\"PHOTO_PRINT\",\"subType\":\"6寸照片\",\"quantity\":20}]";
        when(rs.getString("included_products")).thenReturn(json);

        List<BundleProductItem> result = handler.getNullableResult(rs, "included_products");
        
        assertNotNull(result);
        assertEquals(1, result.size());
        assertEquals("PHOTO_PRINT", result.get(0).getType());
        assertEquals("6寸照片", result.get(0).getSubType());
        assertEquals(20, result.get(0).getQuantity());
        
        System.out.println("反序列化测试通过: " + result.get(0).getType());
    }

    @Test
    void testGetNullableResultWithNullJson() throws SQLException {
        // Mock ResultSet返回null
        ResultSet rs = Mockito.mock(ResultSet.class);
        when(rs.getString("included_products")).thenReturn(null);

        List<BundleProductItem> result = handler.getNullableResult(rs, "included_products");
        
        assertNotNull(result);
        assertEquals(0, result.size());
        
        System.out.println("Null JSON处理测试通过");
    }

    @Test
    void testGetNullableResultWithEmptyJson() throws SQLException {
        // Mock ResultSet返回空字符串
        ResultSet rs = Mockito.mock(ResultSet.class);
        when(rs.getString("included_products")).thenReturn("");

        List<BundleProductItem> result = handler.getNullableResult(rs, "included_products");
        
        assertNotNull(result);
        assertEquals(0, result.size());
        
        System.out.println("空JSON处理测试通过");
    }

    @Test
    void testGetNullableResultWithInvalidJson() throws SQLException {
        // Mock ResultSet返回无效JSON
        ResultSet rs = Mockito.mock(ResultSet.class);
        when(rs.getString("included_products")).thenReturn("invalid json");

        List<BundleProductItem> result = handler.getNullableResult(rs, "included_products");
        
        assertNotNull(result);
        assertEquals(0, result.size());
        
        System.out.println("无效JSON处理测试通过");
    }
}