//! Defines the logging of transaction messages sent to the client router.
use crate::logging::messages::connections::ping::PingJourney;
use crate::logging::messages::transactions::base::TransactionJourney;
use crate::messages::client::message::TransactionMessage;
use crate::messages::server::interface::ServerMessage;

const TARGET: &str = "surrealcs::kernel::client::router";

/// Logs a transaction message that the client router has received.
///
/// # Arguments
/// * `message`: the transaction message that the client router has received
pub fn log_client_router_message(message: &TransactionMessage) {
	if let TransactionMessage::TransactionOperation(unwrapped_message) = message {
		// Log ping messages separately
		if let ServerMessage::Ping(_) = unwrapped_message.message {
			tracing::trace!(target: TARGET, connection_id = %unwrapped_message.connection_id,
				"{}",
				PingJourney::RecievedByClientRouter.as_str()
			);
		}
		// Log other transaction messages
		let tx_pointer = TransactionJourney::RecievedByClientRouter;
		let log = tx_pointer.from_wrapped_server_message(unwrapped_message);
		if let Some(log) = log {
			tracing::trace!(target: TARGET, "{log}");
		}
	}
}
