package com.jeesharp.business.modules.contentsecurity.config;

import com.jeesharp.business.modules.contentsecurity.enums.CheckDecisionEnum;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * 内容安全模块配置，支持按类型路由 Provider 及异常降级策略。
 *
 * @author jeesharp
 * @since 2026-06-27
 */
@Data
@Component
@ConfigurationProperties(prefix = "jeesharp.content-security")
public class ContentSecurityProperties {

    /** 总开关，false 时跳过检测（仅开发环境建议关闭） */
    private boolean enabled = true;

    /** 默认 Provider 编码，见 ContentSecurityProviderCodeEnum */
    private String defaultProvider = "WECHAT_MINI";

    /** 微信检测缺少 openid 时的降级结论：REVIEW 或 REJECT */
    private String onMissingOpenid = "REVIEW";

    /** Provider 异常时的降级结论：REVIEW 或 REJECT */
    private String onProviderError = "REVIEW";

    /** 文本检测 Provider 链（当前取第一个可用） */
    private java.util.List<String> textProviders = java.util.List.of("WECHAT_MINI");

    /** 图片检测 Provider 链 */
    private java.util.List<String> imageProviders = java.util.List.of("WECHAT_MINI");

    /** 图片是否走微信 mediaCheckAsync（true=异步+PENDING；false=同步 img_sec_check） */
    private boolean mediaAsyncEnabled = true;

    /** 异步提交失败时是否降级为同步 img_sec_check */
    private boolean mediaAsyncFallbackSync = true;

    public CheckDecisionEnum resolveMissingOpenidDecision() {
        return parseDecision(onMissingOpenid, CheckDecisionEnum.REVIEW);
    }

    public CheckDecisionEnum resolveProviderErrorDecision() {
        return parseDecision(onProviderError, CheckDecisionEnum.REVIEW);
    }

    private static CheckDecisionEnum parseDecision(String raw, CheckDecisionEnum fallback) {
        CheckDecisionEnum d = CheckDecisionEnum.fromCode(raw);
        return d != null ? d : fallback;
    }
}
