use clap::Parser;
use toolbox::logging::{LogCliVQ, LoggingFormat, LoggingInitializer, LoggingTarget};
use tracing::error;

use crate::cmd::Command;

mod cmd;

#[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,
}

#[derive(Debug)]
pub struct CmdContext {}

#[tokio::main]
async fn main() {
	let cli = Cli::parse();

	{
		let res = LoggingInitializer {
			app_name: "webpage",
			loki: None,
			preset: cli.vq.into_preset(),
			target: LoggingTarget::Stderr {
				format: LoggingFormat::Ansi,
			},
		}
		.initialize();

		if let Err(e) = res {
			#[expect(clippy::print_stderr)]
			for e in e.chain() {
				eprintln!("{e}");
			}

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

	let ctx = CmdContext {};
	let res = cli.command.run(ctx).await;

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

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