package org.xyzh.common.utils.validation;

import java.util.ArrayList;
import java.util.List;

/**
 * @description 校验结果类
 * @filename ValidationResult.java
 * @author yslg
 * @copyright yslg
 * @since 2025-11-02
 */
public class ValidationResult {
    
    /**
     * @description 是否校验通过
     */
    private Boolean valid;
    
    /**
     * @description 错误信息列表
     */
    private List<String> errors;
    
    /**
     * @description 第一个错误信息
     */
    private String firstError;

    public ValidationResult() {
        this.valid = true;
        this.errors = new ArrayList<>();
    }

    public Boolean isValid() {
        return valid;
    }

    public void setValid(Boolean valid) {
        this.valid = valid;
    }

    public List<String> getErrors() {
        return errors;
    }

    public String getFirstError() {
        return firstError;
    }

    /**
     * @description 添加错误信息
     * @param error 错误信息
     */
    public void addError(String error) {
        this.valid = false;
        this.errors.add(error);
        if (this.firstError == null) {
            this.firstError = error;
        }
    }

    /**
     * @description 获取所有错误信息的字符串
     * @return 错误信息字符串
     */
    public String getAllErrors() {
        return String.join("; ", errors);
    }

    /**
     * @description 是否有错误
     * @return Boolean
     */
    public Boolean hasErrors() {
        return !valid;
    }

    /**
     * @description 获取错误数量
     * @return 错误数量
     */
    public int getErrorCount() {
        return errors.size();
    }

    @Override
    public String toString() {
        return "ValidationResult{" +
                "valid=" + valid +
                ", errorCount=" + errors.size() +
                ", errors=" + errors +
                '}';
    }
}

