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.Mockito.doNothing;
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.post;
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 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.order.service.IOrderAppService;
import com.ruoyi.web.modules.order.vo.OrderAppDetailVO;
import com.ruoyi.web.modules.pay.vo.WeChatPayParamsVO;

@ExtendWith(MockitoExtension.class)
class OrderAppControllerTest
{
    private static final Long MEMBER_ID = 200L;

    private static final Long ORDER_ID = 900L;

    @Mock
    private IOrderAppService orderAppService;

    @InjectMocks
    private OrderAppController controller;

    private MockMvc mockMvc;

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

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

    @Test
    void pay_requiresLogin() throws Exception
    {
        NestedServletException ex = assertThrows(NestedServletException.class,
                () -> mockMvc.perform(post("/api/order/{orderId}/pay", ORDER_ID)).andReturn());
        assertInstanceOf(ServiceException.class, ex.getCause());
    }

    @Test
    void pay_returns200() throws Exception
    {
        MemberContext.setMemberId(MEMBER_ID);
        WeChatPayParamsVO params = new WeChatPayParamsVO();
        params.setOrderId(ORDER_ID);
        params.setMock(true);
        when(orderAppService.createPay(MEMBER_ID, ORDER_ID)).thenReturn(params);

        mockMvc.perform(post("/api/order/{orderId}/pay", ORDER_ID))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.data.mock").value(true));

        verify(orderAppService).createPay(MEMBER_ID, ORDER_ID);
    }

    @Test
    void cancelPay_returns200() throws Exception
    {
        MemberContext.setMemberId(MEMBER_ID);
        doNothing().when(orderAppService).cancelPay(MEMBER_ID, ORDER_ID);

        mockMvc.perform(post("/api/order/{orderId}/pay/cancel", ORDER_ID))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200));

        verify(orderAppService).cancelPay(MEMBER_ID, ORDER_ID);
    }

    @Test
    void detail_returns200() throws Exception
    {
        MemberContext.setMemberId(MEMBER_ID);
        OrderAppDetailVO vo = new OrderAppDetailVO();
        vo.setOrderId(ORDER_ID);
        vo.setOrderNo("O20260526000001");
        vo.setPayAmount(new BigDecimal("25.00"));
        vo.setItems(Collections.emptyList());
        when(orderAppService.getDetail(MEMBER_ID, ORDER_ID)).thenReturn(vo);

        mockMvc.perform(get("/api/order/{orderId}", ORDER_ID))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data.orderNo").value("O20260526000001"));
    }

    @Test
    void confirmReceive_returns200() throws Exception
    {
        MemberContext.setMemberId(MEMBER_ID);
        doNothing().when(orderAppService).confirmReceive(MEMBER_ID, ORDER_ID);

        mockMvc.perform(post("/api/order/{orderId}/confirm-receive", ORDER_ID))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200));

        verify(orderAppService).confirmReceive(MEMBER_ID, ORDER_ID);
    }

    @Test
    void badges_returns200() throws Exception
    {
        MemberContext.setMemberId(MEMBER_ID);
        when(orderAppService.getBadges(MEMBER_ID)).thenReturn(new com.ruoyi.web.modules.order.vo.OrderAppBadgeVO());

        mockMvc.perform(get("/api/order/badges"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200));
    }
}
