syntax = "proto3";

package ricedb;

service RiceDB {
  // Auth
  rpc Login(LoginRequest) returns (LoginResponse);
  rpc CreateUser(CreateUserRequest) returns (CreateUserResponse);
  rpc DeleteUser(DeleteUserRequest) returns (DeleteUserResponse);

  // Health check
  rpc Health(HealthRequest) returns (HealthResponse);

  // Insert operations
  rpc Insert(InsertRequest) returns (InsertResponse);
  rpc BatchInsert(stream InsertRequest) returns (BatchInsertResponse);

  // Get/Delete Node
  rpc GetNode(GetNodeRequest) returns (GetNodeResponse);
  rpc DeleteNode(DeleteNodeRequest) returns (DeleteNodeResponse);

  // Search operations
  rpc Search(SearchRequest) returns (SearchResponse);
  rpc StreamSearch(SearchRequest) returns (stream SearchResult);

  // ACL Operations
  rpc GrantPermission(GrantPermissionRequest) returns (GrantPermissionResponse);
  rpc RevokePermission(RevokePermissionRequest) returns (RevokePermissionResponse);

  // Graph Operations
  rpc AddEdge(AddEdgeRequest) returns (AddEdgeResponse);
  rpc GetNeighbors(GetNeighborsRequest) returns (GetNeighborsResponse);
  rpc TraverseGraph(TraverseGraphRequest) returns (TraverseGraphResponse);

  // PubSub
  rpc Subscribe(SubscribeRequest) returns (stream PubSubEvent);

  // SDM operations
  rpc WriteMemory(WriteMemoryRequest) returns (WriteMemoryResponse);
  rpc ReadMemory(ReadMemoryRequest) returns (ReadMemoryResponse);

  // Agent Memory (Scratchpad)
  rpc AddMemory(AddMemoryRequest) returns (AddMemoryResponse);
  rpc GetMemory(GetMemoryRequest) returns (GetMemoryResponse);
  rpc ClearMemory(ClearMemoryRequest) returns (ClearMemoryResponse);
  rpc WatchMemory(WatchMemoryRequest) returns (stream MemoryEvent);

  // Cortex Session Operations
  rpc CreateSession(CreateSessionRequest) returns (CreateSessionResponse);
  rpc SnapshotSession(SnapshotSessionRequest) returns (SnapshotSessionResponse);
  rpc LoadSession(LoadSessionRequest) returns (LoadSessionResponse);
  rpc CommitSession(CommitSessionRequest) returns (CommitSessionResponse);
  rpc DropSession(DropSessionRequest) returns (DropSessionResponse);
}

// Auth
message LoginRequest {
  string username = 1;
  string password = 2;
}

message LoginResponse {
  string token = 1;
  uint64 userId = 2;
  string role = 3;
}

message CreateUserRequest {
  string username = 1;
  string password = 2;
  string role = 3;
}

message CreateUserResponse {
  uint64 userId = 1;
  string message = 2;
}

message DeleteUserRequest {
  string username = 1;
}

message DeleteUserResponse {
  bool success = 1;
  string message = 2;
}

// Health check
message HealthRequest {}

message HealthResponse {
  string status = 1;
  string version = 2;
}

// Node
message Node {
  uint64 id = 1;
  reserved 2; // repeated float vector = 2; // Deprecated
  bytes metadata = 3; // JSON-encoded metadata
}

// Insert operations
message InsertRequest {
  uint64 id = 1;
  string text = 2; // Raw text input for HDC encoding
  bytes metadata = 3; // JSON-encoded metadata
  uint64 userId = 4;
  optional string sessionId = 5;
}

message InsertResponse {
  bool success = 1;
  uint64 nodeId = 2;
  string message = 3;
}

message BatchInsertResponse {
  uint32 count = 1;
  repeated uint64 nodeIds = 2;
}

// Get/Delete Node
message GetNodeRequest {
  uint64 nodeId = 1;
}

message GetNodeResponse {
  Node node = 1;
}

message DeleteNodeRequest {
  uint64 nodeId = 1;
  optional string sessionId = 2;
}

message DeleteNodeResponse {
  bool success = 1;
  string message = 2;
}

// Search operations
message SearchRequest {
  string queryText = 1; // Raw text query for HDC encoding
  uint64 userId = 2;
  uint32 k = 3;
  optional string sessionId = 4;
  string filter = 5; // JSON-encoded metadata filter
}

