mod auth;
mod run;

pub use auth::{login, logout, whoami};
pub use run::run;

use clap::{Parser, Subcommand};

#[derive(Parser)]
#[command(name = "biolib-rs", version, about = "BioLib CLI")]
pub struct Cli {
    #[command(subcommand)]
    pub command: Commands,
}

#[derive(Subcommand)]
pub enum Commands {
    Login,
    Logout,
    Whoami,
    Run {
        app_uri: String,
        #[arg(long)]
        experiment: Option<String>,
        #[arg(long, default_value_t = false)]
        non_blocking: bool,
        #[arg(trailing_var_arg = true, allow_hyphen_values = true)]
        args: Vec<String>,
    },
}

pub fn execute() {
    crate::logging::configure(30);
    let cli = Cli::parse();

    let result = match cli.command {
        Commands::Login => login(),
        Commands::Logout => logout(),
        Commands::Whoami => whoami(),
        Commands::Run {
            app_uri,
            experiment,
            non_blocking,
            args,
        } => run(&app_uri, experiment.as_deref(), !non_blocking, args),
    };

    if let Err(e) = result {
        eprintln!("Error: {e}");
        std::process::exit(1);
    }
}
