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

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

import "google/protobuf/wrappers.proto";

option go_package = "github.com/agntcy/slim/control-plane/common/proto/controller/v1;controllerv1";

service ControllerService {
  rpc OpenControlChannel(stream ControlMessage) returns (stream ControlMessage) {}
}

message ControlMessage {
  string message_id = 1;

  oneof payload {
    ConfigurationCommand config_command = 2;
    Ack ack = 3;
    SubscriptionListRequest subscription_list_request = 4;
    SubscriptionListResponse subscription_list_response = 5;
    ConnectionListRequest connection_list_request = 6;
    ConnectionListResponse connection_list_response = 7;
    RegisterNodeRequest register_node_request = 8;
    RegisterNodeResponse register_node_response = 9;
    DeregisterNodeRequest deregister_node_request = 10;
    DeregisterNodeResponse deregister_node_response = 11;

    CreateChannelRequest create_channel_request = 12;
    CreateChannelResponse create_channel_response = 13;
    DeleteChannelRequest delete_channel_request = 14;
    AddParticipantRequest add_participant_request = 15;
    DeleteParticipantRequest delete_participant_request = 16;
    ListChannelsRequest list_channel_request = 17;
    ListChannelsResponse list_channel_response = 18;
    ListParticipantsRequest list_participants_request = 19;
    ListParticipantsResponse list_participants_response = 20;
  }
}

message Connection {
  string connection_id = 1;
  string config_data = 2;
}

message Subscription {
  string component_0 = 1;
  string component_1 = 2;
  string component_2 = 3;
  google.protobuf.UInt64Value id = 4;
  string connection_id = 5;
}

message ConfigurationCommand {
  repeated Connection connections_to_create = 1;
  repeated Subscription subscriptions_to_set = 2;
  repeated Subscription subscriptions_to_delete = 3;
}

message Ack {
  string original_message_id = 1;
  bool success = 2;
  repeated string messages = 3;
}

message SubscriptionListRequest {}

message SubscriptionListResponse {
  repeated SubscriptionEntry entries = 1;
}

message SubscriptionEntry {
  string component_0 = 1;
  string component_1 = 2;
  string component_2 = 3;
  google.protobuf.UInt64Value id = 4;
  repeated ConnectionEntry local_connections = 5;
  repeated ConnectionEntry remote_connections = 6;
}

enum ConnectionType {
  CONNECTION_TYPE_LOCAL = 0;
  CONNECTION_TYPE_REMOTE = 1;
}

message ConnectionEntry {
  uint64 id = 1;
  ConnectionType connection_type = 2;
  string config_data = 3;
}

message ConnectionListRequest {}

message ConnectionListResponse {
  repeated ConnectionEntry entries = 1;
}

message Node {
  string id = 1;
}

message RegisterNodeRequest {
  string nodeId = 1;
}

message RegisterNodeResponse {
  bool success = 1;
}

message DeregisterNodeRequest {
  Node node = 1;
}

message DeregisterNodeResponse {
  bool success = 1;
}

message CreateChannelRequest {
  // list of moderators for the channel (e.g. spiffe SVID)
  repeated string moderators = 1;
}

message CreateChannelResponse {
  // ID of the channel, to which clients will subscribe (e.g. spiffe SVID)
  string channel_id = 1;
}

message DeleteChannelRequest {
  // ID of the channel
  string channel_id = 1;
}

message AddParticipantRequest {
  // ID of the channel
  string channel_id = 1;

  // ID of participant (e.g. spiffe SVID)
  string participant_id = 2;
}

message DeleteParticipantRequest {
  // ID of the channel
  string channel_id = 1;

  // ID of participant (e.g. spiffe SVID)
  string participant_id = 2;
}

message ListChannelsRequest {}

message ListChannelsResponse {
  // IDs of the channels available in the control plane
  repeated string channel_id = 1;
}

message ListParticipantsRequest {
  // ID of the channel
  string channel_id = 1;
}

message ListParticipantsResponse {
  // list of participants in the channel
  repeated string participant_id = 1;
}
