package com.ruoyi.credit.service;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.HashMap;
import java.util.Map;

/**
 * 规则配置类
 * 封装验证规则的配置参数
 * 
 * @author ruoyi
 * @date 2025-12-30
 */
@JsonIgnoreProperties(ignoreUnknown = true)
public class RuleConfig {
    
    /** 是否启用规则 */
    @JsonProperty("enabled")
    private boolean enabled = true;
    
    /** 失败时是否停止后续规则执行 */
    @JsonProperty("stopOnFailure")
    private boolean stopOnFailure = false;
    
    /** 规则超时时间（毫秒） */
    @JsonProperty("timeout")
    private Long timeout = 5000L;
    
    /** 最大重试次数 */
    @JsonProperty("maxRetries")
    private Integer maxRetries = 3;
    
    /** 重试间隔（毫秒） */
    @JsonProperty("retryInterval")
    private Long retryInterval = 1000L;
    
    /** 是否记录详细日志 */
    @JsonProperty("verboseLogging")
    private boolean verboseLogging = false;
    
    /** 验证阈值 */
    @JsonProperty("threshold")
    private Double threshold;
    
    /** 自定义参数 */
    @JsonProperty("customParams")
    private Map<String, Object> customParams;
    
    /** 错误处理策略 */
    @JsonProperty("errorHandling")
    private String errorHandling = "CONTINUE"; // CONTINUE, STOP, RETRY
    
    /** 缓存配置 */
    @JsonProperty("cacheConfig")
    private CacheConfig cacheConfig;
    
    /** 通知配置 */
    @JsonProperty("notificationConfig")
    private NotificationConfig notificationConfig;
    
    /**
     * 默认构造函数
     */
    public RuleConfig() {
        this.customParams = new HashMap<>();
        this.cacheConfig = new CacheConfig();
        this.notificationConfig = new NotificationConfig();
    }
    
    /**
     * 获取自定义参数
     * 
     * @param key 参数键
     * @return 参数值
     */
    public Object getCustomParam(String key) {
        return customParams.get(key);
    }
    
    /**
     * 获取自定义参数（带默认值）
     * 
     * @param key 参数键
     * @param defaultValue 默认值
     * @param <T> 参数类型
     * @return 参数值
     */
    @SuppressWarnings("unchecked")
    public <T> T getCustomParam(String key, T defaultValue) {
        Object value = customParams.get(key);
        return value != null ? (T) value : defaultValue;
    }
    
    /**
     * 设置自定义参数
     * 
     * @param key 参数键
     * @param value 参数值
     */
    public void setCustomParam(String key, Object value) {
        if (customParams == null) {
            customParams = new HashMap<>();
        }
        customParams.put(key, value);
    }
    
    /**
     * 检查是否包含自定义参数
     * 
     * @param key 参数键
     * @return 是否包含
     */
    public boolean hasCustomParam(String key) {
        return customParams != null && customParams.containsKey(key);
    }
    
    // Getters and Setters
    
    public boolean isEnabled() {
        return enabled;
    }
    
    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }
    
    public boolean isStopOnFailure() {
        return stopOnFailure;
    }
    
    public void setStopOnFailure(boolean stopOnFailure) {
        this.stopOnFailure = stopOnFailure;
    }
    
    public Long getTimeout() {
        return timeout;
    }
    
    public void setTimeout(Long timeout) {
        this.timeout = timeout;
    }
    
    public Integer getMaxRetries() {
        return maxRetries;
    }
    
    public void setMaxRetries(Integer maxRetries) {
        this.maxRetries = maxRetries;
    }
    
    public Long getRetryInterval() {
        return retryInterval;
    }
    
    public void setRetryInterval(Long retryInterval) {
        this.retryInterval = retryInterval;
    }
    
    public boolean isVerboseLogging() {
        return verboseLogging;
    }
    
    public void setVerboseLogging(boolean verboseLogging) {
        this.verboseLogging = verboseLogging;
    }
    
    public Double getThreshold() {
        return threshold;
    }
    
    public void setThreshold(Double threshold) {
        this.threshold = threshold;
    }
    
    public Map<String, Object> getCustomParams() {
        return customParams;
    }
    
    public void setCustomParams(Map<String, Object> customParams) {
        this.customParams = customParams;
    }
    
    public String getErrorHandling() {
        return errorHandling;
    }
    
    public void setErrorHandling(String errorHandling) {
        this.errorHandling = errorHandling;
    }
    
    public CacheConfig getCacheConfig() {
        return cacheConfig;
    }
    
    public void setCacheConfig(CacheConfig cacheConfig) {
        this.cacheConfig = cacheConfig;
    }
    
    public NotificationConfig getNotificationConfig() {
        return notificationConfig;
    }
    
    public void setNotificationConfig(NotificationConfig notificationConfig) {
        this.notificationConfig = notificationConfig;
    }
    
    /**
     * 缓存配置内部类
     */
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class CacheConfig {
        
        /** 是否启用缓存 */
        @JsonProperty("enabled")
        private boolean enabled = true;
        
        /** 缓存过期时间（秒） */
        @JsonProperty("expireTime")
        private Integer expireTime = 300; // 5分钟
        
        /** 缓存键前缀 */
        @JsonProperty("keyPrefix")
        private String keyPrefix = "validation:";
        
        public boolean isEnabled() {
            return enabled;
        }
        
        public void setEnabled(boolean enabled) {
            this.enabled = enabled;
        }
        
        public Integer getExpireTime() {
            return expireTime;
        }
        
        public void setExpireTime(Integer expireTime) {
            this.expireTime = expireTime;
        }
        
        public String getKeyPrefix() {
            return keyPrefix;
        }
        
        public void setKeyPrefix(String keyPrefix) {
            this.keyPrefix = keyPrefix;
        }
    }
    
    /**
     * 通知配置内部类
     */
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class NotificationConfig {
        
        /** 是否启用通知 */
        @JsonProperty("enabled")
        private boolean enabled = false;
        
        /** 通知类型 */
        @JsonProperty("notificationType")
        private String notificationType = "EMAIL"; // EMAIL, SMS, SYSTEM
        
        /** 通知模板 */
        @JsonProperty("template")
        private String template;
        
        /** 通知接收者 */
        @JsonProperty("recipients")
        private String recipients;
        
        public boolean isEnabled() {
            return enabled;
        }
        
        public void setEnabled(boolean enabled) {
            this.enabled = enabled;
        }
        
        public String getNotificationType() {
            return notificationType;
        }
        
        public void setNotificationType(String notificationType) {
            this.notificationType = notificationType;
        }
        
        public String getTemplate() {
            return template;
        }
        
        public void setTemplate(String template) {
            this.template = template;
        }
        
        public String getRecipients() {
            return recipients;
        }
        
        public void setRecipients(String recipients) {
            this.recipients = recipients;
        }
    }
    
    @Override
    public String toString() {
        return "RuleConfig{" +
                "enabled=" + enabled +
                ", stopOnFailure=" + stopOnFailure +
                ", timeout=" + timeout +
                ", maxRetries=" + maxRetries +
                ", retryInterval=" + retryInterval +
                ", verboseLogging=" + verboseLogging +
                ", threshold=" + threshold +
                ", errorHandling='" + errorHandling + '\'' +
                '}';
    }
}