package com.ruoyi.app.mendian;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.system.domain.InfoUser;
import com.ruoyi.system.domain.PosFood;
import com.ruoyi.system.service.IInfoUserService;
import com.ruoyi.system.service.IPosFenleiService;
import com.ruoyi.system.service.IPosFoodService;
import com.ruoyi.system.utils.JwtUtil;
import com.ruoyi.system.utils.AuthContext;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.MockitoAnnotations;

import java.util.Collections;

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.verify;
import static org.mockito.Mockito.when;

class PosFoodControllerShelfFilterTest {

    @Mock
    private IPosFoodService posFoodService;

    @Mock
    private IInfoUserService infoUserService;

    @Mock
    private IPosFenleiService posFenleiService;

    @InjectMocks
    private PosFoodController controller;

    private AutoCloseable mocks;
    private MockedStatic<MessageUtils> messages;
    private MockedStatic<AuthContext> authContext;

    @BeforeEach
    void setUp() {
        mocks = MockitoAnnotations.openMocks(this);
        messages = org.mockito.Mockito.mockStatic(MessageUtils.class);
        messages.when(() -> MessageUtils.message(anyString()))
                .thenAnswer(invocation -> invocation.getArgument(0));
        authContext = org.mockito.Mockito.mockStatic(AuthContext.class);
        authContext.when(AuthContext::requireUserId).thenReturn(101L);
    }

    @AfterEach
    void tearDown() throws Exception {
        authContext.close();
        messages.close();
        mocks.close();
    }

    @Test
    void getidlistExcludesOffShelfFoodsWhenNoShelfFilterIsProvided() {
        InfoUser user = new InfoUser();
        user.setUserType("0");
        when(infoUserService.getById(101L)).thenReturn(user);
        when(posFoodService.list(any(QueryWrapper.class))).thenReturn(Collections.emptyList());

        controller.getidlist(JwtUtil.token("101", "customer"), 9, "", null, null);

        @SuppressWarnings("unchecked")
        ArgumentCaptor<QueryWrapper<PosFood>> queryCaptor = ArgumentCaptor.forClass(QueryWrapper.class);
        verify(posFoodService).list(queryCaptor.capture());
        QueryWrapper<PosFood> query = queryCaptor.getValue();
        assertTrue(query.getSqlSegment().contains("stacking_up <>"));
        assertTrue(query.getParamNameValuePairs().containsValue("1"));
    }
}
