package com.ruoyi.credit.service;

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

import com.ruoyi.credit.strategy.PerformanceDegradationStrategy;

/**
 * 验证上下文类
 * 封装验证过程中需要的数据和参数
 * 
 * @author ruoyi
 * @date 2025-12-30
 */
public class ValidationContext {
    
    /** 贷款ID */
    private Long loanId;
    
    /** 债权凭证ID */
    private Long creditId;
    
    /** 服务周期ID */
    private Long servicePeriodId;
    
    /** 合同ID */
    private Long contractId;
    
    /** 验证类型 */
    private String validationType;
    
    /** 验证参数 */
    private Map<String, Object> parameters;
    
    /** 上下文数据 */
    private Map<String, Object> contextData;
    
    /** 用户ID */
    private Long userId;
    
    /** 用户名 */
    private String username;
    
    /** 验证批次ID */
    private String batchId;
    
    /** 是否异步验证 */
    private boolean async;
    
    /** 超时时间（毫秒） */
    private Long timeout;
    
    /** 性能降级配置 */
    private PerformanceDegradationStrategy.DegradationConfig degradationConfig;
    
    /**
     * 默认构造函数
     */
    public ValidationContext() {
        this.parameters = new HashMap<>();
        this.contextData = new HashMap<>();
        this.async = false;
    }
    
    /**
     * 构造函数
     * 
     * @param loanId 贷款ID
     */
    public ValidationContext(Long loanId) {
        this();
        this.loanId = loanId;
    }
    
    /**
     * 构造函数
     * 
     * @param loanId 贷款ID
     * @param creditId 债权凭证ID
     * @param servicePeriodId 服务周期ID
     */
    public ValidationContext(Long loanId, Long creditId, Long servicePeriodId) {
        this(loanId);
        this.creditId = creditId;
        this.servicePeriodId = servicePeriodId;
    }
    
    /**
     * 添加参数
     * 
     * @param key 参数键
     * @param value 参数值
     * @return 当前对象（支持链式调用）
     */
    public ValidationContext addParameter(String key, Object value) {
        this.parameters.put(key, value);
        return this;
    }
    
    /**
     * 获取参数
     * 
     * @param key 参数键
     * @return 参数值
     */
    public Object getParameter(String key) {
        return this.parameters.get(key);
    }
    
    /**
     * 获取参数（带默认值）
     * 
     * @param key 参数键
     * @param defaultValue 默认值
     * @param <T> 参数类型
     * @return 参数值
     */
    @SuppressWarnings("unchecked")
    public <T> T getParameter(String key, T defaultValue) {
        Object value = this.parameters.get(key);
        return value != null ? (T) value : defaultValue;
    }
    
    /**
     * 添加上下文数据
     * 
     * @param key 数据键
     * @param value 数据值
     * @return 当前对象（支持链式调用）
     */
    public ValidationContext addContextData(String key, Object value) {
        this.contextData.put(key, value);
        return this;
    }
    
    /**
     * 获取上下文数据
     * 
     * @param key 数据键
     * @return 数据值
     */
    public Object getContextData(String key) {
        return this.contextData.get(key);
    }
    
    /**
     * 获取上下文数据（带默认值）
     * 
     * @param key 数据键
     * @param defaultValue 默认值
     * @param <T> 数据类型
     * @return 数据值
     */
    @SuppressWarnings("unchecked")
    public <T> T getContextData(String key, T defaultValue) {
        Object value = this.contextData.get(key);
        return value != null ? (T) value : defaultValue;
    }
    
    /**
     * 检查是否包含指定参数
     * 
     * @param key 参数键
     * @return 是否包含
     */
    public boolean hasParameter(String key) {
        return this.parameters.containsKey(key);
    }
    
    /**
     * 检查是否包含指定上下文数据
     * 
     * @param key 数据键
     * @return 是否包含
     */
    public boolean hasContextData(String key) {
        return this.contextData.containsKey(key);
    }
    
