package cz.senslog.analytics.utils.validator.domain;

import org.apache.logging.log4j.util.Strings;

public record ValidationReport(String message, ValidationResult[] validationResults) {

    public static ValidationReport empty() {
        return new ValidationReport(null, new ValidationResult[0]);
    }

    public boolean isEmpty() {
        return validationResults.length == 0 && Strings.isEmpty(message);
    }

    public ValidationReport join(ValidationReport validationReport) {
        if (validationReport == null || validationReport.isEmpty()) {
            return this;
        }

        if (this.isEmpty() && !validationReport.isEmpty()) {
            return validationReport;
        }

        ValidationResult[] newResults = new ValidationResult[this.validationResults.length + validationReport.validationResults.length];
        System.arraycopy(this.validationResults, 0, newResults, 0, this.validationResults.length);
        System.arraycopy(validationReport.validationResults, 0, newResults, this.validationResults.length, validationReport.validationResults.length);

        String newMessage;
        if (this.message == null && validationReport.message == null) {
            newMessage = null;
        } else if (this.message == null) {
            newMessage = validationReport.message;
        } else if (validationReport.message == null) {
            newMessage = this.message;
        } else {
            newMessage = this.message + validationReport.message;
        }

        return new ValidationReport(newMessage, newResults);
    }
}
