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

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

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.industrypolicy.domain.BizSubsidyProject;

/**
 * ZCZX-BTXM-UT-001～005
 */
@DisplayName("SubsidyProjectStatusResolver")
class SubsidyProjectStatusResolverTest
{
    @Test
    @DisplayName("ZCZX-BTXM-UT-001 T<开始日 → 未开始")
    void ut001_notStarted()
    {
        Date today = daysFromNow(0);
        assertEquals(SubsidyProjectRules.EXEC_NOT_STARTED,
                SubsidyProjectStatusResolver.resolveExecStatus(daysFromNow(1), daysFromNow(2), today));
    }

    @Test
    @DisplayName("ZCZX-BTXM-UT-002 进行中区间")
    void ut002_inProgress()
    {
        Date today = daysFromNow(0);
        assertEquals(SubsidyProjectRules.EXEC_IN_PROGRESS,
                SubsidyProjectStatusResolver.resolveExecStatus(daysFromNow(-1), daysFromNow(1), today));
    }

    @Test
    @DisplayName("ZCZX-BTXM-UT-003 T=预计完成日 → 已完成")
    void ut003_completedOnEndDay()
    {
        Date today = daysFromNow(0);
        assertEquals(SubsidyProjectRules.EXEC_COMPLETED,
                SubsidyProjectStatusResolver.resolveExecStatus(daysFromNow(-30), today, today));
    }

    @Test
    @DisplayName("ZCZX-BTXM-UT-003A T>预计完成日 → 仍为进行中")
    void ut003a_overdueStillInProgress()
    {
        Date today = daysFromNow(0);
        assertEquals(SubsidyProjectRules.EXEC_IN_PROGRESS,
                SubsidyProjectStatusResolver.resolveExecStatus(daysFromNow(-30), daysFromNow(-1), today));
    }

    @Test
    @DisplayName("ZCZX-BTXM-UT-004 手工锁定不自动推算")
    void ut004_manualLock()
    {
        BizSubsidyProject row = new BizSubsidyProject();
        row.setManualCompleteFlag(SubsidyProjectRules.MANUAL_FLAG_LOCKED);
        row.setExecStatus(SubsidyProjectRules.EXEC_COMPLETED);
        row.setProjectStartDate(daysFromNow(-30));
        row.setPlannedEndDate(daysFromNow(30));
        row.setActualEndDate(daysFromNow(-1));
        SubsidyProjectStatusResolver.applyAutoResolution(row, daysFromNow(0));
        assertEquals(SubsidyProjectRules.EXEC_COMPLETED, row.getExecStatus());
        assertEquals(daysFromNow(-1), row.getActualEndDate());
    }

    @Test
    @DisplayName("自动完成默认实际完成日为当天")
    void autoCompleteSetsActualEnd()
    {
        Date today = daysFromNow(0);
        BizSubsidyProject row = new BizSubsidyProject();
        row.setManualCompleteFlag(SubsidyProjectRules.MANUAL_FLAG_AUTO);
        row.setProjectStartDate(daysFromNow(-30));
        row.setPlannedEndDate(today);
        SubsidyProjectStatusResolver.applyAutoResolution(row, today);
        assertEquals(SubsidyProjectRules.EXEC_COMPLETED, row.getExecStatus());
        assertEquals(today, row.getActualEndDate());
    }

    @Test
    @DisplayName("未开始清空实际完成日")
    void notStartedClearsActual()
    {
        BizSubsidyProject row = new BizSubsidyProject();
        row.setManualCompleteFlag(SubsidyProjectRules.MANUAL_FLAG_AUTO);
        row.setProjectStartDate(daysFromNow(1));
        row.setPlannedEndDate(daysFromNow(10));
        row.setActualEndDate(daysFromNow(0));
        SubsidyProjectStatusResolver.applyAutoResolution(row, daysFromNow(0));
        assertEquals(SubsidyProjectRules.EXEC_NOT_STARTED, row.getExecStatus());
        assertNull(row.getActualEndDate());
    }

    private static Date daysFromNow(int delta)
    {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, delta);
        return SubsidyProjectStatusResolver.truncateToDay(cal.getTime());
    }
}
