#![allow(deprecated)]
use std::{str::FromStr, sync::Arc};

use anyhow::Result;
use dittolive_ditto::prelude::*;

#[tokio::main]
async fn main() -> Result<()> {
    let app_id = AppId::from_str("bus_example_app").unwrap();

    let ditto = Ditto::builder()
        .with_temp_dir()
        .with_identity(|r| identity::OfflinePlayground::new(r, app_id))?
        // .with_minimum_log_level(LogLevel::Debug)
        .with_transport_config(|_| {
            let mut tc = TransportConfig::new();
            tc.enable_all_peer_to_peer();
            tc
        })?
        .build()?;
    ditto.set_license_from_env("DITTO_LICENSE")?;

    ditto.sync().start()?;
    let ditto = Arc::new(ditto);

    let _acceptor = ditto
        .bus()
        .bind_topic("example")
        .on_receive_factory(tokio::sync::mpsc::unbounded_channel)
        .finish_with(|mut stream| {
            tokio::task::spawn(async move {
                while let Some(payload) = stream.recv().await {
                    let _ = stream.message(payload).send();
                }
            });
        });

    std::future::pending::<()>().await;
    Ok(())
}
