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

#[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>> {
    dotenv().ok();
    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(())
}
