//! Transactional Blacksite project schema/material upgrader.

use std::path::PathBuf;

fn main() {
    let mut project = PathBuf::from(".");
    let mut apply = false;
    let mut json = false;
    let mut args = std::env::args().skip(1);
    while let Some(argument) = args.next() {
        match argument.as_str() {
            "--project" => {
                let Some(path) = args.next() else {
                    eprintln!("upgrade-project: --project requires a path");
                    std::process::exit(2);
                };
                project = PathBuf::from(path);
            }
            "--apply" => apply = true,
            "--dry-run" => apply = false,
            "--json" => json = true,
            other => {
                eprintln!("upgrade-project: unknown argument `{other}`");
                std::process::exit(2);
            }
        }
    }
    match scene::upgrade_project(&project, apply) {
        Ok(report) => {
            if json {
                println!("{}", serde_json::to_string_pretty(&report).unwrap());
            } else {
                println!(
                    "upgrade-project: {} changes, applied={}, backup={}",
                    report.changes.len(),
                    report.applied,
                    report.backup_path.as_deref().unwrap_or("none")
                );
                for change in report.changes {
                    println!("  {} [{}]", change.path, change.kind);
                }
                for warning in report.warnings {
                    eprintln!("  warning: {warning}");
                }
            }
        }
        Err(error) => {
            eprintln!("upgrade-project failed: {error}");
            std::process::exit(1);
        }
    }
}
