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

import java.nio.file.Path;
import java.util.LinkedHashMap;
import java.util.Map;

import com.ruoyi.web.kb.support.KbMarkdownGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.architecture.domain.BizWithdrawalPeriodStandard;

/**
 * 休药期标准同步知识库前，按模板生成 Markdown 文件。
 */
@Component
public class WithdrawalPeriodStandardKbMarkdown
{
    public static final String TEMPLATE_FILE = "biz_withdrawal_period_standard.md";

    public static final String OUTPUT_FILE_PREFIX = "休药期标准_";

    @Autowired
    private KbMarkdownGenerator kbMarkdownGenerator;

    /**
     * 生成 {@code {profile}/template/休药期标准_{id}.md}。
     */
    public Path generateForSync(BizWithdrawalPeriodStandard row)
    {
        if (row == null || row.getId() == null)
        {
            throw new ServiceException("休药期标准主键为空，无法生成知识库 Markdown");
        }
        Map<String, String> placeholders = new LinkedHashMap<>();
        placeholders.put("drugName", row.getDrugName());
        placeholders.put("drugType", resolveDrugTypeLabel(row.getDrugType()));
        placeholders.put("withdrawalDays", row.getWithdrawalDays() != null ? String.valueOf(row.getWithdrawalDays()) : "");
        placeholders.put("usageGuide", row.getUsageGuide());
        placeholders.put("drugEffect", row.getDrugEffect());
        String outputName = OUTPUT_FILE_PREFIX + row.getId() + ".md";
        return kbMarkdownGenerator.generateFromTemplate(TEMPLATE_FILE, outputName, placeholders);
    }

    /**
     * 药物类型：1 抗生素、2 抗原虫、3 抗病毒、4 激素。
     */
    public static String resolveDrugTypeLabel(Integer drugType)
    {
        if (drugType == null)
        {
            return "";
        }
        switch (drugType)
        {
            case 1:
                return "抗生素";
            case 2:
                return "抗原虫";
            case 3:
                return "抗病毒";
            case 4:
                return "激素";
            default:
                return String.valueOf(drugType);
        }
    }
}
