// Copyright AGNTCY Contributors (https://github.com/agntcy)
// SPDX-License-Identifier: Apache-2.0

syntax = "proto3";
package pubsub.proto.v1;

service PubSubService {
  rpc OpenChannel(stream Message) returns (stream Message) {}
}

message Subscribe {
  SLIMHeader header = 1;
  string component_0 = 2;
  string component_1 = 3;
  string component_2 = 4;
}

message Unsubscribe { SLIMHeader header = 1; }

message Publish {
  SLIMHeader header = 1;
  SessionHeader session = 2;
  Content msg = 4;
}

// recvFrom = connection from where the sub/unsub is supposed to be received
// forwardTo = connection where to forward the message
// incomingConn = connection from where the packet was received
// error = if true the publication contains an error notification
message SLIMHeader {
  Name source = 1;
  Name destination = 2;
  uint32 fanout = 3;
  optional uint64 recvFrom = 4;
  optional uint64 forwardTo = 5;
  optional uint64 incomingConn = 6;
  optional bool error = 7;
}

message Name {
  uint64 component_0 = 1;
  uint64 component_1 = 2;
  uint64 component_2 = 3;
  uint64 component_3 = 4;
}

enum SessionType {
  SESSION_UNKNOWN = 0;
  SESSION_FIRE_FORGET = 1;
  SESSION_STREAMING = 2;
  SESSION_PUB_SUB = 3;
}

enum SessionMessageType {
  UNSPECIFIED = 0;
  FNF_MSG = 1;
  FNF_RELIABLE = 2;
  FNF_ACK = 3;
  STREAM_MSG = 4;
  PUB_SUB_MSG = 5;
  RTX_REQUEST = 6;
  RTX_REPLY = 7;
  BEACON_STREAM = 8;
  BEACON_PUB_SUB = 9;
  CHANNEL_DISCOVERY_REQUEST = 10;
  CHANNEL_DISCOVERY_REPLY = 11;
  CHANNEL_JOIN_REQUEST = 12;
  CHANNEL_JOIN_REPLY = 13;
  CHANNEL_LEAVE_REQUEST = 14;
  CHANNEL_LEAVE_REPLY = 15;
  CHANNEL_MLS_COMMIT = 16;
  CHANNEL_MLS_WELCOME = 17;
  CHANNEL_MLS_PROPOSAL = 18;
  CHANNEL_MLS_ACK = 19;
}
// Session.session_id is the ID of the session

// Session.message_id meaning according to the SessionMessageType
// FNF = nonce
// FNF_RELIABLE = nonce
// FNF_ACK = nonce of the received FNF_RELIABLE message
// REQUEST = nonce
// REPLY = nonce of the associated REQUEST
// STREAM = sequential incremental ID
// PUBSUB = sequential incremental ID
// RTX_REQUEST = ID of the lost packet
// RTX_REPLY = ID of the retransmitted packet
// BEACON_STREAM = ID of the last message sent by the producer
// BEACON_PUB_SUB = ID of the last message sent by the producer
// JOIN_REQUEST = nonce
// JOIN_REPLY = nonce of the associated JOIN_REQUEST
// LEAVE_REQUEST = nonce
// LEAVE_REPLY = nonce of the associated LEAVE_REQUEST

message SessionHeader {
  SessionType session_type = 1;
  SessionMessageType session_message_type = 2;
  uint32 session_id = 3;
  uint32 message_id = 4;
}

message Content {
  string content_type = 1;
  bytes blob = 2;
}

message Message {
  oneof messageType {
    Subscribe subscribe = 1;
    Unsubscribe unsubscribe = 2;
    Publish publish = 3;
  }
  map<string, string> metadata = 4;
}
