use achubb_website::{run_load, run_server, database::establish_connection};
use clap::Parser;
use std::error::Error;
use sqlx::postgres::PgPool;

#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
    #[arg(short, long)]
    load: bool,

    #[arg(short, long)]
    run: bool,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let args = Args::parse();

    let pool: PgPool = match establish_connection().await {
        Ok(p) => p,
        Err(_) => panic!("error connecting to database"),
    };

    if args.load {
        run_load(&pool).await?;
    }

    if args.run || !args.load {
        run_server(pool).await?;
    }
    Ok(())
}
