package com.njcn.rdms.module.system.dal.mysql.notify;

import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.njcn.rdms.framework.common.pojo.PageResult;
import com.njcn.rdms.module.system.controller.admin.notify.vo.message.NotifyMessageMyPageReqVO;
import com.njcn.rdms.module.system.dal.dataobject.notify.NotifyMessageDO;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;

import java.util.Locale;

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

/**
 * {@link NotifyMessageMapper} 的单元测试 —— 我的站内信分页（keyword 检索 + 排序口径）。
 */
class NotifyMessageMapperTest {

    @BeforeAll
    static void initMyBatisPlusTableInfo() {
        TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), NotifyMessageDO.class);
    }

    @SuppressWarnings({"unchecked", "rawtypes"})
    private String captureMyPageSqlSegment(NotifyMessageMyPageReqVO reqVO) {
        NotifyMessageMapper mapper = mock(NotifyMessageMapper.class, invocation -> invocation.callRealMethod());
        doReturn(PageResult.empty()).when(mapper).selectPage(eq(reqVO), any(Wrapper.class));

        mapper.selectPage(reqVO, 1L, 2);

        ArgumentCaptor<Wrapper<NotifyMessageDO>> wrapperCaptor = ArgumentCaptor.forClass(Wrapper.class);
        verify(mapper).selectPage(eq(reqVO), wrapperCaptor.capture());
        return wrapperCaptor.getValue().getSqlSegment().toLowerCase(Locale.ROOT);
    }

    @Test
    void myPage_keywordShouldFilterTemplateContentByLike() {
        NotifyMessageMyPageReqVO reqVO = new NotifyMessageMyPageReqVO();
        reqVO.setKeyword("指派");
        reqVO.setReadStatus(false);

        String sqlSegment = captureMyPageSqlSegment(reqVO);

        assertTrue(sqlSegment.contains("template_content like"));
        assertTrue(sqlSegment.contains("read_status"));
        assertTrue(sqlSegment.contains("user_id"));
        assertTrue(sqlSegment.contains("user_type"));
    }

    @Test
    void myPage_blankKeywordShouldNotAddLikeCondition() {
        NotifyMessageMyPageReqVO noKeyword = new NotifyMessageMyPageReqVO();
        assertFalse(captureMyPageSqlSegment(noKeyword).contains("like"));

        NotifyMessageMyPageReqVO blankKeyword = new NotifyMessageMyPageReqVO();
        blankKeyword.setKeyword("");
        assertFalse(captureMyPageSqlSegment(blankKeyword).contains("like"));
    }

    @Test
    void myPage_shouldOrderByIdDescOnly() {
        String sqlSegment = captureMyPageSqlSegment(new NotifyMessageMyPageReqVO());

        // 与前端约定的分页口径：id 倒序（= 收到时间倒序、唯一稳定），不随读状态重排
        assertTrue(sqlSegment.contains("order by id desc"));
        assertFalse(sqlSegment.contains("read_status desc"));
        assertFalse(sqlSegment.contains("read_time"));
    }

}
