package com.aicheck.config;

import org.yaml.snakeyaml.Yaml;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 读取 .gitea/config.yaml，提供检测开关、扫描目录、企微配置等。
 */
public class AppConfig {
    private boolean enabled = true;
    private boolean dtoEntityConversionEnabled = true;
    private List<String> modelDirs = new ArrayList<>();
    private List<String> controllerScanDirs = new ArrayList<>();
    private List<String> feignScanDirs = new ArrayList<>();
    private List<String> conversionScanDirs = new ArrayList<>();
    private String wecomWebhookUrl = "";
    private boolean wecomEnabled = true;
    private boolean onlyOnChange = true;

    /** 从 YAML 文件加载配置 */
    @SuppressWarnings("unchecked")
    public static AppConfig load(Path configPath) throws IOException {
        Yaml yaml = new Yaml();
        Map<String, Object> root;
        try (InputStream in = Files.newInputStream(configPath)) {
            root = yaml.load(in);
        }
        if (root == null) {
            root = Map.of();
        }

        AppConfig config = new AppConfig();
        Map<String, Object> classCheck = mapOrEmpty(root.get("class_check"));
        config.enabled = boolOrDefault(classCheck.get("enabled"), true);

        Map<String, Object> conversion = mapOrEmpty(classCheck.get("dto_entity_conversion"));
        config.dtoEntityConversionEnabled = boolOrDefault(conversion.get("enabled"), true);

        config.modelDirs = stringList(classCheck.get("model_dirs"));
        Map<String, Object> endpointScan = mapOrEmpty(classCheck.get("endpoint_scan"));
        config.controllerScanDirs = stringList(endpointScan.get("controllers"));
        config.feignScanDirs = stringList(endpointScan.get("feign_apis"));
        config.conversionScanDirs = stringList(classCheck.get("conversion_scan"));

        Map<String, Object> wecom = mapOrEmpty(root.get("wecom"));
        config.wecomWebhookUrl = stringOrEmpty(wecom.get("webhook_url"));
        config.wecomEnabled = boolOrDefault(wecom.get("enabled"), true);

        Map<String, Object> notify = mapOrEmpty(root.get("notify"));
        config.onlyOnChange = boolOrDefault(notify.get("only_on_change"), true);

        return config;
    }

    /** 安全转为 Map，非 Map 则返回空 Map */
    @SuppressWarnings("unchecked")
    private static Map<String, Object> mapOrEmpty(Object value) {
        if (value instanceof Map) {
            return (Map<String, Object>) value;
        }
        return Map.of();
    }

    /** 安全转为字符串列表 */
    @SuppressWarnings("unchecked")
    private static List<String> stringList(Object value) {
        if (value instanceof List) {
            List<?> list = (List<?>) value;
            List<String> result = new ArrayList<>();
            for (Object item : list) {
                if (item != null) {
                    result.add(item.toString());
                }
            }
            return result;
        }
        return new ArrayList<>();
    }

    /** 安全转为 boolean，缺省用 defaultValue */
    private static boolean boolOrDefault(Object value, boolean defaultValue) {
        if (value instanceof Boolean) {
            return (Boolean) value;
        }
        return defaultValue;
    }

    /** 安全转为字符串，null 则空串 */
    private static String stringOrEmpty(Object value) {
        return value == null ? "" : value.toString();
    }

    /** 类变更检测总开关 */
    public boolean isEnabled() {
        return enabled;
    }

    /** Dto→Entity 类转换检测开关 */
    public boolean isDtoEntityConversionEnabled() {
        return dtoEntityConversionEnabled;
    }

    /** 模型类目录（预留，当前扫描仍按类名后缀） */
    public List<String> getModelDirs() {
        return modelDirs;
    }

    /** Controller 扫描目录 */
    public List<String> getControllerScanDirs() {
        return controllerScanDirs;
    }

    /** Feign 接口扫描目录 */
    public List<String> getFeignScanDirs() {
        return feignScanDirs;
    }

    /** BeanUtils / convert 扫描目录 */
    public List<String> getConversionScanDirs() {
        return conversionScanDirs;
    }

    /** 企微 Webhook 地址 */
    public String getWecomWebhookUrl() {
        return wecomWebhookUrl;
    }

    /** 企微通知开关 */
    public boolean isWecomEnabled() {
        return wecomEnabled;
    }

    /** 无变更时是否打印提示后退出 */
    public boolean isOnlyOnChange() {
        return onlyOnChange;
    }
}
