syntax = "proto3";
package gateway;
option go_package = "agent-gateway/internal/grpc/pb";

// Bidirectional streaming service for agent communication
service AgentGateway {
  // Main bidirectional stream for all agent communication
  rpc AgentStream(stream ClientMessage) returns (stream ServerMessage);
}

// Client -> Server messages
message ClientMessage {
  oneof payload {
    AuthRequest auth = 1;
    SubscribeRequest subscribe = 2;
    UnsubscribeRequest unsubscribe = 3;
    ProduceRequest produce = 4;
    PingRequest ping = 5;
  }
}

// Server -> Client messages
message ServerMessage {
  oneof payload {
    AuthResponse auth_response = 1;
    KafkaMessage message = 2;
    ProduceAck produce_ack = 3;
    SubscribeAck subscribe_ack = 4;
    UnsubscribeAck unsubscribe_ack = 5;
    PongResponse pong = 6;
    ErrorResponse error = 7;
  }
}

message AuthRequest {
  string api_key = 1;
  repeated string agent_names = 2;
}

message AuthResponse {
  string connection_id = 1;
  repeated string subscribed_topics = 2;
}

message SubscribeRequest {
  string agent_name = 1;
}

message SubscribeAck {
  string topic = 1;
}

message UnsubscribeRequest {
  string agent_name = 1;
}

message UnsubscribeAck {
  string agent_name = 1;
}

message ProduceRequest {
  string topic = 1;
  string key = 2;
  map<string, string> headers = 3;
  bytes body = 4;  // JSON as bytes
  string correlation_id = 5;
}

message ProduceAck {
  string topic = 1;
  int32 partition = 2;
  int64 offset = 3;
  string correlation_id = 4;
}

message KafkaMessage {
  string topic = 1;
  int32 partition = 2;
  int64 offset = 3;
  string key = 4;
  map<string, string> headers = 5;
  bytes body = 6;  // JSON as bytes
  int64 timestamp = 7;
}

message PingRequest {
  int64 timestamp = 1;
}

message PongResponse {
  int64 client_timestamp = 1;
  int64 server_timestamp = 2;
}

message ErrorResponse {
  string code = 1;
  string message = 2;
  string correlation_id = 3;
}
