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

import static org.junit.jupiter.api.Assertions.assertEquals;

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

@DisplayName("PracticalTrainingEnrollStatusResolver")
class PracticalTrainingEnrollStatusResolverTest
{

    @Test
    @DisplayName("未发布均为未开始")
    void draftNotStarted()
    {
        BizPracticalTraining row = PracticalTrainingTestSamples.sampleDraft();
        assertEquals(PracticalTrainingRules.ENROLL_STATUS_NOT_STARTED,
                PracticalTrainingEnrollStatusResolver.resolveEnrollStatus(row, new Date()));
    }

    @Test
    @DisplayName("已发布报名未开始")
    void publishedBeforeReg()
    {
        BizPracticalTraining row = PracticalTrainingTestSamples.samplePublishedFutureReg();
        Date now = new Date();
        assertEquals(PracticalTrainingRules.ENROLL_STATUS_NOT_STARTED,
                PracticalTrainingEnrollStatusResolver.resolveEnrollStatus(row, now));
    }

    @Test
    @DisplayName("报名中")
    void enrolling()
    {
        BizPracticalTraining row = PracticalTrainingTestSamples.sampleDraft();
        row.setPublishStatus(PracticalTrainingRules.PUBLISH_STATUS_PUBLISHED);
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.HOUR, -1);
        row.setRegistrationStartTime(cal.getTime());
        cal.add(Calendar.DAY_OF_MONTH, 7);
        row.setRegistrationEndTime(cal.getTime());
        cal.add(Calendar.DAY_OF_MONTH, 7);
        row.setTrainingTime(cal.getTime());
        row.setActualEnrolledCount(5);
        row.setPlannedHeadCount(20);
        assertEquals(PracticalTrainingRules.ENROLL_STATUS_ENROLLING,
                PracticalTrainingEnrollStatusResolver.resolveEnrollStatus(row, new Date()));
    }

    @Test
    @DisplayName("已报满")
    void full()
    {
        BizPracticalTraining row = PracticalTrainingTestSamples.sampleDraft();
        row.setPublishStatus(PracticalTrainingRules.PUBLISH_STATUS_PUBLISHED);
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.HOUR, -1);
        row.setRegistrationStartTime(cal.getTime());
        cal.add(Calendar.DAY_OF_MONTH, 7);
        row.setRegistrationEndTime(cal.getTime());
        cal.add(Calendar.DAY_OF_MONTH, 7);
        row.setTrainingTime(cal.getTime());
        row.setActualEnrolledCount(20);
        row.setPlannedHeadCount(20);
        assertEquals(PracticalTrainingRules.ENROLL_STATUS_FULL,
                PracticalTrainingEnrollStatusResolver.resolveEnrollStatus(row, new Date()));
    }

    @Test
    @DisplayName("已截止")
    void closed()
    {
        BizPracticalTraining row = PracticalTrainingTestSamples.sampleDraft();
        row.setPublishStatus(PracticalTrainingRules.PUBLISH_STATUS_PUBLISHED);
        row.setRegistrationStartTime(parse("2020-01-01 08:00:00"));
        row.setRegistrationEndTime(parse("2020-01-15 18:00:00"));
        row.setTrainingTime(parse("2020-02-01 09:00:00"));
        assertEquals(PracticalTrainingRules.ENROLL_STATUS_CLOSED,
                PracticalTrainingEnrollStatusResolver.resolveEnrollStatus(row, new Date()));
    }

    @Test
    @DisplayName("截止时刻为已截止")
    void closedAtEndBoundary()
    {
        BizPracticalTraining row = PracticalTrainingTestSamples.sampleDraft();
        row.setPublishStatus(PracticalTrainingRules.PUBLISH_STATUS_PUBLISHED);
        Date end = parse("2026-06-01 18:00:00");
        row.setRegistrationStartTime(parse("2026-05-01 08:00:00"));
        row.setRegistrationEndTime(end);
        row.setTrainingTime(parse("2026-06-10 09:00:00"));
        row.setActualEnrolledCount(0);
        row.setPlannedHeadCount(30);
        assertEquals(PracticalTrainingRules.TRAINING_STATUS_CLOSED,
                PracticalTrainingEnrollStatusResolver.resolveTrainingStatus(row, end));
    }

    @Test
    @DisplayName("培训状态编码")
    void trainingStatusCodes()
    {
        BizPracticalTraining row = PracticalTrainingTestSamples.sampleDraft();
        row.setPublishStatus(PracticalTrainingRules.PUBLISH_STATUS_PUBLISHED);
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.HOUR, -1);
        row.setRegistrationStartTime(cal.getTime());
        cal.add(Calendar.DAY_OF_MONTH, 7);
        row.setRegistrationEndTime(cal.getTime());
        row.setActualEnrolledCount(20);
        row.setPlannedHeadCount(20);
        assertEquals(PracticalTrainingRules.TRAINING_STATUS_FULL,
                PracticalTrainingEnrollStatusResolver.resolveTrainingStatus(row, 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);
        }
    }
}
