package com.ltgk.common.config.sensitiveWord;


import com.github.houbb.sensitive.word.api.IWordAllow;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 敏感词-允许的内容-返回的内容不被当作敏感词
 *
 * @author Qi ChengBin
 * @date 2025/02/11
 */
@Slf4j
@Component
public class MyDbWordAllow implements IWordAllow {

    @Value("${sensitive.allowFile}")
    private String sensitiveWordAllowFilePath;

    @Override
    public List<String> allow() {

        File file = new File(sensitiveWordAllowFilePath);

        Set<String> allowWordSet = new HashSet<>();

        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
            String line;
            while ((line = reader.readLine()) != null) {
                // 忽略空行或空白行
                if (!line.trim().isEmpty()) {
                    allowWordSet.add(line);
                }
            }
        } catch (IOException e){
            // 处理 IO 异常
            log.error("读取敏感词白名单文件失败！", e);
        }
        log.info("读取敏感词白名单文件读取成功！数据为：{}", allowWordSet);

        return new ArrayList<>(allowWordSet);
    }
}
