use anyhow::Context;
use clap::{Parser, Subcommand};

use folk_builder::{config, runner};

#[derive(Parser)]
#[command(name = "folk-builder", version = env!("CARGO_PKG_VERSION"))]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    Build {
        #[arg(short, long, default_value = "folk.build.toml")]
        config: String,
        #[arg(short, long, default_value = ".")]
        output_dir: String,
        /// Build straight from a release manifest (build-manifest.toml).
        /// Overrides --config; no intermediate folk.build.toml needed.
        #[arg(short, long)]
        manifest: Option<String>,
    },
}

fn main() -> anyhow::Result<()> {
    tracing_subscriber::fmt::init();
    let cli = Cli::parse();
    match cli.command {
        Commands::Build {
            config,
            output_dir,
            manifest,
        } => {
            let cfg = match manifest {
                Some(path) => {
                    config::load_manifest(&path).with_context(|| format!("load manifest {path}"))?
                }
                None => config::load(&config).with_context(|| format!("load {config}"))?,
            };
            runner::build(&cfg, &output_dir)
        }
    }
}
