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.LoanValidationNotificationService;

/**
 * 贷款验证通知配置类
 * 
 * @author ruoyi
 * @date 2025-12-31
 */
@Configuration
@ConfigurationProperties(prefix = "loan.validation.notification")
public class LoanValidationNotificationConfig 
{
    private static final Logger log = LoggerFactory.getLogger(LoanValidationNotificationConfig.class);

    @Autowired
    private LoanValidationNotificationService loanValidationNotificationService;

    /**
     * 是否启用通知功能
     */
    private boolean enabled = true;

    /**
     * 是否启用异步通知
     */
    private boolean asyncEnabled = true;

    /**
     * 通知重试次数
     */
    private int retryCount = 3;

    /**
     * 通知超时时间（毫秒）
     */
    private long timeoutMs = 30000L;

    /**
     * 默认通知渠道
     */
    private String defaultChannel = "EMAIL";

    /**
     * 管理员邮箱
     */
    private String adminEmail = "admin@example.com";

    /**
     * 管理员电话
     */
    private String adminPhone = "13800138000";

    /**
     * 是否启用短信通知
     */
    private boolean smsEnabled = true;

    /**
     * 是否启用邮件通知
     */
    private boolean emailEnabled = true;

    /**
     * 是否启用系统内通知
     */
    private boolean systemNotificationEnabled = true;

    /**
     * 高优先级失败阈值（验证得分）
     */
    private int highPriorityFailureThreshold = 20;

    /**
     * 通知发送间隔（毫秒）- 防止频繁发送
     */
    private long notificationIntervalMs = 300000L; // 5分钟

    /**
     * 批量通知大小
     */
    private int batchNotificationSize = 50;

    /**
     * 是否在系统启动时初始化通知模板
     */
    private boolean initTemplatesOnStartup = true;

    @PostConstruct
    public void init() {
        log.info("初始化贷款验证通知配置");
        log.info("通知功能启用状态: {}", enabled);
        log.info("异步通知启用状态: {}", asyncEnabled);
        log.info("默认通知渠道: {}", defaultChannel);
        log.info("管理员邮箱: {}", adminEmail);
        
        // 如果启用了通知功能且需要在启动时初始化模板
        if (enabled && initTemplatesOnStartup) {
            try {
                boolean templateInitResult = loanValidationNotificationService.initializeNotificationTemplates();
                if (templateInitResult) {
                    log.info("贷款验证通知模板初始化成功");
                } else {
                    log.warn("贷款验证通知模板初始化失败");
                }
            } catch (Exception e) {
                log.error("初始化贷款验证通知模板异常", e);
            }
        }
    }

    // Getter and Setter methods

    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 int getRetryCount() {
        return retryCount;
    }

    public void setRetryCount(int retryCount) {
        this.retryCount = retryCount;
    }

    public long getTimeoutMs() {
        return timeoutMs;
    }

    public void setTimeoutMs(long timeoutMs) {
        this.timeoutMs = timeoutMs;
    }

    public String getDefaultChannel() {
        return defaultChannel;
    }

    public void setDefaultChannel(String defaultChannel) {
        this.defaultChannel = defaultChannel;
    }

    public String getAdminEmail() {
        return adminEmail;
    }

    public void setAdminEmail(String adminEmail) {
        this.adminEmail = adminEmail;
    }

    public String getAdminPhone() {
        return adminPhone;
    }

    public void setAdminPhone(String adminPhone) {
        this.adminPhone = adminPhone;
    }

    public boolean isSmsEnabled() {
        return smsEnabled;
    }

    public void setSmsEnabled(boolean smsEnabled) {
        this.smsEnabled = smsEnabled;
    }

    public boolean isEmailEnabled() {
        return emailEnabled;
    }

    public void setEmailEnabled(boolean emailEnabled) {
        this.emailEnabled = emailEnabled;
    }

    public boolean isSystemNotificationEnabled() {
        return systemNotificationEnabled;
    }

    public void setSystemNotificationEnabled(boolean systemNotificationEnabled) {
        this.systemNotificationEnabled = systemNotificationEnabled;
    }

    public int getHighPriorityFailureThreshold() {
        return highPriorityFailureThreshold;
    }

    public void setHighPriorityFailureThreshold(int highPriorityFailureThreshold) {
        this.highPriorityFailureThreshold = highPriorityFailureThreshold;
    }

    public long getNotificationIntervalMs() {
        return notificationIntervalMs;
    }

    public void setNotificationIntervalMs(long notificationIntervalMs) {
        this.notificationIntervalMs = notificationIntervalMs;
    }

    public int getBatchNotificationSize() {
        return batchNotificationSize;
    }

    public void setBatchNotificationSize(int batchNotificationSize) {
        this.batchNotificationSize = batchNotificationSize;
    }

    public boolean isInitTemplatesOnStartup() {
        return initTemplatesOnStartup;
    }

    public void setInitTemplatesOnStartup(boolean initTemplatesOnStartup) {
        this.initTemplatesOnStartup = initTemplatesOnStartup;
    }

    @Override
    public String toString() {
        return "LoanValidationNotificationConfig{" +
                "enabled=" + enabled +
                ", asyncEnabled=" + asyncEnabled +
                ", retryCount=" + retryCount +
                ", timeoutMs=" + timeoutMs +
                ", defaultChannel='" + defaultChannel + '\'' +
                ", adminEmail='" + adminEmail + '\'' +
                ", adminPhone='" + adminPhone + '\'' +
                ", smsEnabled=" + smsEnabled +
                ", emailEnabled=" + emailEnabled +
                ", systemNotificationEnabled=" + systemNotificationEnabled +
                ", highPriorityFailureThreshold=" + highPriorityFailureThreshold +
                ", notificationIntervalMs=" + notificationIntervalMs +
                ", batchNotificationSize=" + batchNotificationSize +
                ", initTemplatesOnStartup=" + initTemplatesOnStartup +
                '}';
    }
}