//! Shared [`MockTransport`] for notification listener tests.

use crate::chat::{ChatTransport, MessageId};
use async_trait::async_trait;
use std::sync::Arc;

pub(super) type CallLog = Arc<std::sync::Mutex<Vec<(String, String, String)>>>;

/// Records every `send_message` call for inspection in tests.
pub(super) struct MockTransport {
    pub(super) calls: CallLog,
}

impl MockTransport {
    pub(super) fn new() -> (Arc<Self>, CallLog) {
        let calls: CallLog = Arc::new(std::sync::Mutex::new(Vec::new()));
        (
            Arc::new(Self {
                calls: Arc::clone(&calls),
            }),
            calls,
        )
    }
}

#[async_trait]
impl ChatTransport for MockTransport {
    async fn send_message(
        &self,
        room_id: &str,
        plain: &str,
        html: &str,
    ) -> Result<MessageId, String> {
        self.calls
            .lock()
            .unwrap()
            .push((room_id.to_string(), plain.to_string(), html.to_string()));
        Ok("mock-msg-id".to_string())
    }

    async fn edit_message(
        &self,
        _room_id: &str,
        _id: &str,
        _plain: &str,
        _html: &str,
    ) -> Result<(), String> {
        Ok(())
    }

    async fn send_typing(&self, _room_id: &str, _typing: bool) -> Result<(), String> {
        Ok(())
    }
}
