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

import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
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 java.util.Collections;
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.order.constant.OrderAppConstants;
import com.ruoyi.web.modules.order.dto.SellerAftersaleFinishDTO;
import com.ruoyi.web.modules.order.service.ISellerAftersaleService;
import com.ruoyi.web.modules.order.vo.SellerAftersaleDetailVO;
import com.ruoyi.web.modules.order.vo.SellerAftersaleListRowVO;

@ExtendWith(MockitoExtension.class)
class SellerAftersaleControllerTest
{
    @Mock
    private ISellerAftersaleService sellerAftersaleService;

    @Spy
    @InjectMocks
    private SellerAftersaleController 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 list_withoutShopId_rejected()
    {
        SellerShopContext.clear();

        NestedServletException ex = assertThrows(NestedServletException.class,
                () -> mockMvc.perform(get("/agri/seller/aftersale/list")).andReturn());
        assertInstanceOf(ServiceException.class, ex.getCause());
        verify(sellerAftersaleService, never()).selectSellerList(any(), any());
    }

    @Test
    void list_returns200() throws Exception
    {
        SellerAftersaleListRowVO row = new SellerAftersaleListRowVO();
        row.setAftersaleId(1L);
        row.setAftersaleNo("AS20260526000001");
        row.setAftersaleStatusText("进行中");
        when(sellerAftersaleService.selectSellerList(eq(101L), any())).thenReturn(Collections.singletonList(row));

        mockMvc.perform(get("/agri/seller/aftersale/list"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.rows[0].aftersaleNo").value("AS20260526000001"));
    }

    @Test
    void detail_returns200() throws Exception
    {
        SellerAftersaleDetailVO vo = new SellerAftersaleDetailVO();
        vo.setAftersaleId(10L);
        vo.setOrderNo("O202605260001");
        when(sellerAftersaleService.getSellerDetail(101L, 10L)).thenReturn(vo);

        mockMvc.perform(get("/agri/seller/aftersale/10"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data.orderNo").value("O202605260001"));
    }

    @Test
    void finish_delegatesToService() throws Exception
    {
        SellerAftersaleFinishDTO dto = new SellerAftersaleFinishDTO();
        dto.setProcessResult("已同意退款");

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

        verify(sellerAftersaleService).finish(eq(101L), eq(10L), any(), eq("seller01"));
    }
}
