package com.ruoyi.credit.service.validator;

import com.ruoyi.credit.domain.ValidationRule;
import com.ruoyi.credit.service.RuleConfig;
import com.ruoyi.credit.service.RuleValidator;
import com.ruoyi.credit.service.ValidationContext;
import com.ruoyi.credit.service.ValidationResult;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * 债权凭证有效性验证器
 * 验证债权凭证的状态和有效性
 * 
 * @author ruoyi
 * @date 2025-01-05
 */
@Component
public class CreditValidityValidator implements RuleValidator {
    
    private static final Logger log = LoggerFactory.getLogger(CreditValidityValidator.class);
    
    @Override
    public ValidationResult validate(ValidationRule rule, RuleConfig config, ValidationContext context) {
        log.debug("开始执行债权凭证有效性验证，贷款ID: {}, 债权凭证ID: {}", 
            context.getLoanId(), context.getCreditId());
        
        try {
            Long creditId = context.getCreditId();
            if (creditId == null) {
                return ValidationResult.failure("债权凭证ID不能为空");
            }
            
            // 这里可以添加更复杂的债权凭证有效性检查逻辑
            // 比如检查债权凭证状态、有效期、区块链验证等
            
            // 目前简化实现：如果债权凭证ID存在，则认为有效
            log.debug("债权凭证有效性验证通过，债权凭证ID: {}", creditId);
            return ValidationResult.success("债权凭证有效性验证通过");
            
        } catch (Exception e) {
            log.error("债权凭证有效性验证异常，贷款ID: {}", context.getLoanId(), e);
            return ValidationResult.error("债权凭证有效性验证异常: " + e.getMessage(), "credit_validity");
        }
    }
    
    @Override
    public String getValidatorName() {
        return "债权凭证有效性验证器";
    }
    
    @Override
    public String getSupportedRuleType() {
        return ValidationRule.TYPE_CREDIT_VALIDITY;
    }
}