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

import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.lenient;
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.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
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.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.web.modules.category.support.SellerShopContext;
import com.ruoyi.web.modules.store.dto.SellerShopProfileDTO;
import com.ruoyi.web.modules.store.service.IShopService;
import com.ruoyi.web.modules.store.vo.SellerShopProfileVO;

@ExtendWith(MockitoExtension.class)
class SellerShopControllerTest
{
    @Mock
    private IShopService shopService;

    @Spy
    @InjectMocks
    private SellerShopController controller;

    private MockMvc mockMvc;

    private final ObjectMapper objectMapper = new ObjectMapper();

    @BeforeEach
    void setUp() throws Exception
    {
        SellerShopContext.setShopId(1L);
        mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
        lenient().doReturn("seller").when(controller).getUsername();
    }

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

    @Test
    void getProfile_returnsShopFields() throws Exception
    {
        SellerShopProfileVO vo = new SellerShopProfileVO();
        vo.setShopId(1L);
        vo.setShopName("测试店");
        vo.setShopAvatar("/a.jpg");
        vo.setShopPhone("010-88886666");
        vo.setShopStatus("0");
        when(shopService.selectSellerShopProfile(1L)).thenReturn(vo);

        mockMvc.perform(get("/agri/seller/shop"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data.shopName").value("测试店"));
    }

    @Test
    void updateProfile_delegatesToService() throws Exception
    {
        SellerShopProfileDTO dto = new SellerShopProfileDTO();
        dto.setShopName("新店名");
        dto.setShopAvatar("/b.jpg");
        dto.setShopPhone("010-88886667");

        mockMvc.perform(put("/agri/seller/shop")
                .contentType(MediaType.APPLICATION_JSON)
                .content(objectMapper.writeValueAsString(dto)))
                .andExpect(status().isOk());

        verify(shopService).updateSellerShopProfile(eq(1L), org.mockito.ArgumentMatchers.any(), eq("seller"));
    }
}
