//! Append an operation to a local ontology's oplog.
//!
//! Phase 1 (D28) is a thin wrapper around
//! [`ontoref_oplog::OpLog::append`]. Once a `SyncBackend` is wired in,
//! this function will also announce the new op to peers — the same
//! call site, no caller-visible API change.

use std::path::Path;

use ontoref_oplog::OpLog;
use ontoref_types::{OpId, Operation};

use crate::{Error, Result};

/// Append `op` to the oplog at `oplog_path`, creating the oplog if it
/// does not yet exist. Returns the resulting [`OpId`]. Re-appending the
/// same op is a no-op and returns the same id.
///
/// # Errors
///
/// - [`Error::OpenOplog`] when the oplog file cannot be opened.
/// - [`Error::Append`] when the oplog rejects the operation (bad signature,
///   unknown parent, non-monotonic timestamp, IO).
pub fn publish_op(oplog_path: &Path, op: &Operation) -> Result<OpId> {
    let log = OpLog::open(oplog_path).map_err(|e| Error::OpenOplog {
        path: oplog_path.display().to_string(),
        source: e,
    })?;
    log.append(op).map_err(Error::Append)
}