    /**
     * 设置异步验证模式
     * 
     * @param async 是否异步
     * @return 当前对象（支持链式调用）
     */
    public ValidationContext setAsync(boolean async) {
        this.async = async;
        return this;
    }
    
    /**
     * 设置超时时间
     * 
     * @param timeout 超时时间（毫秒）
     * @return 当前对象（支持链式调用）
     */
    public ValidationContext setTimeout(Long timeout) {
        this.timeout = timeout;
        return this;
    }
    
    /**
     * 设置验证批次ID
     * 
     * @param batchId 批次ID
     * @return 当前对象（支持链式调用）
     */
    public ValidationContext setBatchId(String batchId) {
        this.batchId = batchId;
        return this;
    }
    
    /**
     * 设置用户信息
     * 
     * @param userId 用户ID
     * @param username 用户名
     * @return 当前对象（支持链式调用）
     */
    public ValidationContext setUser(Long userId, String username) {
        this.userId = userId;
        this.username = username;
        return this;
    }
    
    /**
     * 设置性能降级配置
     * 
     * @param degradationConfig 降级配置
     * @return 当前对象（支持链式调用）
     */
    public ValidationContext setDegradationConfig(PerformanceDegradationStrategy.DegradationConfig degradationConfig) {
        this.degradationConfig = degradationConfig;
        return this;
    }
    
    /**
     * 获取性能降级配置
     * 
     * @return 降级配置
     */
    public PerformanceDegradationStrategy.DegradationConfig getDegradationConfig() {
        return degradationConfig;
    }
    
    /**
     * 检查是否启用了性能降级
     * 
     * @return 是否启用降级
     */
    public boolean isDegradationEnabled() {
        return degradationConfig != null && 
               degradationConfig.getLevel() != PerformanceDegradationStrategy.DegradationLevel.NONE;
    }
    
    // Getters and Setters
    
    public Long getLoanId() {
        return loanId;
    }
    
    public void setLoanId(Long loanId) {
        this.loanId = loanId;
    }
    
    public Long getCreditId() {
        return creditId;
    }
    
    public void setCreditId(Long creditId) {
        this.creditId = creditId;
    }
    
    public Long getServicePeriodId() {
        return servicePeriodId;
    }
    
    public void setServicePeriodId(Long servicePeriodId) {
        this.servicePeriodId = servicePeriodId;
    }
    
    public Long getContractId() {
        return contractId;
    }
    
    public void setContractId(Long contractId) {
        this.contractId = contractId;
    }
    
    public String getValidationType() {
        return validationType;
    }
    
    public void setValidationType(String validationType) {
        this.validationType = validationType;
    }
    
    public Map<String, Object> getParameters() {
        return parameters;
    }
    
    public void setParameters(Map<String, Object> parameters) {
        this.parameters = parameters;
    }
    
    public Map<String, Object> getContextData() {
        return contextData;
    }
    
    public void setContextData(Map<String, Object> contextData) {
        this.contextData = contextData;
    }
    
    public Long getUserId() {
        return userId;
    }
    
    public void setUserId(Long userId) {
        this.userId = userId;
    }
    
    public String getUsername() {
        return username;
    }
    
    public void setUsername(String username) {
        this.username = username;
    }
    
    public String getBatchId() {
        return batchId;
    }
    
    public boolean isAsync() {
        return async;
    }
    
    public Long getTimeout() {
        return timeout;
    }
    
    @Override
    public String toString() {
        return "ValidationContext{" +
                "loanId=" + loanId +
                ", creditId=" + creditId +
                ", servicePeriodId=" + servicePeriodId +
                ", contractId=" + contractId +
                ", validationType='" + validationType + '\'' +
                ", userId=" + userId +
                ", username='" + username + '\'' +
                ", batchId='" + batchId + '\'' +
                ", async=" + async +
                ", timeout=" + timeout +
                '}';
    }
}