//! Memory Signal - Real-time notifications for agent coordination.
//!
//! MemorySignal events enable agents and orchestrators to react to
//! memory state changes in real-time across regions.

use mimirs_core::{AgentId, BlockId, MemoryClass, MemoryId, VerifiabilityStage};
use serde::{Deserialize, Serialize};

/// MemorySignal - Events emitted by agents for real-time coordination.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MemorySignal {
    /// Memory was stored.
    MemoryStored {
        /// Agent that stored the memory.
        agent_id: AgentId,
        /// Memory identifier.
        memory_id: MemoryId,
        /// Memory class.
        memory_class: MemoryClass,
    },

    /// Memory was recalled.
    MemoryRecalled {
        /// Agent that performed the recall.
        agent_id: AgentId,
        /// Query text.
        query: String,
        /// Number of results returned.
        results_count: usize,
    },

    /// Block was attached to agent.
    BlockAttached {
        /// Agent that attached the block.
        agent_id: AgentId,
        /// Block identifier.
        block_id: BlockId,
    },

    /// Block was detached from agent.
    BlockDetached {
        /// Agent that detached the block.
        agent_id: AgentId,
        /// Block identifier.
        block_id: BlockId,
    },

    /// Verifiability stage changed.
    VerifiabilityChanged {
        /// Agent where change occurred.
        agent_id: AgentId,
        /// Memory identifier.
        memory_id: MemoryId,
        /// New verifiability stage.
        new_stage: VerifiabilityStage,
    },

    /// Consolidation cycle completed.
    ConsolidationCompleted {
        /// Agent that completed consolidation.
        agent_id: AgentId,
        /// Number of memories processed.
        memories_processed: usize,
    },

    /// Agent identity evolved (L7).
    IdentityEvolved {
        /// Agent whose identity changed.
        agent_id: AgentId,
        /// New identity version.
        epoch: u64,
    },

    /// Contradiction detected during reflection (Phase 4.1).
    ConflictDetected {
        /// Agent that detected conflict.
        agent_id: AgentId,
        /// Conflicting memory A.
        memory_a: MemoryId,
        /// Conflicting memory B.
        memory_b: MemoryId,
        /// Contradiction score.
        score: f32,
    },

    /// Consensus was reached on a change (Thing).
    ConsensusReached {
        /// Agent that proposed the change.
        agent_id: AgentId,
        /// Term number.
        term: u64,
    },

    /// Heartbeat from a cluster node.
    NodeHeartbeat {
        /// Agent sending heartbeat.
        agent_id: AgentId,
        /// System load / health score.
        health: f32,
    },
}

impl MemorySignal {
    /// Returns the agent_id from any signal variant.
    pub fn agent_id(&self) -> AgentId {
        match self {
            MemorySignal::MemoryStored { agent_id, .. } => *agent_id,
            MemorySignal::MemoryRecalled { agent_id, .. } => *agent_id,
            MemorySignal::BlockAttached { agent_id, .. } => *agent_id,
            MemorySignal::BlockDetached { agent_id, .. } => *agent_id,
            MemorySignal::VerifiabilityChanged { agent_id, .. } => *agent_id,
            MemorySignal::ConsolidationCompleted { agent_id, .. } => *agent_id,
            MemorySignal::IdentityEvolved { agent_id, .. } => *agent_id,
            MemorySignal::ConflictDetected { agent_id, .. } => *agent_id,
            MemorySignal::ConsensusReached { agent_id, .. } => *agent_id,
            MemorySignal::NodeHeartbeat { agent_id, .. } => *agent_id,
        }
    }

    /// Returns a string description of the signal type.
    pub fn signal_type(&self) -> &'static str {
        match self {
            MemorySignal::MemoryStored { .. } => "MemoryStored",
            MemorySignal::MemoryRecalled { .. } => "MemoryRecalled",
            MemorySignal::BlockAttached { .. } => "BlockAttached",
            MemorySignal::BlockDetached { .. } => "BlockDetached",
            MemorySignal::VerifiabilityChanged { .. } => "VerifiabilityChanged",
            MemorySignal::ConsolidationCompleted { .. } => "ConsolidationCompleted",
            MemorySignal::IdentityEvolved { .. } => "IdentityEvolved",
            MemorySignal::ConflictDetected { .. } => "ConflictDetected",
            MemorySignal::ConsensusReached { .. } => "ConsensusReached",
            MemorySignal::NodeHeartbeat { .. } => "NodeHeartbeat",
        }
    }
}
