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

import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.Date;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
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 org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.util.NestedServletException;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.account.support.MemberContext;
import com.ruoyi.web.modules.home.service.IMemberShopFollowAppService;
import com.ruoyi.web.modules.home.vo.MemberShopFollowItemVO;

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

    @Mock
    private IMemberShopFollowAppService memberShopFollowAppService;

    @InjectMocks
    private MemberShopFollowAppController controller;

    private MockMvc mockMvc;

    @BeforeEach
    void setUp()
    {
        mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
    }

    @AfterEach
    void tearDown()
    {
        MemberContext.clear();
    }

    @Test
    void list_requiresLogin() throws Exception
    {
        NestedServletException ex = assertThrows(NestedServletException.class,
                () -> mockMvc.perform(get("/api/member/shop-follows")).andReturn());
        assertInstanceOf(ServiceException.class, ex.getCause());
    }

    @Test
    void list_returns200WithPagination() throws Exception
    {
        MemberContext.setMemberId(MEMBER_ID);
        when(memberShopFollowAppService.listFollowShops(MEMBER_ID))
                .thenReturn(Collections.singletonList(followItem()));

        mockMvc.perform(get("/api/member/shop-follows")
                .param("pageNum", "1")
                .param("pageSize", "10"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.rows[0].shopId").value(101))
                .andExpect(jsonPath("$.rows[0].shopName").value("测试店"))
                .andExpect(jsonPath("$.rows[0].shopAvatar").value("/avatar.jpg"))
                .andExpect(jsonPath("$.rows[0].shopStatus").value("0"))
                .andExpect(jsonPath("$.rows[0].rating").value(4.5))
                .andExpect(jsonPath("$.rows[0].fansCount").value(8));

        verify(memberShopFollowAppService).listFollowShops(MEMBER_ID);
    }

    @Test
    void list_emptyResult() throws Exception
    {
        MemberContext.setMemberId(MEMBER_ID);
        when(memberShopFollowAppService.listFollowShops(MEMBER_ID)).thenReturn(Collections.emptyList());

        mockMvc.perform(get("/api/member/shop-follows"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.rows").isArray())
                .andExpect(jsonPath("$.rows").isEmpty());
    }

    private static MemberShopFollowItemVO followItem()
    {
        MemberShopFollowItemVO vo = new MemberShopFollowItemVO();
        vo.setShopId(101L);
        vo.setShopName("测试店");
        vo.setShopAvatar("/avatar.jpg");
        vo.setShopStatus("0");
        vo.setRating(new BigDecimal("4.50"));
        vo.setFansCount(8);
        vo.setFollowTime(new Date());
        return vo;
    }
}
