use nostr_rs_client::{Error as ClientError, Pool};
use nostr_rs_types::{client::Subscribe, Response};

#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error("Nostr: {0}")]
    Addr(#[from] nostr_rs_types::types::addr::Error),

    #[error("client: {0}")]
    Client(#[from] ClientError),
}

#[tokio::main]
async fn main() {
    env_logger::init();
    let mut clients = vec![
        "wss://relay.damus.io/",
        "wss://brb.io",
        "wss://nos.lol",
        "wss://relay.current.fyi",
        "wss://eden.nostr.land",
        "wss://relay.snort.social",
    ]
    .into_iter()
    .fold(Pool::new(), |clients, host| {
        clients.connect_to(host.parse().expect("valid url"))
    });

    let _ = clients.subscribe(Subscribe::default().into()).await;

    loop {
        if let Some((msg, relayed_by)) = clients.recv().await {
            match msg {
                Response::Event(x) => {
                    println!("{} => {:?}", relayed_by, x);
                }
                Response::EndOfStoredEvents(_) => {}
                msg => {
                    println!("{} {:?}", relayed_by, msg);
                }
            }
        }
    }
}
