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

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

import java.math.BigDecimal;
import java.util.Arrays;
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.diagnosis.MedicalResourceTestSamples;
import com.ruoyi.web.modules.diagnosis.domain.BizMedicalResource;

@DisplayName("MedicalResourceValidation")
class MedicalResourceValidationTest
{

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

    @Test
    @DisplayName("手机号")
    void contactPhone()
    {
        assertThrows(ServiceException.class, () -> MedicalResourceValidation.validateContactPhone("123", true));
        assertDoesNotThrow(() -> MedicalResourceValidation.validateContactPhone("13800138000", true));
    }

    @Test
    @DisplayName("收费标准")
    void feeStandard()
    {
        assertThrows(ServiceException.class,
                () -> MedicalResourceValidation.validateFeeStandard(BigDecimal.ZERO, true));
        assertDoesNotThrow(() -> MedicalResourceValidation.validateFeeStandard(new BigDecimal("100"), true));
    }

    @Test
    @DisplayName("设备类型不要求手机")
    void equipmentNoPhone()
    {
        BizMedicalResource row = new BizMedicalResource();
        row.setResourceType(MedicalResourceRules.RESOURCE_TYPE_EQUIPMENT);
        row.setResourceName("B超仪");
        row.setPhotoFileUrl(MedicalResourceTestSamples.PHOTO_URL);
        row.setPhotoFilePath(MedicalResourceTestSamples.PHOTO_PATH);
        row.setIntroduction("设备介绍");
        row.setAffiliatedUnit("县医院");
        row.setEquipmentModel("A1");
        assertDoesNotThrow(() -> MedicalResourceValidation.validateRowForSave(row));
    }

    @Test
    @DisplayName("兽医缺收费失败")
    void vetMissingFee()
    {
        BizMedicalResource row = MedicalResourceTestSamples.sampleVetDraft();
        row.setFeeStandard(null);
        assertThrows(ServiceException.class, () -> MedicalResourceValidation.validateRowForSave(row));
    }

    @Test
    @DisplayName("已发布不可改")
    void updatePublished()
    {
        BizMedicalResource db = MedicalResourceTestSamples.samplePublishedVet();
        assertThrows(ServiceException.class, () -> MedicalResourceValidation.validateForUpdate(db));
    }

    @Test
    @DisplayName("仅已发布可下架")
    void offlineRules()
    {
        BizMedicalResource draft = MedicalResourceTestSamples.sampleVetDraft();
        assertThrows(ServiceException.class, () -> MedicalResourceValidation.validateForOffline(draft));
        assertDoesNotThrow(() -> MedicalResourceValidation.validateForOffline(
                MedicalResourceTestSamples.samplePublishedVet()));
    }

    @Test
    @DisplayName("分配账号类型与次数")
    void assignAccount()
    {
        BizMedicalResource team = MedicalResourceTestSamples.sampleTeamDraft();
        team.setId(1L);
        assertThrows(ServiceException.class, () -> MedicalResourceValidation.validateForAssignAccount(team));
        BizMedicalResource assigned = MedicalResourceTestSamples.sampleVetDraft();
        assigned.setId(1L);
        assigned.setAccountAssigned(MedicalResourceRules.ACCOUNT_ASSIGNED_YES);
        assertThrows(ServiceException.class, () -> MedicalResourceValidation.validateForAssignAccount(assigned));
    }

    @Test
    @DisplayName("团队成立时间未来")
    void establishDateFuture()
    {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.YEAR, 1);
        assertThrows(ServiceException.class,
                () -> MedicalResourceValidation.validateEstablishDate(cal.getTime(), true));
    }

    @Test
    @DisplayName("兽医不要求单日最大接诊")
    void vetNoMaxDaily()
    {
        BizMedicalResource row = MedicalResourceTestSamples.sampleVetDraft();
        assertDoesNotThrow(() -> MedicalResourceValidation.validateRowForSave(row));
        row.setMaxDailyAppointments(20);
        MedicalResourceValidation.validateRowForSave(row);
        assertNull(row.getMaxDailyAppointments());
    }

    @Test
    @DisplayName("接诊方式至少一项")
    void consultModes()
    {
        assertThrows(ServiceException.class,
                () -> MedicalResourceValidation.validateConsultModes(Arrays.asList(), true));
        assertDoesNotThrow(() -> MedicalResourceValidation.validateConsultModes(Arrays.asList(1), true));
    }
}
