package com.ruoyi.web.modules.epidemic.support;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.Calendar;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
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;

/**
 * YQSB-UT-004～007
 */
@DisplayName("EpidemicReportValidation")
class EpidemicReportValidationTest
{

    @Test
    @DisplayName("YQSB-UT-005 涉疫轨迹长度")
    void ut005_trackLength()
    {
        assertThrows(ServiceException.class, () -> EpidemicReportValidation.validateAffectedLocationTrack(""));
        assertThrows(ServiceException.class, () -> EpidemicReportValidation.validateAffectedLocationTrack(longStr(501)));
        assertDoesNotThrow(() -> EpidemicReportValidation.validateAffectedLocationTrack("轨迹"));
    }

    @Test
    @DisplayName("YQSB-UT-006 疫病名称长度")
    void ut006_epidemicName()
    {
        assertThrows(ServiceException.class, () -> EpidemicReportValidation.validateEpidemicName(""));
        assertThrows(ServiceException.class, () -> EpidemicReportValidation.validateEpidemicName(longStr(51)));
        assertDoesNotThrow(() -> EpidemicReportValidation.validateEpidemicName("口蹄疫"));
    }

    @Test
    @DisplayName("YQSB-UT-007 解除不早于上报")
    void ut007_releaseDateRange()
    {
        EpidemicReportRelieveRequest req = new EpidemicReportRelieveRequest();
        req.setId(1L);
        req.setReleaseDate(EpidemicReportTestSamples.date(2026, Calendar.MAY, 9));
        assertThrows(ServiceException.class, () -> EpidemicReportValidation.validateRelieveRequest(req,
                EpidemicReportTestSamples.date(2026, Calendar.MAY, 10)));
        req.setReleaseDate(EpidemicReportTestSamples.date(2026, Calendar.MAY, 10));
        assertDoesNotThrow(() -> EpidemicReportValidation.validateRelieveRequest(req,
                EpidemicReportTestSamples.date(2026, Calendar.MAY, 10)));
    }

    @Test
    @DisplayName("合法保存")
    void validRow()
    {
        assertDoesNotThrow(() -> EpidemicReportValidation.validateRowForSave(EpidemicReportTestSamples.sampleOpen()));
        BizEpidemicReport bad = EpidemicReportTestSamples.sampleOpen();
        bad.setTransmissionRoute(9);
        assertThrows(ServiceException.class, () -> EpidemicReportValidation.validateRowForSave(bad));
    }

    private static String longStr(int len)
    {
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++)
        {
            sb.append('x');
        }
        return sb.toString();
    }
}
