"""
This program has been developed by students from the bachelor Computer Science at Utrecht
University within the Software Project course.
© Copyright Utrecht University (Department of Information and Computing Sciences)
"""

import logging

from fastapi import APIRouter, Request

from control_backend.schemas.message import Message

logger = logging.getLogger(__name__)

router = APIRouter()


@router.post("/message", status_code=202)
async def receive_message(message: Message, request: Request):
    """
    Generic endpoint to receive text messages.

    Publishes the message to the internal 'message' topic via ZMQ.

    :param message: The message payload.
    :param request: The FastAPI request object (used to access app state).
    """
    logger.info("Received message: %s", message.message)

    topic = b"message"
    body = message.model_dump_json().encode("utf-8")

    pub_socket = request.app.state.endpoints_pub_socket
    await pub_socket.send_multipart([topic, body])

    return {"status": "Message received"}
