use super::util::current_dir;
use koala_core::invariant::Context;
use koala_drift::{format_github_annotations, format_report, Registry as DriftRegistry, Severity};
use std::path::PathBuf;
use std::process::ExitCode;

pub fn run(root: Option<PathBuf>, explain: bool, format: String) -> ExitCode {
    let root = root.unwrap_or_else(current_dir);
    let ctx = Context::new(root);
    let reg = DriftRegistry::builtin();
    let findings = reg.run_all(&ctx);
    match format.as_str() {
        "github" => print!("{}", format_github_annotations(&findings)),
        _ => print!("{}", format_report(&reg, &findings, explain)),
    }
    if findings.iter().any(|f| f.severity == Severity::Hard) {
        ExitCode::from(1)
    } else {
        ExitCode::SUCCESS
    }
}
