//! Relationship edge operations and entity accessor methods on `TidalDb`.

use crate::entities::{
    CreatorItemsBitmap, HardNegIndex, InteractionLedger, PreferenceVectors, RelationshipType,
    UserStateIndex,
};
use crate::schema::{EntityId, TidalError, Timestamp};

use super::TidalDb;

impl TidalDb {
    /// Write a relationship edge between two entities.
    ///
    /// The edge is stored in the users keyspace under the `from` entity's key
    /// range using the relationship key encoding.
    ///
    /// # Errors
    ///
    /// - `TidalError::Internal` if no storage backend is wired.
    /// - `TidalError::Storage` on storage engine failure.
    pub fn write_relationship(
        &self,
        from: EntityId,
        rel_type: RelationshipType,
        to: EntityId,
        weight: f64,
        timestamp: Timestamp,
    ) -> crate::Result<()> {
        use crate::entities::relationship::{
            encode_relationship_key, serialize_relationship_value,
        };
        self.require_writeable("write_relationship")?;

        let storage = self.storage()?;

        let key = encode_relationship_key(from, rel_type, to);
        let value = serialize_relationship_value(weight, timestamp);
        storage
            .users_engine()
            .put(&key, &value)
            .map_err(TidalError::from)?;

        // Update in-memory user state based on relationship type.
        match rel_type {
            RelationshipType::Blocks => {
                self.user_state
                    .add_block_creator(from.as_u64(), to.as_u64());
            }
            RelationshipType::Hide => {
                #[allow(clippy::cast_possible_truncation)]
                self.user_state.add_hide(from.as_u64(), to.as_u64() as u32);
            }
            RelationshipType::Follows => {
                // Forward: user -> followed creator
                self.user_state.add_follow(from.as_u64(), to.as_u64());
                // Reverse: creator -> follower users
                self.user_state
                    .add_creator_follower(to.as_u64(), from.as_u64());
            }
            _ => {}
        }

        Ok(())
    }

    /// Delete a relationship edge.
    ///
    /// Removes both the durable storage entry and the corresponding in-memory
    /// state (blocked creators, hidden items, follows).
    ///
    /// # Errors
    ///
    /// - `TidalError::Internal` if no storage backend is wired.
    /// - `TidalError::Storage` on storage engine failure.
    pub fn delete_relationship(
        &self,
        from: EntityId,
        rel_type: RelationshipType,
        to: EntityId,
    ) -> crate::Result<()> {
        use crate::entities::relationship::encode_relationship_key;
        self.require_writeable("delete_relationship")?;

        let storage = self.storage()?;
        let key = encode_relationship_key(from, rel_type, to);
        storage
            .users_engine()
            .delete(&key)
            .map_err(TidalError::from)?;

        // Remove corresponding in-memory state.
        match rel_type {
            RelationshipType::Blocks => {
                self.user_state
                    .remove_block_creator(from.as_u64(), to.as_u64());
            }
            RelationshipType::Hide => {
                #[allow(clippy::cast_possible_truncation)]
                self.user_state
                    .remove_hide(from.as_u64(), to.as_u64() as u32);
            }
            RelationshipType::Follows => {
                self.user_state.remove_follow(from.as_u64(), to.as_u64());
                self.user_state
                    .remove_creator_follower(to.as_u64(), from.as_u64());
            }
            _ => {}
        }

        Ok(())
    }

    /// List all relationship targets for a given (from, `rel_type`) pair.
    ///
    /// Returns `(to_entity_id, weight, timestamp_nanos)` tuples.
    ///
    /// # Errors
    ///
    /// - `TidalError::Internal` if no storage backend is wired.
    /// - `TidalError::Storage` on storage engine failure.
    pub fn list_relationships(
        &self,
        from: EntityId,
        rel_type: RelationshipType,
    ) -> crate::Result<Vec<(EntityId, f64, u64)>> {
        use crate::entities::relationship::{
            deserialize_relationship_value, parse_relationship_to, relationship_prefix,
        };

        let storage = self.storage()?;
        let prefix = relationship_prefix(from, rel_type);
        let mut results = Vec::new();
        for entry in storage.users_engine().scan_prefix(&prefix) {
            let (key, value) = entry.map_err(TidalError::from)?;
            if let Some(to) = parse_relationship_to(&key)
                && let Some((weight, ts_nanos)) = deserialize_relationship_value(&value)
            {
                results.push((to, weight, ts_nanos));
            }
        }
        Ok(results)
    }

    /// Access the per-user state index (seen, blocked, saved, liked, completion).
    #[must_use]
    pub fn user_state(&self) -> &UserStateIndex {
        &self.user_state
    }

    /// Access the creator-items bitmap (maps creators to their item sets).
    #[must_use]
    pub fn creator_items(&self) -> &CreatorItemsBitmap {
        &self.creator_items
    }

    /// Access the hard-negative index.
    #[must_use]
    pub fn hard_negatives(&self) -> &HardNegIndex {
        &self.hard_negatives
    }

    /// Access the interaction weight ledger.
    #[must_use]
    pub fn interaction_ledger(&self) -> &InteractionLedger {
        &self.interaction_ledger
    }

    /// Access the preference vectors store.
    #[must_use]
    pub fn preference_vectors(&self) -> &PreferenceVectors {
        &self.preference_vectors
    }
}
