use clap::Parser;
use toolbox::logging::LogCliVQ;
use tracing::error;

use crate::{cmd::Command, config::WebpageConfig};

mod cmd;
mod config;

#[derive(Parser, Debug)]
#[command(version, about, long_about = None, styles=toolbox::cli::clap_styles())]
struct Cli {
	#[clap(flatten)]
	vq: LogCliVQ,

	/// Do not show progress bars
	#[arg(long)]
	noprogress: bool,

	#[command(subcommand)]
	command: Command,
}

pub struct CmdContext {
	config: WebpageConfig,
}

fn main() {
	let cli = Cli::parse();
	let ctx = CmdContext {
		config: WebpageConfig::load(),
	};

	let rt = ctx.config.make_runtime();
	rt.block_on(ctx.config.init_logging());
	let res = rt.block_on(cli.command.run(ctx));

	if let Err(e) = res {
		for e in e.chain() {
			error!("{e}");
		}

		std::process::exit(1);
	}
}
