"""This module contains the tasks of the ``broker_models_hooks`` app"""

import json

from mtp.celery import app
from mtp.apps.common.message_broker import send_data_to_topic


@app.task()
def publish_to_broker(serialized_instance, broker_hook_topic, event_type):
    """Celery task to publish a change of a given model to the message broker

    Parameters
    ----------
    serialized_instance: dict
        The python representation of the model.
    broker_hook_topic: str
        The message broker topic to publish change to.
    event_type: str
        One of the ``UPDATE`` or ``CREATE`` constants.

    """

    broker_message = {
        'event_type': event_type,
        'instance': serialized_instance,
    }

    send_data_to_topic(
        json.dumps(broker_message),
        broker_hook_topic
    )
