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

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

import java.util.Date;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.technology.PracticalTrainingTestSamples;
import com.ruoyi.web.modules.technology.domain.BizPracticalTraining;

@DisplayName("PracticalTrainingValidation")
class PracticalTrainingValidationTest
{

    @Test
    @DisplayName("时间链合法与非法")
    void timeChain()
    {
        Date start = parse("2025-08-01 08:00:00");
        Date end = parse("2025-08-15 18:00:00");
        Date training = parse("2025-08-25 09:00:00");
        assertDoesNotThrow(() -> PracticalTrainingValidation.validateTimeChain(start, end, training));
        assertThrows(ServiceException.class,
                () -> PracticalTrainingValidation.validateTimeChain(end, start, training));
        assertThrows(ServiceException.class,
                () -> PracticalTrainingValidation.validateTimeChain(start, training, end));
    }

    @Test
    @DisplayName("培训主题长度")
    void trainingTopic()
    {
        assertThrows(ServiceException.class, () -> PracticalTrainingValidation.validateTrainingTopic(""));
        assertThrows(ServiceException.class, () -> PracticalTrainingValidation.validateTrainingTopic(longStr(21)));
        assertDoesNotThrow(() -> PracticalTrainingValidation.validateTrainingTopic("牧业实战一期"));
    }

    @Test
    @DisplayName("培训简介 trim")
    void trainingIntro()
    {
        BizPracticalTraining row = new BizPracticalTraining();
        row.setTrainingTopic("主题");
        row.setTrainingIntro("   ");
        row.setPlannedHeadCount(10);
        row.setTrainingLocation("地点");
        row.setRegistrationStartTime(parse("2025-08-01 08:00:00"));
        row.setRegistrationEndTime(parse("2025-08-15 18:00:00"));
        row.setTrainingTime(parse("2025-08-25 09:00:00"));
        assertThrows(ServiceException.class, () -> PracticalTrainingValidation.validateRowForSave(row));
    }

    @Test
    @DisplayName("计划人数")
    void plannedHeadCount()
    {
        assertThrows(ServiceException.class, () -> PracticalTrainingValidation.validatePlannedHeadCount(0));
        assertDoesNotThrow(() -> PracticalTrainingValidation.validatePlannedHeadCount(30));
    }

    @Test
    @DisplayName("列表日期区间")
    void listDateRange()
    {
        assertThrows(ServiceException.class,
                () -> PracticalTrainingValidation.validateListDateRange("2025-09-01", "2025-08-01"));
        assertDoesNotThrow(() -> PracticalTrainingValidation.validateListDateRange("2025-08-01", "2025-08-31"));
    }

    @Test
    @DisplayName("下架：报名开始后不可下架")
    void offlineAfterRegStart()
    {
        BizPracticalTraining db = PracticalTrainingTestSamples.sampleDraft();
        db.setPublishStatus(PracticalTrainingRules.PUBLISH_STATUS_PUBLISHED);
        db.setRegistrationStartTime(parse("2020-01-01 08:00:00"));
        assertThrows(ServiceException.class,
                () -> PracticalTrainingValidation.validateOffline(db, new Date()));
    }

    private static Date parse(String text)
    {
        try
        {
            return new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(text);
        }
        catch (Exception ex)
        {
            throw new RuntimeException(ex);
        }
    }

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