package com.ruoyi.web;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.framework.config.FastJson2JsonRedisSerializer;
import com.ruoyi.web.modules.openstats.vo.CategorySalesItemVO;
import com.ruoyi.web.modules.openstats.vo.CategorySalesVO;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

class FastJson2OpenStatsCacheTest
{
    private static final String CATEGORY_SALES_JSON =
            "{\"@type\":\"com.ruoyi.web.modules.openstats.vo.CategorySalesVO\",\"items\":[{\"categoryId\":1L,\"categoryName\":\"兽药\",\"qty\":15L,\"ratio\":100.0}],\"statYear\":2026,\"totalQty\":15L}";

    @Test
    void deserializeCategorySalesAsTypedObject()
    {
        FastJson2JsonRedisSerializer<Object> serializer = new FastJson2JsonRedisSerializer<>(Object.class);
        Object cached = serializer.deserialize(CATEGORY_SALES_JSON.getBytes(StandardCharsets.UTF_8));
        Assertions.assertNotNull(cached);
        Assertions.assertTrue(cached instanceof CategorySalesVO);
        Assertions.assertEquals(2026, ((CategorySalesVO) cached).getStatYear());
    }

    @Test
    void jsonObjectCanConvertToCategorySales()
    {
        JSONObject jsonObject = JSONObject.parseObject(CATEGORY_SALES_JSON);
        CategorySalesVO vo = jsonObject.to(CategorySalesVO.class);
        Assertions.assertEquals(15L, vo.getTotalQty());
        Assertions.assertEquals("兽药", vo.getItems().get(0).getCategoryName());
    }

    @Test
    void roundTripCategorySales()
    {
        CategorySalesItemVO item = new CategorySalesItemVO();
        item.setCategoryId(1L);
        item.setCategoryName("兽药");
        item.setQty(15L);
        item.setRatio(100.0);

        CategorySalesVO vo = new CategorySalesVO();
        vo.setStatYear(2026);
        vo.setTotalQty(15L);
        vo.setItems(Collections.singletonList(item));

        FastJson2JsonRedisSerializer<Object> serializer = new FastJson2JsonRedisSerializer<>(Object.class);
        Object cached = serializer.deserialize(serializer.serialize(vo));
        Assertions.assertTrue(cached instanceof CategorySalesVO);
    }

    @Test
    void deserializeJsonWithJavaLongLiteralSuffix()
    {
        String legacy = CATEGORY_SALES_JSON.replace(":15L", ":15").replace(":1L", ":1");
        String withLongSuffix = legacy.replace("\"qty\":15", "\"qty\":15L").replace("\"categoryId\":1", "\"categoryId\":1L");
        FastJson2JsonRedisSerializer<Object> serializer = new FastJson2JsonRedisSerializer<>(Object.class);
        Object cached = serializer.deserialize(withLongSuffix.getBytes(StandardCharsets.UTF_8));
        Assertions.assertTrue(cached instanceof CategorySalesVO);
    }

    @Test
    void deserializeJacksonFormatWithoutTypeHint()
    {
        String jacksonJson = "{\"statYear\":2026,\"totalQty\":15,\"items\":[{\"categoryId\":1,\"categoryName\":\"兽药\",\"qty\":15,\"ratio\":100.0}]}";
        FastJson2JsonRedisSerializer<Object> serializer = new FastJson2JsonRedisSerializer<>(Object.class);
        Object cached = serializer.deserialize(jacksonJson.getBytes(StandardCharsets.UTF_8));
        Assertions.assertNotNull(cached);
    }
}
