package com.ruoyi.web.modules.home.service;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import com.ruoyi.web.modules.home.mapper.MemberShopFollowAppMapper;
import com.ruoyi.web.modules.home.service.impl.MemberShopFollowAppServiceImpl;
import com.ruoyi.web.modules.home.vo.MemberShopFollowItemVO;

/**
 * C 端 · 我的店铺关注 Service 单元测试
 */
@ExtendWith(MockitoExtension.class)
class MemberShopFollowAppServiceImplTest
{
    private static final Long MEMBER_ID = 200L;

    @Mock
    private MemberShopFollowAppMapper memberShopFollowAppMapper;

    @InjectMocks
    private MemberShopFollowAppServiceImpl memberShopFollowAppService;

    @Test
    void listFollowShops_returnsMapperResult()
    {
        MemberShopFollowItemVO item1 = followItem(101L, "测试店A");
        MemberShopFollowItemVO item2 = followItem(102L, "测试店B");
        when(memberShopFollowAppMapper.selectFollowShopList(MEMBER_ID)).thenReturn(Arrays.asList(item1, item2));

        List<MemberShopFollowItemVO> result = memberShopFollowAppService.listFollowShops(MEMBER_ID);

        verify(memberShopFollowAppMapper).selectFollowShopList(MEMBER_ID);
        assertEquals(2, result.size());
        assertEquals("测试店A", result.get(0).getShopName());
        assertEquals("测试店B", result.get(1).getShopName());
    }

    @Test
    void listFollowShops_emptyListWhenNoFollows()
    {
        when(memberShopFollowAppMapper.selectFollowShopList(MEMBER_ID)).thenReturn(Collections.emptyList());

        List<MemberShopFollowItemVO> result = memberShopFollowAppService.listFollowShops(MEMBER_ID);

        assertNotNull(result);
        assertTrue(result.isEmpty());
    }

    @Test
    void listFollowShops_nullMapperResultReturnsEmpty()
    {
        when(memberShopFollowAppMapper.selectFollowShopList(MEMBER_ID)).thenReturn(null);

        List<MemberShopFollowItemVO> result = memberShopFollowAppService.listFollowShops(MEMBER_ID);

        assertNotNull(result);
        assertTrue(result.isEmpty());
    }

    @Test
    void listFollowShops_preservesMapperOrder()
    {
        MemberShopFollowItemVO newer = followItem(101L, "新店");
        MemberShopFollowItemVO older = followItem(100L, "老店");
        when(memberShopFollowAppMapper.selectFollowShopList(MEMBER_ID)).thenReturn(Arrays.asList(newer, older));

        List<MemberShopFollowItemVO> result = memberShopFollowAppService.listFollowShops(MEMBER_ID);

        assertEquals(101L, result.get(0).getShopId());
        assertEquals(100L, result.get(1).getShopId());
    }

    private static MemberShopFollowItemVO followItem(Long shopId, String shopName)
    {
        MemberShopFollowItemVO vo = new MemberShopFollowItemVO();
        vo.setShopId(shopId);
        vo.setShopName(shopName);
        vo.setShopAvatar("/avatar/" + shopId + ".jpg");
        vo.setShopStatus("0");
        vo.setRating(new BigDecimal("4.50"));
        vo.setFansCount(10);
        vo.setFollowTime(new Date());
        return vo;
    }
}
