package com.ruoyi.web.modules.innovation.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.innovation.TechProjectGuideTestSamples;
import com.ruoyi.web.modules.innovation.domain.BizTechProjectGuide;

/**
 * KJXM-UT-002～006
 */
@DisplayName("TechProjectGuideValidation")
class TechProjectGuideValidationTest
{

    @Test
    @DisplayName("KJXM-UT-002 项目名称长度")
    void ut002_projectName()
    {
        assertThrows(ServiceException.class, () -> TechProjectGuideValidation.validateProjectName(""));
        assertThrows(ServiceException.class, () -> TechProjectGuideValidation.validateProjectName(longStr(21)));
        assertDoesNotThrow(() -> TechProjectGuideValidation.validateProjectName(longStr(20)));
    }

    @Test
    @DisplayName("KJXM-UT-003 项目简介长度")
    void ut003_projectIntro()
    {
        assertThrows(ServiceException.class, () -> TechProjectGuideValidation.validateProjectIntro(""));
        assertThrows(ServiceException.class, () -> TechProjectGuideValidation.validateProjectIntro(longStr(51)));
        assertDoesNotThrow(() -> TechProjectGuideValidation.validateProjectIntro(longStr(50)));
    }

    @Test
    @DisplayName("KJXM-UT-004 申报日期关系")
    void ut004_applicationDates()
    {
        assertThrows(ServiceException.class, () -> TechProjectGuideValidation.validateApplicationDateRange(
                TechProjectGuideTestSamples.date(2026, Calendar.JUNE, 1),
                TechProjectGuideTestSamples.date(2026, Calendar.MAY, 1)));
        assertDoesNotThrow(() -> TechProjectGuideValidation.validateApplicationDateRange(
                TechProjectGuideTestSamples.date(2026, Calendar.JUNE, 1),
                TechProjectGuideTestSamples.date(2026, Calendar.JUNE, 1)));
    }

    @Test
    @DisplayName("KJXM-UT-005 手机号格式")
    void ut005_phone()
    {
        assertDoesNotThrow(() -> TechProjectGuideValidation.validateContactPhone("13800138000"));
        assertThrows(ServiceException.class, () -> TechProjectGuideValidation.validateContactPhone("12345"));
        assertThrows(ServiceException.class, () -> TechProjectGuideValidation.validateContactPhone(""));
    }

    @Test
    @DisplayName("KJXM-UT-006 负责人长度")
    void ut006_personInCharge()
    {
        assertThrows(ServiceException.class, () -> TechProjectGuideValidation.validatePersonInCharge(""));
        assertThrows(ServiceException.class, () -> TechProjectGuideValidation.validatePersonInCharge(longStr(11)));
        assertDoesNotThrow(() -> TechProjectGuideValidation.validatePersonInCharge(longStr(10)));
    }

    @Test
    @DisplayName("KJXM-UT-001 项目类型编码非空")
    void ut001_projectTypeCode()
    {
        assertThrows(ServiceException.class, () -> TechProjectGuideValidation.validateProjectType(null));
        assertThrows(ServiceException.class, () -> TechProjectGuideValidation.validateProjectType(""));
        assertDoesNotThrow(() -> TechProjectGuideValidation.validateProjectType("002001"));
    }

    @Test
    @DisplayName("合法行校验")
    void validRow()
    {
        assertDoesNotThrow(() -> TechProjectGuideValidation.validateRowForSave(TechProjectGuideTestSamples.sampleDraft()));
    }

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