// SPDX-FileCopyrightText: Copyright (c) 2025 Cisco and/or its affiliates.
// SPDX-License-Identifier: Apache-2.0

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

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

message Subscribe {
  AGPHeader header = 1;
}

message Unsubscribe {
  AGPHeader header = 1;
}

message Publish {
  AGPHeader header = 1;
  ServiceHeader control = 2;
  uint32 fanout = 3;
  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 AGPHeader {
  Agent source = 1;
  Agent destination = 2;
  optional uint64 recvFrom = 3;
  optional uint64 forwardTo = 4;
  optional uint64 incomingConn = 5;
  optional bool error = 6;
}

message Agent {
  uint64 organization = 1;
  uint64 namespace = 2;
  uint64 agent_type = 3;
  optional uint64 agent_id = 4;
}

enum ServiceHeaderType {
  CTRL_UNSPECIFIED = 0;
  CTRL_FNF = 1;
  CTRL_REQUEST = 2;
  CTRL_REPLY = 3;
  CTRL_STREAM = 4;
  CTRL_RTX_REQUEST = 5;
  CTRL_RTX_REPLY = 6;
}

// Service.id meaning according to ControlType
// CTRL_FNF = nonce
// CTRL_REQUEST = nonce
// CTRL_REPLY = nonce of the associated CTRL_REQUEST
// CTRL_STREAM = sequential incremental ID
// CTRL_RTX_REQUEST = nonce
// CTRL_RTX_REPLY = nonce of the associated CTRL_RTX_REQUEST

// Service.stream meaning according to ControlType
// CTRL_STREAM = stream for which the id field is valid
// CTRL_RTX_REQUEST = stream for this CTRL_RTX_REQUEST
// CTRL_RTX_REPLY = stream for this CTRL_RTX_REPLY
// None in all the other cases

// Service.rtx meaning according to ControlType
// CTRL_RTX_REQUEST = id of the messing packet to retransmit
// CTRL_RTX_REPLY = id of the retransmitted packet
// None in all the other cases

message ServiceHeader {
  ServiceHeaderType header_type = 1;
  uint32 id = 2;
  optional uint32 stream = 3;
  optional uint32 rtx = 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;
}
