package com.ruoyi.credit.config;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import com.ruoyi.credit.service.IValidationLogService;

/**
 * 验证日志配置类
 * 
 * @author ruoyi
 * @date 2025-12-31
 */
@Configuration
@ConfigurationProperties(prefix = "validation.logging")
public class ValidationLoggingConfig 
{
    private static final Logger log = LoggerFactory.getLogger(ValidationLoggingConfig.class);

    @Autowired
    private IValidationLogService validationLogService;

    /** 是否启用验证日志 */
    private boolean enabled = true;

    /** 是否启用异步日志 */
    private boolean asyncEnabled = true;

    /** 日志级别 */
    private String logLevel = "INFO";

    /** 是否启用性能日志 */
    private boolean performanceLogEnabled = true;

    /** 性能警告阈值（毫秒） */
    private long performanceWarningThreshold = 2000L;

    /** 是否启用异常日志 */
    private boolean exceptionLogEnabled = true;

    /** 是否启用缓存操作日志 */
    private boolean cacheLogEnabled = true;

    /** 是否启用通知日志 */
    private boolean notificationLogEnabled = true;

    /** 日志保留天数 */
    private int logRetentionDays = 30;

    /** 是否启用日志清理 */
    private boolean logCleanupEnabled = true;

    /** 日志清理间隔（小时） */
    private int logCleanupInterval = 24;

    /** 批量插入日志的大小 */
    private int batchInsertSize = 100;

    /** 是否启用日志压缩 */
    private boolean logCompressionEnabled = true;

    /** 日志文件最大大小（MB） */
    private int maxLogFileSize = 100;

    /** 日志文件最大数量 */
    private int maxLogFileCount = 10;

    @PostConstruct
    public void init() {
        log.info("初始化验证日志配置");
        log.info("验证日志启用状态: {}", enabled);
        log.info("异步日志启用状态: {}", asyncEnabled);
        log.info("日志级别: {}", logLevel);
        log.info("性能日志启用状态: {}", performanceLogEnabled);
        log.info("性能警告阈值: {}ms", performanceWarningThreshold);
        log.info("日志保留天数: {}", logRetentionDays);
        log.info("批量插入大小: {}", batchInsertSize);
        
        // 验证配置的合理性
        validateConfiguration();
    }

    /**
     * 验证配置的合理性
     */
    private void validateConfiguration() {
        if (performanceWarningThreshold <= 0) {
            log.warn("性能警告阈值配置不合理，使用默认值: 2000ms");
            performanceWarningThreshold = 2000L;
        }
        
        if (logRetentionDays <= 0) {
            log.warn("日志保留天数配置不合理，使用默认值: 30天");
            logRetentionDays = 30;
        }
        
        if (batchInsertSize <= 0 || batchInsertSize > 1000) {
            log.warn("批量插入大小配置不合理，使用默认值: 100");
            batchInsertSize = 100;
        }
        
        if (maxLogFileSize <= 0) {
            log.warn("日志文件最大大小配置不合理，使用默认值: 100MB");
            maxLogFileSize = 100;
        }
        
        if (maxLogFileCount <= 0) {
            log.warn("日志文件最大数量配置不合理，使用默认值: 10");
            maxLogFileCount = 10;
        }
    }

    /**
     * 检查是否应该记录指定级别的日志
     * 
     * @param level 日志级别
     * @return 是否应该记录
     */
    public boolean shouldLog(String level) {
        if (!enabled) {
            return false;
        }
        
        // 简单的日志级别检查
        switch (logLevel.toUpperCase()) {
            case "DEBUG":
                return true;
            case "INFO":
                return !"DEBUG".equalsIgnoreCase(level);
            case "WARN":
                return "WARN".equalsIgnoreCase(level) || "ERROR".equalsIgnoreCase(level);
            case "ERROR":
                return "ERROR".equalsIgnoreCase(level);
            default:
                return true;
        }
    }

    /**
     * 检查是否应该记录性能日志
     * 
     * @param duration 执行时长
     * @return 是否应该记录
     */
    public boolean shouldLogPerformance(long duration) {
        return performanceLogEnabled && duration >= performanceWarningThreshold;
    }

    // Getters and Setters
    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public boolean isAsyncEnabled() {
        return asyncEnabled;
    }

    public void setAsyncEnabled(boolean asyncEnabled) {
        this.asyncEnabled = asyncEnabled;
    }

    public String getLogLevel() {
        return logLevel;
    }

    public void setLogLevel(String logLevel) {
        this.logLevel = logLevel;
    }

    public boolean isPerformanceLogEnabled() {
        return performanceLogEnabled;
    }

    public void setPerformanceLogEnabled(boolean performanceLogEnabled) {
        this.performanceLogEnabled = performanceLogEnabled;
    }

    public long getPerformanceWarningThreshold() {
        return performanceWarningThreshold;
    }

    public void setPerformanceWarningThreshold(long performanceWarningThreshold) {
        this.performanceWarningThreshold = performanceWarningThreshold;
    }

    public boolean isExceptionLogEnabled() {
        return exceptionLogEnabled;
    }

    public void setExceptionLogEnabled(boolean exceptionLogEnabled) {
        this.exceptionLogEnabled = exceptionLogEnabled;
    }

    public boolean isCacheLogEnabled() {
        return cacheLogEnabled;
    }

    public void setCacheLogEnabled(boolean cacheLogEnabled) {
        this.cacheLogEnabled = cacheLogEnabled;
    }

    public boolean isNotificationLogEnabled() {
        return notificationLogEnabled;
    }

    public void setNotificationLogEnabled(boolean notificationLogEnabled) {
        this.notificationLogEnabled = notificationLogEnabled;
    }

    public int getLogRetentionDays() {
        return logRetentionDays;
    }

    public void setLogRetentionDays(int logRetentionDays) {
        this.logRetentionDays = logRetentionDays;
    }

    public boolean isLogCleanupEnabled() {
        return logCleanupEnabled;
    }

    public void setLogCleanupEnabled(boolean logCleanupEnabled) {
        this.logCleanupEnabled = logCleanupEnabled;
    }

    public int getLogCleanupInterval() {
        return logCleanupInterval;
    }

    public void setLogCleanupInterval(int logCleanupInterval) {
        this.logCleanupInterval = logCleanupInterval;
    }

    public int getBatchInsertSize() {
        return batchInsertSize;
    }

    public void setBatchInsertSize(int batchInsertSize) {
        this.batchInsertSize = batchInsertSize;
    }

    public boolean isLogCompressionEnabled() {
        return logCompressionEnabled;
    }

    public void setLogCompressionEnabled(boolean logCompressionEnabled) {
        this.logCompressionEnabled = logCompressionEnabled;
    }

    public int getMaxLogFileSize() {
        return maxLogFileSize;
    }

    public void setMaxLogFileSize(int maxLogFileSize) {
        this.maxLogFileSize = maxLogFileSize;
    }

    public int getMaxLogFileCount() {
        return maxLogFileCount;
    }

    public void setMaxLogFileCount(int maxLogFileCount) {
        this.maxLogFileCount = maxLogFileCount;
    }
}