package com.ruoyi.credit.service;

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

/**
 * 验证结果类
 * 封装验证执行的结果信息
 * 
 * @author ruoyi
 * @date 2025-12-30
 */
public class ValidationResult {
    
    /**
     * 验证状态枚举
     */
    public enum Status {
        SUCCESS,    // 验证成功
        FAILURE,    // 验证失败
        ERROR,      // 验证错误（系统异常）
        SKIPPED     // 验证跳过
    }
    
    /** 验证状态 */
    private Status status;
    
    /** 验证消息 */
    private String message;
    
    /** 规则名称 */
    private String ruleName;
    
    /** 验证时间 */
    private Date validationTime;
    
    /** 执行耗时（毫秒） */
    private Long executionTime;
    
    /** 验证得分（0-100） */
    private Integer score;
    
    /** 附加数据 */
    private Map<String, Object> additionalData;
    
    /** 异常信息 */
    private String exceptionMessage;
    
    /** 验证详情 */
    private String details;
    
    /**
     * 私有构造函数
     */
    private ValidationResult() {
        this.validationTime = new Date();
        this.additionalData = new HashMap<>();
    }
    
    /**
     * 创建成功结果
     * 
     * @param message 成功消息
     * @return 验证结果
     */
    public static ValidationResult success(String message) {
        ValidationResult result = new ValidationResult();
        result.status = Status.SUCCESS;
        result.message = message;
        result.score = 100;
        return result;
    }
    
    /**
     * 创建成功结果（带规则名称）
     * 
     * @param message 成功消息
     * @param ruleName 规则名称
     * @return 验证结果
     */
    public static ValidationResult success(String message, String ruleName) {
        ValidationResult result = success(message);
        result.ruleName = ruleName;
        return result;
    }
    
    /**
     * 创建失败结果
     * 
     * @param message 失败消息
     * @return 验证结果
     */
    public static ValidationResult failure(String message) {
        ValidationResult result = new ValidationResult();
        result.status = Status.FAILURE;
        result.message = message;
        result.score = 0;
        return result;
    }
    
    /**
     * 创建失败结果（带规则名称）
     * 
     * @param message 失败消息
     * @param ruleName 规则名称
     * @return 验证结果
     */
    public static ValidationResult failure(String message, String ruleName) {
        ValidationResult result = failure(message);
        result.ruleName = ruleName;
        return result;
    }
    
    /**
     * 创建错误结果
     * 
     * @param message 错误消息
     * @param ruleName 规则名称
     * @return 验证结果
     */
    public static ValidationResult error(String message, String ruleName) {
        ValidationResult result = new ValidationResult();
        result.status = Status.ERROR;
        result.message = message;
        result.ruleName = ruleName;
        result.score = 0;
        result.exceptionMessage = message;
        return result;
    }
    
    /**
     * 创建跳过结果
     * 
     * @param message 跳过消息
     * @return 验证结果
     */
    public static ValidationResult skipped(String message) {
        ValidationResult result = new ValidationResult();
        result.status = Status.SKIPPED;
        result.message = message;
        result.score = null;
        return result;
    }
    
    /**
     * 创建跳过结果（带规则名称）
     * 
     * @param message 跳过消息
     * @param ruleName 规则名称
     * @return 验证结果
     */
    public static ValidationResult skipped(String message, String ruleName) {
        ValidationResult result = skipped(message);
        result.ruleName = ruleName;
        return result;
    }
    
    /**
     * 检查是否成功
     * 
     * @return 是否成功
     */
    public boolean isSuccess() {
        return Status.SUCCESS.equals(status);
    }
    
    /**
     * 检查是否失败
     * 
     * @return 是否失败
     */
    public boolean isFailed() {
        return Status.FAILURE.equals(status);
    }
    
    /**
     * 检查是否错误
     * 
     * @return 是否错误
     */
    public boolean isError() {
        return Status.ERROR.equals(status);
    }
    
    /**
     * 检查是否跳过
     * 
     * @return 是否跳过
     */
    public boolean isSkipped() {
        return Status.SKIPPED.equals(status);
    }
    
    /**
     * 添加附加数据
     * 
     * @param key 键
     * @param value 值
     * @return 当前对象（支持链式调用）
     */
    public ValidationResult addData(String key, Object value) {
        this.additionalData.put(key, value);
        return this;
    }
    
    /**
     * 设置执行耗时
     * 
     * @param executionTime 执行耗时（毫秒）
     * @return 当前对象（支持链式调用）
     */
    public ValidationResult setExecutionTime(Long executionTime) {
        this.executionTime = executionTime;
        return this;
    }
    
    /**
     * 设置验证得分
     * 
     * @param score 验证得分（0-100）
     * @return 当前对象（支持链式调用）
     */
    public ValidationResult setScore(Integer score) {
        this.score = score;
        return this;
    }
    
    /**
     * 设置验证详情
     * 
     * @param details 验证详情
     * @return 当前对象（支持链式调用）
     */
    public ValidationResult setDetails(String details) {
        this.details = details;
        return this;
    }
    
    // Getters and Setters
    
    public Status getStatus() {
        return status;
    }
    
    public void setStatus(Status status) {
        this.status = status;
    }
    
    public String getMessage() {
        return message;
    }
    
    public void setMessage(String message) {
        this.message = message;
    }
    
    public String getRuleName() {
        return ruleName;
    }
    
    public void setRuleName(String ruleName) {
        this.ruleName = ruleName;
    }
    
    public Date getValidationTime() {
        return validationTime;
    }
    
    public void setValidationTime(Date validationTime) {
        this.validationTime = validationTime;
    }
    
    public Long getExecutionTime() {
        return executionTime;
    }
    
    public Integer getScore() {
        return score;
    }
    
    public Map<String, Object> getAdditionalData() {
        return additionalData;
    }
    
    public void setAdditionalData(Map<String, Object> additionalData) {
        this.additionalData = additionalData;
    }
    
    public String getExceptionMessage() {
        return exceptionMessage;
    }
    
    public void setExceptionMessage(String exceptionMessage) {
        this.exceptionMessage = exceptionMessage;
    }
    
    public String getDetails() {
        return details;
    }
    
    @Override
    public String toString() {
        return "ValidationResult{" +
                "status=" + status +
                ", message='" + message + '\'' +
                ", ruleName='" + ruleName + '\'' +
                ", validationTime=" + validationTime +
                ", executionTime=" + executionTime +
                ", score=" + score +
                ", details='" + details + '\'' +
                '}';
    }
}