namespace ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Shared.Contracts;

/// <summary>
///     Length-prefixed framing per decision #28. Each IPC frame is:
///     <c>[4-byte big-endian length][1-byte message kind][MessagePack body]</c>.
///     Length is the body size only; the kind byte is not part of the prefixed length.
/// </summary>
public static class Framing
{
    public const int LengthPrefixSize = 4;
    public const int KindByteSize = 1;

    /// <summary>
    ///     Maximum permitted body length (16 MiB). Protects the receiver from a hostile or
    ///     misbehaving peer sending an oversized length prefix.
    /// </summary>
    public const int MaxFrameBodyBytes = 16 * 1024 * 1024;
}

/// <summary>
///     Wire identifier for each contract. Values are stable — new contracts append.
/// </summary>
public enum MessageKind : byte
{
    Hello                      = 0x01,
    HelloAck                   = 0x02,
    Heartbeat                  = 0x03,
    HeartbeatAck               = 0x04,

    OpenSessionRequest         = 0x10,
    OpenSessionResponse        = 0x11,
    CloseSessionRequest        = 0x12,

    DiscoverHierarchyRequest   = 0x20,
    DiscoverHierarchyResponse  = 0x21,

    ReadValuesRequest          = 0x30,
    ReadValuesResponse         = 0x31,
    WriteValuesRequest         = 0x32,
    WriteValuesResponse        = 0x33,

    SubscribeRequest           = 0x40,
    SubscribeResponse          = 0x41,
    UnsubscribeRequest         = 0x42,
    OnDataChangeNotification   = 0x43,

    AlarmSubscribeRequest      = 0x50,
    AlarmEvent                 = 0x51,
    AlarmAckRequest            = 0x52,

    HistoryReadRequest           = 0x60,
    HistoryReadResponse          = 0x61,
    HistoryReadProcessedRequest  = 0x62,
    HistoryReadProcessedResponse = 0x63,
    HistoryReadAtTimeRequest     = 0x64,
    HistoryReadAtTimeResponse    = 0x65,
    HistoryReadEventsRequest     = 0x66,
    HistoryReadEventsResponse    = 0x67,

    HostConnectivityStatus     = 0x70,
    RuntimeStatusChange        = 0x71,

    // Phase 7 Stream D — historian alarm sink. Main server → Galaxy.Host batched
    // writes into the Aveva Historian alarm schema via the already-loaded
    // aahClientManaged DLLs. HistorianConnectivityStatus fires proactively from the
    // Host when the SDK session transitions so diagnostics flip promptly.
    HistorianAlarmEventRequest        = 0x80,
    HistorianAlarmEventResponse       = 0x81,
    HistorianConnectivityStatus       = 0x82,

    RecycleHostRequest         = 0xF0,
    RecycleStatusResponse      = 0xF1,

    ErrorResponse              = 0xFE,
}
