package com.njcn.rdms.module.system.service.notify;

import com.njcn.rdms.framework.test.core.ut.BaseMockitoUnitTest;
import com.njcn.rdms.module.system.dal.dataobject.notify.NotifyMessageDO;
import com.njcn.rdms.module.system.dal.dataobject.notify.NotifyTemplateDO;
import com.njcn.rdms.module.system.dal.mysql.notify.NotifyMessageMapper;
import com.njcn.rdms.module.system.enums.notify.NotifyMessageLevelConstants;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.verify;

/**
 * 站内信落库单测：消息等级 level 正确落库（指定值 + null 兜底普通）。
 */
class NotifyMessageServiceImplTest extends BaseMockitoUnitTest {

    @InjectMocks
    private NotifyMessageServiceImpl notifyMessageService;

    @Mock
    private NotifyMessageMapper notifyMessageMapper;

    private NotifyTemplateDO template() {
        NotifyTemplateDO t = new NotifyTemplateDO();
        t.setId(1L);
        t.setCode("due_alert_overdue_owner");
        t.setType(3);
        t.setNickname("系统通知");
        return t;
    }

    @Test
    void createNotifyMessage_shouldPersistGivenLevel() {
        notifyMessageService.createNotifyMessage(100L, 2, template(), "正文", null,
                NotifyMessageLevelConstants.SEVERE);

        ArgumentCaptor<NotifyMessageDO> captor = ArgumentCaptor.forClass(NotifyMessageDO.class);
        verify(notifyMessageMapper).insert(captor.capture());
        assertEquals(NotifyMessageLevelConstants.SEVERE, captor.getValue().getLevel());
    }

    @Test
    void createNotifyMessage_nullLevel_shouldFallbackToNormal() {
        notifyMessageService.createNotifyMessage(100L, 2, template(), "正文", null, null);

        ArgumentCaptor<NotifyMessageDO> captor = ArgumentCaptor.forClass(NotifyMessageDO.class);
        verify(notifyMessageMapper).insert(captor.capture());
        assertEquals(NotifyMessageLevelConstants.NORMAL, captor.getValue().getLevel());
    }
}
