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

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

import java.math.BigDecimal;
import java.util.Arrays;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.farming.TechResourceTestSamples;
import com.ruoyi.web.modules.farming.domain.BizTechResource;

@DisplayName("TechResourceValidation")
class TechResourceValidationTest
{

    @Test
    @DisplayName("服务时段")
    void serviceTimeRange()
    {
        assertThrows(ServiceException.class,
                () -> TechResourceValidation.validateServiceTimeRange("18:00", "08:00", true));
        assertDoesNotThrow(() -> TechResourceValidation.validateServiceTimeRange("08:00", "18:00", true));
    }

    @Test
    @DisplayName("成果缺摘要失败")
    void achievementMissingSummary()
    {
        BizTechResource row = TechResourceTestSamples.sampleAchievementDraft();
        row.setAchievementSummary(null);
        assertThrows(ServiceException.class, () -> TechResourceValidation.validateRowForSave(row));
    }

    @Test
    @DisplayName("课程缺视频失败")
    void videoMissingFile()
    {
        BizTechResource row = TechResourceTestSamples.sampleVideoDraft();
        row.setVideoFilePath(null);
        assertThrows(ServiceException.class, () -> TechResourceValidation.validateRowForSave(row));
    }

    @Test
    @DisplayName("借用金额须为正数")
    void borrowFee()
    {
        assertThrows(ServiceException.class,
                () -> TechResourceValidation.validatePositiveAmount(BigDecimal.ZERO, "借用金额", true));
    }

    @Test
    @DisplayName("类型字段矩阵")
    void typeMatrix()
    {
        BizTechResource achievement = TechResourceTestSamples.sampleAchievementDraft();
        assertDoesNotThrow(() -> TechResourceValidation.validateRowForSave(achievement));
        BizTechResource expert = TechResourceTestSamples.sampleExpertDraft();
        expert.setFeeStandard(null);
        assertThrows(ServiceException.class, () -> TechResourceValidation.validateRowForSave(expert));
    }

    @Test
    @DisplayName("已发布不可改")
    void updatePublished()
    {
        assertThrows(ServiceException.class,
                () -> TechResourceValidation.validateForUpdate(TechResourceTestSamples.samplePublishedExpert()));
    }

    @Test
    @DisplayName("分配账号仅专家")
    void assignAccount()
    {
        BizTechResource achievement = TechResourceTestSamples.sampleAchievementDraft();
        achievement.setId(1L);
        assertThrows(ServiceException.class, () -> TechResourceValidation.validateForAssignAccount(achievement));
    }

    @Test
    @DisplayName("已分配不可重复")
    void assignAccountAlready()
    {
        BizTechResource expert = TechResourceTestSamples.sampleExpertDraft();
        expert.setAccountAssigned(TechResourceRules.ACCOUNT_ASSIGNED_YES);
        assertThrows(ServiceException.class, () -> TechResourceValidation.validateForAssignAccount(expert));
    }
}
