package com.ruoyi.web.modules.epidemic.service.impl;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.Calendar;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.epidemic.EpidemicReportTestSamples;
import com.ruoyi.web.modules.epidemic.domain.BizEpidemicReport;
import com.ruoyi.web.modules.epidemic.domain.dto.EpidemicReportRelieveRequest;
import com.ruoyi.web.modules.epidemic.mapper.BizEpidemicReportMapper;

/**
 * YQSB-UT-010～013
 */
@ExtendWith(MockitoExtension.class)
@DisplayName("BizEpidemicReportServiceImpl")
class BizEpidemicReportServiceImplTest
{

    @Mock
    private BizEpidemicReportMapper epidemicReportMapper;

    @InjectMocks
    private BizEpidemicReportServiceImpl service;

    @Test
    @DisplayName("YQSB-UT-010 新增不写解除日")
    void ut010_insert()
    {
        when(epidemicReportMapper.insertBizEpidemicReport(any())).thenReturn(1);
        BizEpidemicReport row = EpidemicReportTestSamples.sampleOpen();
        row.setReleaseDate(EpidemicReportTestSamples.date(2026, Calendar.JUNE, 1));
        assertEquals(1, service.insertBizEpidemicReport(row));
        verify(epidemicReportMapper).insertBizEpidemicReport(argThat(r -> r.getReleaseDate() == null));
    }

    @Test
    @DisplayName("YQSB-UT-011 解除更新日期")
    void ut011_relieve()
    {
        BizEpidemicReport db = EpidemicReportTestSamples.sampleOpen();
        db.setId(1L);
        when(epidemicReportMapper.selectBizEpidemicReportById(1L)).thenReturn(db);
        when(epidemicReportMapper.updateReleaseDate(any())).thenReturn(1);
        EpidemicReportRelieveRequest req = new EpidemicReportRelieveRequest();
        req.setId(1L);
        req.setReleaseDate(EpidemicReportTestSamples.date(2026, Calendar.MAY, 20));
        assertEquals(1, service.relieve(req, "tester"));
        verify(epidemicReportMapper).updateReleaseDate(argThat(r ->
                r.getReleaseDate() != null && r.getId() == 1L));
    }

    @Test
    @DisplayName("YQSB-UT-012 已解除禁止修改")
    void ut012_updateRelieved()
    {
        BizEpidemicReport db = EpidemicReportTestSamples.sampleRelieved();
        when(epidemicReportMapper.selectBizEpidemicReportById(2L)).thenReturn(db);
        BizEpidemicReport in = EpidemicReportTestSamples.sampleOpen();
        in.setId(2L);
        in.setEpidemicName("新名称");
        assertThrows(ServiceException.class, () -> service.updateBizEpidemicReport(in));
        verify(epidemicReportMapper, never()).updateBizEpidemicReport(any());
    }

    @Test
    @DisplayName("YQSB-UT-013 重复解除")
    void ut013_relieveAgain()
    {
        when(epidemicReportMapper.selectBizEpidemicReportById(2L))
                .thenReturn(EpidemicReportTestSamples.sampleRelieved());
        EpidemicReportRelieveRequest req = new EpidemicReportRelieveRequest();
        req.setId(2L);
        req.setReleaseDate(EpidemicReportTestSamples.date(2026, Calendar.JUNE, 2));
        assertThrows(ServiceException.class, () -> service.relieve(req, "tester"));
    }
}
