use crate::rules::check_encapsulation;
use masl_diagnostics::diagnostic::Diagnostic;
use masl_hir::HirDocument;

/// Validates architectural and encapsulation rules on a HIR Document.
pub fn run_validation(hir: HirDocument) -> Result<HirDocument, Vec<Diagnostic>> {
    let mut diags = Vec::new();

    check_encapsulation(&hir, &mut diags);

    // If there are errors (or we just want warnings, but compilation can still continue)
    // Remember: Validator Warnings (W06xx) do NOT halt compilation unless there are actual Errors.
    // If a warning is treated as diagnostic but allows compilation, we return Ok(hir) and print warnings.
    // However, if we collect warnings as results, we return Ok(hir) if none are Errors, but return diagnostics for the compiler to print.
    // Let's return Err only if there's any Error. Warnings are returned as diagnostics along with Ok(hir) if needed,
    // but the simple compile driver API expects Result<HirDocument, Vec<Diagnostic>> where diagnostics contain errors.
    // We will treat these as warnings and allow compilation, meaning we return Ok(hir).
    // Let's filter if there's any Severity::Error in diagnostics.
    if diags.iter().any(|d| d.is_error()) {
        Err(diags)
    } else {
        // If there are only warnings, we currently return Ok but let the compiler driver log warnings if it supports it,
        // or we return them if needed. For our API simplicity, we return Ok(hir) when there are only warnings.
        Ok(hir)
    }
}
