//! A "Cell" represents a SAF/AgentId pair - a space where one saf/agent
//! can track its source chain and service network requests / responses.

use ai_hash::AgentPubKey;
use ai_hash::SafHash;
use aingle_middleware_bytes::prelude::*;
use std::fmt;

/// The unique identifier for a Cell.
/// Cells are uniquely determined by this pair - this pair is necessary
/// and sufficient to refer to a cell in a conductor
#[derive(
    Clone, Debug, Hash, PartialEq, Eq, serde::Serialize, serde::Deserialize, SerializedBytes,
)]
pub struct CellId(SafHash, AgentPubKey);

impl fmt::Display for CellId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Cell({}, {})", self.saf_hash(), self.agent_pubkey())
    }
}

impl CellId {
    /// Create a CellId from its components
    pub fn new(saf_hash: SafHash, agent_pubkey: AgentPubKey) -> Self {
        CellId(saf_hash, agent_pubkey)
    }

    /// The saf hash/address for this cell.
    pub fn saf_hash(&self) -> &SafHash {
        &self.0
    }

    /// The agent id / public key for this cell.
    pub fn agent_pubkey(&self) -> &AgentPubKey {
        &self.1
    }

    /// Into [SafHash] and [AgentPubKey]
    pub fn into_saf_and_agent(self) -> (SafHash, AgentPubKey) {
        (self.0, self.1)
    }
}

impl From<(SafHash, AgentPubKey)> for CellId {
    fn from(pair: (SafHash, AgentPubKey)) -> Self {
        Self(pair.0, pair.1)
    }
}
