"""
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.program import Program

logger = logging.getLogger(__name__)
router = APIRouter()


@router.post("/program", status_code=202)
async def receive_message(program: Program, request: Request):
    """
    Endpoint to upload a new Behavior Program.

    Validates the program structure (phases, norms, goals) and publishes it to the internal
    'program' topic. The :class:`~control_backend.agents.bdi.bdi_program_manager.BDIProgramManager`
    will pick this up and update the BDI agent.

    :param program: The parsed Program object.
    :param request: The FastAPI request object.
    """
    logger.debug("Received raw program: %s", program)

    # send away
    topic = b"program"
    body = program.model_dump_json().encode()
    pub_socket = request.app.state.endpoints_pub_socket
    await pub_socket.send_multipart([topic, body])

    return {"status": "Program parsed"}
