package com.ruoyi.app.tableqrcode;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.system.domain.InfoUser;
import com.ruoyi.system.domain.PosStore;
import com.ruoyi.system.domain.TableQrcode;
import com.ruoyi.system.service.IInfoUserService;
import com.ruoyi.system.service.IPosStoreService;
import com.ruoyi.system.service.ITableQrcodeService;
import com.ruoyi.system.utils.JwtUtil;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import org.springframework.test.util.ReflectionTestUtils;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
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.when;

class TableQrcodeControllerTest {

    private TableQrcodeController controller;
    private ITableQrcodeService tableQrcodeService;
    private IInfoUserService infoUserService;
    private IPosStoreService posStoreService;
    private MockedStatic<MessageUtils> messages;

    @BeforeEach
    void setUp() {
        controller = new TableQrcodeController();
        tableQrcodeService = mock(ITableQrcodeService.class);
        infoUserService = mock(IInfoUserService.class);
        posStoreService = mock(IPosStoreService.class);
        ReflectionTestUtils.setField(controller, "tableQrcodeService", tableQrcodeService);
        ReflectionTestUtils.setField(controller, "infoUserService", infoUserService);
        ReflectionTestUtils.setField(controller, "posStoreService", posStoreService);

        messages = mockStatic(MessageUtils.class);
        messages.when(() -> MessageUtils.message(anyString()))
                .thenAnswer(invocation -> invocation.getArgument(0));
    }

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

    @Test
    void listIncludesStoreNameForStoreCodesAndLeavesPublicCodesEmpty() {
        InfoUser merchant = new InfoUser();
        merchant.setUserId(101L);
        merchant.setUserType("4");
        merchant.setStoreId(11L);
        when(infoUserService.selectInfoUserByUserId(101L)).thenReturn(merchant);

        TableQrcode storeCode = new TableQrcode();
        storeCode.setId(1L);
        storeCode.setStoreId(11L);
        TableQrcode publicCode = new TableQrcode();
        publicCode.setId(2L);
        when(tableQrcodeService.list(org.mockito.ArgumentMatchers
                .<Wrapper<TableQrcode>>any())).thenReturn(List.of(storeCode, publicCode));

        PosStore store = new PosStore();
        store.setId(11);
        store.setPosName("烧烤摊");
        when(posStoreService.listByIds(any())).thenReturn(List.of(store));

        AjaxResult result = controller.list(tokenFor(101L), null, null, null, null);

        @SuppressWarnings("unchecked")
        List<TableQrcode> data = (List<TableQrcode>) result.get(AjaxResult.DATA_TAG);
        assertEquals("烧烤摊", data.get(0).getStoreName());
        assertNull(data.get(1).getStoreName());
    }

    private static String tokenFor(Long userId) {
        return JwtUtil.setToken(String.valueOf(userId), "merchant");
    }
}
