import logging

from fastapi import APIRouter, Request
from zmq import Socket

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):
    logger.info("Received message: %s", message.message)

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

    pub_socket: Socket = request.app.state.internal_comm_socket

    pub_socket.send_multipart([topic, body])

    return {"status": "Message received"}
