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.doReturn;
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.delete;
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.ReviewConstants;
import com.ruoyi.web.modules.order.dto.SellerReviewReplyDTO;
import com.ruoyi.web.modules.order.service.ISellerReviewService;
import com.ruoyi.web.modules.order.vo.SellerReviewDetailVO;
import com.ruoyi.web.modules.order.vo.SellerReviewListRowVO;

@ExtendWith(MockitoExtension.class)
class SellerReviewControllerTest
{
    @Mock
    private ISellerReviewService sellerReviewService;

    @Spy
    @InjectMocks
    private SellerReviewController 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/review/list")).andReturn());
        assertInstanceOf(ServiceException.class, ex.getCause());
        verify(sellerReviewService, never()).selectSellerList(any(), any());
    }

    @Test
    void list_returns200() throws Exception
    {
        SellerReviewListRowVO row = new SellerReviewListRowVO();
        row.setReviewId(1L);
        row.setMemberNickName("买家A");
        row.setReplyStatus(ReviewConstants.REPLY_STATUS_UNREPLIED);
        when(sellerReviewService.selectSellerList(eq(101L), any())).thenReturn(Collections.singletonList(row));

        mockMvc.perform(get("/agri/seller/review/list"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.rows[0].memberNickName").value("买家A"));
    }

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

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

    @Test
    void reply_delegatesToService() throws Exception
    {
        SellerReviewReplyDTO dto = new SellerReviewReplyDTO();
        dto.setReplyContent("感谢您的认可");

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

        verify(sellerReviewService).reply(eq(101L), eq(10L), any(), eq("seller01"));
    }

    @Test
    void remove_delegatesToService() throws Exception
    {
        mockMvc.perform(delete("/agri/seller/review/10"))
                .andExpect(status().isOk());

        verify(sellerReviewService).deleteReview(eq(101L), eq(10L), eq("seller01"));
    }
}
