//! Core CRDT traits for distributed merge operations.

use crate::error::Result;
use async_trait::async_trait;

/// Trait for CRDT types that support merge operations.
///
/// This trait defines the interface for extracting state and merging
/// state from remote replicas. Implementations must satisfy the CRDT
/// properties:
///
/// - **Commutativity**: `merge(A, B)` produces the same result as `merge(B, A)`
/// - **Associativity**: `merge(merge(A, B), C)` equals `merge(A, merge(B, C))`
/// - **Idempotence**: `merge(A, A)` equals `A`
///
/// # Type Parameters
///
/// The `State` associated type represents the serializable state that
/// can be transferred between replicas. It should be designed for
/// efficient delta synchronization when possible.
#[async_trait]
pub trait CrdtMerge: Send + Sync {
    /// The serializable state type for this CRDT.
    ///
    /// This type should implement rkyv serialization for efficient
    /// network transfer and storage.
    type State: Send + Sync;

    /// Extracts the current state for a given subject.
    ///
    /// The returned state can be sent to remote replicas and merged
    /// using the `merge` method.
    ///
    /// # Arguments
    ///
    /// * `subject` - The subject identifier to get state for
    ///
    /// # Returns
    ///
    /// The current CRDT state for the subject.
    async fn get_state(&self, subject: &str) -> Result<Self::State>;

    /// Merges remote state into the local replica.
    ///
    /// This operation must be:
    /// - **Idempotent**: Merging the same state twice has no additional effect
    /// - **Commutative**: Order of merge operations doesn't matter
    /// - **Associative**: Grouping of merge operations doesn't matter
    ///
    /// # Arguments
    ///
    /// * `subject` - The subject identifier to merge state for
    /// * `remote` - The state received from a remote replica
    ///
    /// # Returns
    ///
    /// Ok(()) on success, or an error if the merge fails.
    async fn merge(&self, subject: &str, remote: &Self::State) -> Result<()>;

    /// Returns the node ID for this CRDT instance.
    ///
    /// The node ID is used for tiebreaking in some CRDT operations
    /// and for tracking state provenance.
    fn node_id(&self) -> [u8; 16];
}

// NOTE: CrdtStateCompare trait planned for Phase 6B (anti-entropy sync).
// Removed to avoid dead code until implementation is needed.
