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

import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.never;
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 org.springframework.web.util.NestedServletException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.category.support.SellerShopContext;
import com.ruoyi.web.modules.delivery.dto.DeliveryRegionDTO;
import com.ruoyi.web.modules.delivery.dto.ShopDeliverySaveDTO;
import com.ruoyi.web.modules.delivery.service.IShopDeliveryService;
import com.ruoyi.web.modules.delivery.vo.DeliveryRegionVO;
import com.ruoyi.web.modules.delivery.vo.ShopDeliverySettingVO;

@ExtendWith(MockitoExtension.class)
class SellerDeliveryControllerTest
{
    @Mock
    private IShopDeliveryService shopDeliveryService;

    @Spy
    @InjectMocks
    private SellerDeliveryController controller;

    private MockMvc mockMvc;

    private final ObjectMapper objectMapper = new ObjectMapper();

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

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

    @Test
    void getSetting_withoutShopId_rejected()
    {
        SellerShopContext.clear();

        NestedServletException ex = assertThrows(NestedServletException.class,
                () -> mockMvc.perform(get("/agri/seller/delivery")).andReturn());
        assertInstanceOf(ServiceException.class, ex.getCause());
        verify(shopDeliveryService, never()).getByShopId(org.mockito.ArgumentMatchers.any());
    }

    @Test
    void getSetting_returns200() throws Exception
    {
        ShopDeliverySettingVO vo = new ShopDeliverySettingVO();
        vo.setShopId(101L);
        vo.setShipperName("张三");
        DeliveryRegionVO region = new DeliveryRegionVO();
        region.setCityName("拉萨市");
        vo.setRegion(region);
        when(shopDeliveryService.getByShopId(101L)).thenReturn(vo);

        mockMvc.perform(get("/agri/seller/delivery"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data.shipperName").value("张三"))
                .andExpect(jsonPath("$.data.region.cityName").value("拉萨市"));
    }

    @Test
    void saveSetting_delegatesToService() throws Exception
    {
        ShopDeliverySaveDTO dto = new ShopDeliverySaveDTO();
        dto.setShipperName("张三");
        dto.setShipperMobile("13800138000");
        DeliveryRegionDTO region = new DeliveryRegionDTO();
        region.setRegionCode("540102");
        dto.setRegion(region);
        dto.setDetailAddress("某某路 1 号");

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

        verify(shopDeliveryService).save(eq(101L), org.mockito.ArgumentMatchers.any(), eq("seller01"));
    }
}
