"""Broadcast message module for mt_dao framework."""
import uuid

from flwr.common import (
    Code,
    ConfigRecord,
    RecordDict,
)

from mt_dao.constants import (
    BATCH_ID,
    BROADCAST_INS,
    BROADCAST_MSG,
    BROADCAST_P,
    CODE,
    COMM_STACK,
    ENDPOINT_ID,
    FILE_NAME,
    FOLDER_NAME,
    MASK,
    MESSAGE,
    PARAMETERS,
    QUERY,
    STATUS,
    TYPE,
)


def build_broadcast_recorddict(
    transmission_mask: tuple[tuple[bool, ...], list[str], list[str]],
    batch_id: int = 0,
) -> RecordDict:
    """Build a RecordDict for broadcasting model parameters and mask to clients.

    Parameters
    ----------
    transmission_mask : tuple[tuple[bool, ...], list[str], list[str]]
        A tuple containing:
        - A tuple of booleans indicating which parameters to transmit.
        - A list of layer names.
        - A list of layer types.
    batch_id : int, optional
        The batch identifier for the broadcast operation (default is 0).

    Returns
    -------
    RecordDict
        A RecordDict containing:
        - A broadcast query entry.
        - A status ConfigRecord with code OK and broadcast message.
        - Communication stack instructions including endpoint ID, folder and file names,
          mask, and batch ID.

    Notes
    -----
    This function creates a RecordDict for broadcasting model parameters and the
    associated transmission mask to clients. The communication stack instructions
    are stored under the key "{BROADCAST_INS}.{COMM_STACK}" and include metadata
    required for the broadcast operation.

    """
    # Create an empty RecordDict object
    recorddict = RecordDict()
    # Add the broadcast message to the recorddict
    recorddict.config_records[QUERY] = ConfigRecord({TYPE: BROADCAST_P})
    # Add Status to the recorddict
    recorddict.config_records[f"{BROADCAST_INS}.{STATUS}"] = ConfigRecord(
        {
            CODE: int(Code.OK.value),
            MESSAGE: BROADCAST_MSG,
        },
    )
    # Add communication stack instructions to the recorddict
    recorddict.config_records[f"{BROADCAST_INS}.{COMM_STACK}"] = ConfigRecord(
        {
            ENDPOINT_ID: str(uuid.uuid4()),
            FOLDER_NAME: COMM_STACK,
            FILE_NAME: PARAMETERS,
            MASK: repr(transmission_mask),
            BATCH_ID: batch_id,
        },
    )
    return recorddict
