"""MQTT topic helpers for the A2A namespace."""

CONFIG_DEVICES = "a2a/{room}/config/devices"
CONFIG_COMMANDER = "a2a/{room}/config/commander"
AGENT_CARD = "a2a/{room}/agents/{device}/card"
AGENT_STATUS = "a2a/{room}/agents/{device}/status"
CMD_REQUEST = "a2a/{room}/cmd/{device}/request"
CMD_RESPONSE = "a2a/{room}/cmd/{device}/response"
CMD_TASK_STATUS = "a2a/{room}/cmd/{device}/task-status"
PAIRING_REQUEST = "a2a/_pairing/request"
PAIRING_REPLY = "a2a/_pairing/reply/{code}"


def _segment(value: str, name: str) -> str:
    if not value:
        raise ValueError(f"{name} must not be empty")
    if "/" in value:
        raise ValueError(f"{name} must not contain '/'")
    return value


def config_devices_topic(room: str) -> str:
    return CONFIG_DEVICES.format(room=_segment(room, "room"))


def config_commander_topic(room: str) -> str:
    return CONFIG_COMMANDER.format(room=_segment(room, "room"))


def agent_card_topic(room: str, device: str) -> str:
    return AGENT_CARD.format(
        room=_segment(room, "room"),
        device=_segment(device, "device"),
    )


def agent_status_topic(room: str, device: str) -> str:
    return AGENT_STATUS.format(
        room=_segment(room, "room"),
        device=_segment(device, "device"),
    )


def agent_status_filter(room: str) -> str:
    return AGENT_STATUS.format(room=_segment(room, "room"), device="+")


def cmd_request_topic(room: str, device: str) -> str:
    return CMD_REQUEST.format(
        room=_segment(room, "room"),
        device=_segment(device, "device"),
    )


def cmd_response_topic(room: str, device: str) -> str:
    return CMD_RESPONSE.format(
        room=_segment(room, "room"),
        device=_segment(device, "device"),
    )


def cmd_task_status_topic(room: str, device: str) -> str:
    return CMD_TASK_STATUS.format(
        room=_segment(room, "room"),
        device=_segment(device, "device"),
    )


def pairing_request_topic() -> str:
    return PAIRING_REQUEST


def pairing_reply_topic(code: str) -> str:
    return PAIRING_REPLY.format(code=_segment(code, "code"))
