package com.ruoyi.app.order;

import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.system.domain.PosOrder;
import com.ruoyi.system.domain.InfoUser;
import com.ruoyi.app.service.UserService;
import com.ruoyi.system.service.IInfoUserService;
import com.ruoyi.system.service.IPosOrderService;
import com.ruoyi.system.service.IPosStoreService;
import com.ruoyi.system.utils.JwtUtil;
import com.ruoyi.common.utils.MessageUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.mockito.ArgumentCaptor;
import org.mockito.MockedStatic;
import org.springframework.test.util.ReflectionTestUtils;

import java.math.BigDecimal;
import java.util.Collections;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

class PosOrderQsOprateControllerTest {
    private MockedStatic<MessageUtils> messages;

    @BeforeEach
    void setUp() {
        TableInfoHelper.initTableInfo(
                new MapperBuilderAssistant(new MybatisConfiguration(), ""), PosOrder.class);
        messages = mockStatic(MessageUtils.class);
        messages.when(() -> MessageUtils.message(anyString()))
                .thenAnswer(invocation -> invocation.getArgument(0));
    }

    @AfterEach
    void tearDown() {
        messages.close();
    }

    @Test
    void newTasksContainOnlyPaidOrdersWaitingForRiderBeforeMerchantAcceptance() {
        PosOrderQsOprateController controller = new PosOrderQsOprateController();
        IPosOrderService orders = mock(IPosOrderService.class);
        ReflectionTestUtils.setField(controller, "posOrderService", orders);
        ReflectionTestUtils.setField(controller, "posStoreService", mock(IPosStoreService.class));
        ReflectionTestUtils.setField(controller, "infoUserService", mock(IInfoUserService.class));
        UserService userService = mock(UserService.class);
        InfoUser rider = new InfoUser();
        rider.setUserId(88L);
        rider.setUserType("2");
        rider.setDeliveryType("FOOD");
        when(userService.checkUserStatus(88L)).thenReturn(rider);
        ReflectionTestUtils.setField(controller, "userService", userService);
        Page<PosOrder> emptyPage = new Page<>(1, 10);
        emptyPage.setRecords(Collections.emptyList());
        when(orders.page(any(Page.class), any(Wrapper.class))).thenReturn(emptyPage);

        controller.orderList(JwtUtil.token("88", "rider"), 1, 10, "newTask",
                (BigDecimal) null, null);

        ArgumentCaptor<LambdaQueryWrapper<PosOrder>> wrapperCaptor = ArgumentCaptor.forClass(LambdaQueryWrapper.class);
        verify(orders).page(any(Page.class), wrapperCaptor.capture());
        LambdaQueryWrapper<PosOrder> wrapper = wrapperCaptor.getValue();
        String sql = wrapper.getSqlSegment().toLowerCase();
        assertTrue(sql.contains("state"));
        assertTrue(sql.contains("pay_status"));
        assertFalse(wrapper.getParamNameValuePairs().containsValue(2L));
        assertTrue(wrapper.getParamNameValuePairs().containsValue(1L));
    }
}
