package com.ruoyi.web.kb;

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

import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.LinkedHashMap;
import java.util.Map;

import com.ruoyi.web.kb.support.KbMarkdownGenerator;
import com.ruoyi.web.modules.architecture.support.WithdrawalPeriodStandardKbMarkdown;
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.io.TempDir;
import com.ruoyi.common.config.RuoYiConfig;

@DisplayName("KbMarkdownGenerator")
class KbMarkdownGeneratorTest
{

    @TempDir
    Path tempProfile;

    private KbMarkdownGenerator generator;

    @BeforeEach
    void setUp()
    {
        new RuoYiConfig().setProfile(tempProfile.toString());
        generator = new KbMarkdownGenerator();
    }

    @Test
    @DisplayName("按模板生成休药期 Markdown 并替换占位符")
    void generateFromTemplate() throws Exception
    {
        Map<String, String> vars = new LinkedHashMap<>();
        vars.put("drugName", "青霉素");
        vars.put("drugType", "抗生素");
        vars.put("withdrawalDays", "7");
        vars.put("usageGuide", "用法说明");
        vars.put("drugEffect", "杀菌");

        Path out = generator.generateFromTemplate(
                WithdrawalPeriodStandardKbMarkdown.TEMPLATE_FILE,
                "休药期标准_1.md",
                vars);

        assertTrue(Files.isRegularFile(out));
        String content = FileUtils.readFileToString(out.toFile(), StandardCharsets.UTF_8);
        assertTrue(content.contains("青霉素"));
        assertTrue(content.contains("抗生素"));
        assertTrue(content.contains("7"));
        assertTrue(content.contains("用法说明"));
        assertTrue(content.contains("杀菌"));
        assertEquals(false, content.contains("{drugName}"));

        generator.deleteQuietly(out);
        assertEquals(false, Files.exists(out));
    }
}
