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

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

import java.nio.charset.StandardCharsets;
import java.util.Arrays;

import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.test.util.ReflectionTestUtils;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.web.kb.support.KbMarkdownGenerator;
import com.ruoyi.web.modules.architecture.domain.BizDrugIncompatibilityCompanion;
import com.ruoyi.web.modules.architecture.domain.BizDrugIncompatibilityStandard;
import com.ruoyi.web.modules.architecture.mapper.BizDrugIncompatibilityCompanionMapper;

@ExtendWith(MockitoExtension.class)
@DisplayName("DrugIncompatibilityStandardKbMarkdown")
class DrugIncompatibilityStandardKbMarkdownTest
{

    @TempDir
    java.nio.file.Path tempProfile;

    @Mock
    private BizDrugIncompatibilityCompanionMapper companionMapper;

    @InjectMocks
    private DrugIncompatibilityStandardKbMarkdown kbMarkdown;

    private KbMarkdownGenerator generator;

    @BeforeEach
    void setUp()
    {
        new RuoYiConfig().setProfile(tempProfile.toString());
        generator = new KbMarkdownGenerator();
        ReflectionTestUtils.setField(kbMarkdown, "kbMarkdownGenerator", generator);
    }

    @Test
    @DisplayName("配伍药物逗号拼接")
    void formatCompanionDrugNames()
    {
        BizDrugIncompatibilityCompanion a = new BizDrugIncompatibilityCompanion();
        a.setCompanionDrugName("药A");
        BizDrugIncompatibilityCompanion b = new BizDrugIncompatibilityCompanion();
        b.setCompanionDrugName("药B");
        when(companionMapper.selectBizDrugIncompatibilityCompanionListByStandardId(1L))
                .thenReturn(Arrays.asList(a, b));
        assertEquals("药A,药B", kbMarkdown.formatCompanionDrugNames(1L));
    }

    @Test
    @DisplayName("生成 md 替换占位符")
    void generateForSync() throws Exception
    {
        BizDrugIncompatibilityStandard row = new BizDrugIncompatibilityStandard();
        row.setId(1L);
        row.setMainDrugName("青霉素");
        row.setCompatResult("禁止配伍");
        when(companionMapper.selectBizDrugIncompatibilityCompanionListByStandardId(1L))
                .thenReturn(Arrays.asList(compRow("链霉素"), compRow("庆大霉素")));

        java.nio.file.Path out = kbMarkdown.generateForSync(row);
        String content = FileUtils.readFileToString(out.toFile(), StandardCharsets.UTF_8);
        assertTrue(content.contains("青霉素"));
        assertTrue(content.contains("链霉素,庆大霉素"));
        assertTrue(content.contains("禁止配伍"));
        generator.deleteQuietly(out);
    }

    private static BizDrugIncompatibilityCompanion compRow(String name)
    {
        BizDrugIncompatibilityCompanion c = new BizDrugIncompatibilityCompanion();
        c.setCompanionDrugName(name);
        return c;
    }
}
