import struct


def create_fragment(
    object_id: int,
    fragment_id: int,
    data: bytes,
    end: bool = True,
) -> bytes:
    """Create a PSRP fragment.

    Creates a PSRP message fragment. The fragment structure is defined in
    `MS-PSRP 2.2.4 Packet Fragment`_.

    Args:
        object_id: The unique ID of the PSRP message to which the fragment
            belongs.
        fragment_id: Identifies where in the sequence of fragments this
            fragment falls.
        data: The PSRP message value to fragment.
        end: Whether this is the last fragment for the PSRP message (True) or
            not (False).

    Returns:
        (bytes): The PSRP fragment.

    .. _MS-PSRP 2.2.4 Packet Fragment:
        https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-psrp/3610dae4-67f7-4175-82da-a3fab83af288
    """
    start_end_byte = 0
    if fragment_id == 0:
        start_end_byte |= 0x1
    if end:
        start_end_byte |= 0x2

    return b"".join(
        [
            struct.pack(">Q", object_id),
            struct.pack(">Q", fragment_id),
            struct.pack("B", start_end_byte),
            struct.pack(">I", len(data)),
            data,
        ]
    )