message SearchResponse {
  repeated SearchResult results = 1;
}

message SearchResult {
  uint64 id = 1;
  float similarity = 2;
  bytes metadata = 3; // JSON-encoded metadata
}

// ACL Operations
message Permissions {
  bool read = 1;
  bool write = 2;
  bool delete = 3;
}

message GrantPermissionRequest {
  uint64 nodeId = 1;
  uint64 targetUserId = 2;
  Permissions permissions = 3;
}

message GrantPermissionResponse {
  bool success = 1;
  string message = 2;
}

message RevokePermissionRequest {
  uint64 nodeId = 1;
  uint64 targetUserId = 2;
}

message RevokePermissionResponse {
  bool success = 1;
  string message = 2;
}

// Graph Operations
message AddEdgeRequest {
  uint64 from = 1;
  uint64 to = 2;
  string relation = 3;
  float weight = 4;
}

message AddEdgeResponse {
  bool success = 1;
  string message = 2;
}

message GetNeighborsRequest {
  uint64 nodeId = 1;
  optional string relation = 2;
}

message GetNeighborsResponse {
  uint64 nodeId = 1;
  repeated uint64 neighbors = 2;
}

message TraverseGraphRequest {
  uint64 start = 1;
  uint32 maxDepth = 2;
}

message TraverseGraphResponse {
  uint64 start = 1;
  repeated uint64 visited = 2;
}

// PubSub
message SubscribeRequest {
  string filterType = 1; // "all", "node", "semantic"
  optional uint64 nodeId = 2;
  repeated float vector = 3; // Deprecated
  optional float threshold = 4;
  string queryText = 5; // Text to encode for semantic subscription
}

message PubSubEvent {
  string type = 1; // "inserted", "updated", "deleted"
  Node node = 2; // For inserted/updated
  uint64 nodeId = 3; // For deleted
}

// SDM operations
message BitVector {
  repeated uint64 chunks = 1; // 16 chunks of 64 bits = 1024 bits
}

message WriteMemoryRequest {
  BitVector address = 1;
  BitVector data = 2;
  uint64 userId = 3;
}

message WriteMemoryResponse {
  bool success = 1;
  string message = 2;
}

message ReadMemoryRequest {
  BitVector address = 1;
  uint64 userId = 2;
}

message ReadMemoryResponse {
  BitVector data = 1;
}

// Agent Memory (Scratchpad)
message MemoryEntry {
  string id = 1;
  string sessionId = 2;
  string agentId = 3;
  string content = 4;
  int64 timestamp = 5;
  map<string, string> metadata = 6;
  optional int64 expiresAt = 7;
}

message AddMemoryRequest {
  string sessionId = 1;
  string agentId = 2;
  string content = 3;
  map<string, string> metadata = 4;
  optional int64 ttlSeconds = 5;
}

message AddMemoryResponse {
  bool success = 1;
  string message = 2;
  MemoryEntry entry = 3;
}

message GetMemoryRequest {
  string sessionId = 1;
  uint32 limit = 2;
  int64 afterTimestamp = 3;
  map<string, string> filter = 4;
}

message GetMemoryResponse {
  repeated MemoryEntry entries = 1;
}

message ClearMemoryRequest {
  string sessionId = 1;
}

message ClearMemoryResponse {
  bool success = 1;
  string message = 2;
}

message WatchMemoryRequest {
  string sessionId = 1;
}

message MemoryEvent {
  string type = 1;
  MemoryEntry entry = 2;
}

// Cortex Session Operations
message CreateSessionRequest {
  optional string parentSessionId = 1;
}

message CreateSessionResponse {
  string sessionId = 1;
}

message SnapshotSessionRequest {
  string sessionId = 1;
  string path = 2;
}

message SnapshotSessionResponse {
  bool success = 1;
}

message LoadSessionRequest {
  string path = 1;
}

message LoadSessionResponse {
  string sessionId = 1;
}

message CommitSessionRequest {
  string sessionId = 1;
  string mergeStrategy = 2;
}

message CommitSessionResponse {
  bool success = 1;
}

message DropSessionRequest {
  string sessionId = 1;
}

message DropSessionResponse {
  bool success = 1;
}
