//! D-Bus surface for `org.liberux.Contacts1`.
//!
//! Top-level wiring lives here; the per-method implementation is in
//! [`service`], the wire types in [`types`], and the variant/dict codec in
//! [`codec`].

use std::sync::Arc;

use crate::db::Database;

pub mod batch;
pub mod codec;
pub mod error;
pub mod service;
pub mod types;

pub use service::ContactsService;

pub const SERVICE_NAME: &str = "org.liberux.Contacts1";
pub const OBJECT_PATH:  &str = "/org/liberux/Contacts1";

/// Connect to the session bus, register `ContactsService` at the documented
/// object path, and request the well-known name. Returns the live
/// `Connection` — keep it alive for the lifetime of the daemon.
///
/// `eds_push` is optional. When `Some(_)`, every mutating method handler
/// schedules an EDS source refresh after the DB write so gnome-contacts
/// and Evolution see the change in seconds rather than waiting for their
/// next poll.
pub async fn serve(
	db: Arc<Database>,
	eds_push: Option<std::sync::Arc<crate::eds_push::EdsPusher>>,
) -> zbus::Result<zbus::Connection>
{
	let service = ContactsService::new( db, eds_push );
	zbus::connection::Builder::session()?
		.name( SERVICE_NAME )?
		.serve_at( OBJECT_PATH, service )?
		.build()
		.await
}
