//! Validate project content through the same report consumed by the editor.

use std::path::PathBuf;

fn main() {
    let mut project_root = PathBuf::from(".");
    let mut json = false;
    let mut args = std::env::args().skip(1);
    while let Some(argument) = args.next() {
        match argument.as_str() {
            "--json" => json = true,
            "--project" => {
                let Some(path) = args.next() else {
                    eprintln!("validate-levels: --project requires a path");
                    std::process::exit(2);
                };
                project_root = PathBuf::from(path);
            }
            _ => {
                eprintln!("validate-levels: unknown argument `{argument}`");
                std::process::exit(2);
            }
        }
    }

    let report = scene::validate_project(&project_root);
    if json {
        println!(
            "{}",
            serde_json::to_string_pretty(&report).expect("validation report must serialize")
        );
    } else {
        for finding in &report.findings {
            let actor = finding
                .owner_actor_id
                .as_deref()
                .map(|actor| format!(" actor={actor}"))
                .unwrap_or_default();
            eprintln!(
                "{:?} [{}] {}{}: {}",
                finding.severity, finding.code, finding.source_path, actor, finding.message
            );
        }
        println!(
            "validate-levels: {} dependencies, {} findings, {} blocking errors",
            report.dependencies.len(),
            report.findings.len(),
            report.blocking_error_count()
        );
    }
    if !report.is_release_ready() {
        std::process::exit(1);
    }
}
