use clap::Parser;

mod walker;
mod locator;
mod p2p;

use walker::Walker;

#[derive(Debug, Parser)]
struct Opts {
    #[clap(
        short, 
        long, 
        default_value = "0x7657c8c868863dea692178d462fe9018ac8f1e16f51be31eea8f29274d85525b")]
    genesis: String,

    #[clap(
        short, 
        long, 
        default_value = "/dns/bootnode69.chain.ghostchain.io/tcp/30334/p2p/12D3KooWF9SWxz9dmy6vfndQhoxqCa7PESaoFWEiF8Jkqh4xKDRf",
        use_value_delimiter = true, 
        value_parser)]
    bootnodes: Vec<String>,

    #[clap(
        short, 
        long, 
        default_value = "60",
        value_parser = parse_duration,
    )]
    timeout: std::time::Duration,

    #[clap(short, long, default_value = None, value_parser)]
    filename: Option<String>,
}

fn parse_duration(arg: &str) -> Result<std::time::Duration, std::num::ParseIntError> {
    let seconds = arg.parse()?;
    Ok(std::time::Duration::from_secs(seconds))
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let filter = tracing_subscriber::filter::EnvFilter::from_default_env();
    tracing_subscriber::fmt()
        .with_env_filter(filter)
        .init();

    let args = Opts::parse();
    Walker::new()
        .with_genesis(args.genesis)
        .with_bootnodes(args.bootnodes)
        .with_timeout(args.timeout)
        .with_filename(args.filename)
        .build()?
        .walk_around()
        .await
}
