package com.ruoyi.web.modules.architecture.service.impl;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.architecture.domain.AppMedicationCompatibilityCheckVo;
import com.ruoyi.web.modules.architecture.mapper.AppMedicationDrugMapper;

@ExtendWith(MockitoExtension.class)
@DisplayName("AppMedicationCompatibilityServiceImpl")
class AppMedicationCompatibilityServiceImplTest
{
    @Mock
    private AppMedicationDrugMapper appMedicationDrugMapper;

    @InjectMocks
    private AppMedicationCompatibilityServiceImpl service;

    @Test
    @DisplayName("药物1主药药物2配伍有禁忌")
    void incompatibilityDir1()
    {
        when(appMedicationDrugMapper.countIncompatibilityPair("青霉素", "链霉素")).thenReturn(1);
        when(appMedicationDrugMapper.countIncompatibilityPair("链霉素", "青霉素")).thenReturn(0);
        when(appMedicationDrugMapper.selectCompatResultByPair("青霉素", "链霉素")).thenReturn(" 禁止合用 ");

        AppMedicationCompatibilityCheckVo vo = service.checkCompatibility("青霉素", "链霉素");
        assertTrue(vo.getHasIncompatibility());
        assertEquals("禁止合用", vo.getCompatResult());
    }

    @Test
    @DisplayName("仅药物2主药方向有禁忌")
    void incompatibilityDir2()
    {
        when(appMedicationDrugMapper.countIncompatibilityPair("药A", "药B")).thenReturn(0);
        when(appMedicationDrugMapper.countIncompatibilityPair("药B", "药A")).thenReturn(2);
        when(appMedicationDrugMapper.selectCompatResultByPair("药B", "药A")).thenReturn("不宜联用");

        AppMedicationCompatibilityCheckVo vo = service.checkCompatibility("药A", "药B");
        assertTrue(vo.getHasIncompatibility());
        assertEquals("不宜联用", vo.getCompatResult());
    }

    @Test
    @DisplayName("双向无禁忌")
    void noIncompatibility()
    {
        when(appMedicationDrugMapper.countIncompatibilityPair("药A", "药B")).thenReturn(0);
        when(appMedicationDrugMapper.countIncompatibilityPair("药B", "药A")).thenReturn(0);

        AppMedicationCompatibilityCheckVo vo = service.checkCompatibility("药A", "药B");
        assertFalse(vo.getHasIncompatibility());
        assertNull(vo.getCompatResult());
    }

    @Test
    @DisplayName("相同药物禁止配伍")
    void sameDrug()
    {
        assertThrows(ServiceException.class, () -> service.checkCompatibility("青霉素", "青霉素"));
    }
}
