import json
from broker import producer

_channel = producer()


def send_message(msg_str: str):
    """
    Publish a single string message to RabbitMQ using a long‐lived channel.
    """
    _channel.basic_publish(exchange="", routing_key="test", body=msg_str)


def record_activity(
    activity_name: str, agent_id: int, version: int, activity_type: str
):
    send_message(f"ACTIVITY|{activity_name}|{agent_id}|{version}|{activity_type}")


def record_API_call(
    entity_name: str,
    agent_id: int,
    version: int,
    entity_type: str,
    call_type: str,
    call_values: dict,
):
    entity_data = json.dumps(call_values)
    send_message(
        f"ENTITY|{entity_name}|{agent_id}|{version}|{entity_type}|{call_type}|{entity_data}"
    )


def record_agent(name: str, agent_id: int, agent_type: str, timestamp: float):
    send_message(f"AGENT|{name}|{agent_id}|{timestamp}|{agent_type}")


def close_channel():
    _channel.close()
