use anyhow::Result;
use config::Config;
use pbap_core::phonebook::PhonebookPath;
use store::Store;
use transport::iroh::Endpoint;
use super::render::{render_sync_dto, render_sync_report};
use crate::commands::{broker, conn};
pub(crate) async fn run(
cfg: &Config,
endpoint: Option<&Endpoint>,
device: Option<&str>,
store: &Store,
config_path: Option<&std::path::Path>,
) -> Result<String> {
if endpoint.is_none() {
let report = broker::sync_contacts(cfg, device, config_path).await?;
return Ok(render_sync_dto(&report));
}
let mut client = conn::connect_pbap(cfg, endpoint, device).await?;
let report = session::contacts::sync_contacts(&mut client, store, PhonebookPath::Pb).await?;
if let Err(e) = client.disconnect().await {
tracing::warn!("PBAP disconnect failed: {e}");
}
Ok(render_sync_report(&report))
}