//! Channel domain logic — 1:1 DM channel creation.

use std::sync::Arc;

use crate::storage::Store;

use super::types::*;

/// Domain service for 1:1 channel management.
pub struct ChannelService {
    pub store: Arc<dyn Store>,
}

impl ChannelService {
    pub fn create_channel(
        &self,
        req: CreateChannelReq,
        caller_identity_key: &[u8],
    ) -> Result<CreateChannelResp, DomainError> {
        if req.peer_key.len() != 32 {
            return Err(DomainError::InvalidIdentityKey(req.peer_key.len()));
        }
        if caller_identity_key == req.peer_key.as_slice() {
            return Err(DomainError::BadParams(
                "peer_key must not equal caller identity".into(),
            ));
        }

        let (channel_id, was_new) = self
            .store
            .create_channel(caller_identity_key, &req.peer_key)?;

        Ok(CreateChannelResp {
            channel_id,
            was_new,
        })
    }
}
