use anyhow::Context;
use clap::Parser;
use dialtone_common::utils::version::DT_VERSION;
use dialtone_sqlx::db::get_pooled_connection;
use dialtone_sqlx::db::site_info::fetch_site;

/// Fetch a dialtone site information.
#[derive(Parser, Debug)]
#[clap(name = "fetch_site", version = DT_VERSION)]
struct Opts {
    /// DNS host names (e.g. thefoo.example)
    #[clap(value_parser, short, long)]
    host_name: String,
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    dotenv::dotenv().ok();
    let opts: Opts = Opts::parse();
    println!("Fetching sites {}", opts.host_name);

    let pg_pool = get_pooled_connection().await?;
    let site_data = fetch_site(&pg_pool, &opts.host_name)
        .await?
        .with_context(|| "no sites by that names could be found")?;

    let s = serde_json::to_string_pretty(&site_data);
    println!("{}", s.unwrap());

    Ok(())
}
