import { ValidationLevel } from '../../interfaces/common.js';
import type { ValidationResult, ValidationError } from '../../interfaces/common.js';

/**
 * Base validator class that defines common validation functionality
 * for all invoice format validators
 */
export abstract class BaseValidator {
  protected xml: string;
  protected errors: ValidationError[] = [];

  constructor(xml: string) {
    this.xml = xml;
  }

  /**
   * Validates XML against the specified level of validation
   * @param level Validation level (syntax, semantic, business)
   * @returns Result of validation
   */
  abstract validate(level?: ValidationLevel): ValidationResult;

  /**
   * Gets all validation errors found during validation
   * @returns Array of validation errors
   */
  public getValidationErrors(): ValidationError[] {
    return this.errors;
  }

  /**
   * Checks if the document is valid
   * @returns True if no validation errors were found
   */
  public isValid(): boolean {
    return this.errors.length === 0;
  }

  /**
   * Validates XML against schema
   * @returns True if schema validation passed
   */
  protected abstract validateSchema(): boolean;

  /**
   * Validates business rules
   * @returns True if business rule validation passed
   */
  protected abstract validateBusinessRules(): boolean;

  /**
   * Adds an error to the validation errors list
   * @param code Error code
   * @param message Error message
   * @param location Location in the XML where the error occurred
   */
  protected addError(code: string, message: string, location: string = ''): void {
    this.errors.push({
      code,
      message,
      location
    });
  }
}
