syntax = "proto3";

package saengra_api;

message Vertex {
  bytes type_name = 1;
  bytes value = 2;
}

message Edge {
  Vertex from = 1;
  bytes label = 2;
  Vertex to = 3;
}

message Position {
  Vertex vertex = 1;
  PositionKind kind = 2;

  enum PositionKind {
    CORE = 0;
    ORBIT = 1;
  }
}

message Subgraph {
  Position start_position = 1;
  repeated Position end_positions = 2;
  repeated Vertex vertices = 3;
  repeated Edge edges = 4;
  map<string, Vertex> refs = 5;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

message Request {
  oneof request {
    Connect connect = 1;
    ApplyUpdates apply_updates = 2;
    Commit commit = 3;
    Rollback rollback = 4;
    Observe observe = 5;
    Find find = 6;
    Match match = 7;
  }
}

message Connect {
  // Connect to a graph, create new if doesn't exist.
  string graph_id = 1;
}

message ApplyUpdates {
  // Apply sequence of updates to the currently connected graph.
  repeated Update updates = 1;

  message Update {
    UpdateKind kind = 1;
    Vertex from = 2;
    optional string label = 3;
    optional Vertex to = 4;

    enum UpdateKind {
      ADD_VERTEX = 0;
      ADD_EDGE = 1;
      REMOVE_VERTEX = 2;
      REMOVE_EDGE = 3;
      REMOVE_EDGES_TO_ALL = 4;
    }
  }
}

message Commit {
  // Commit pending changes in the currently connected graph.
}

message Rollback {
  // Rollback pending changes in the currently connected graph.
}

message Observe {
  // Register observers for given expressions.
  repeated Observer observers = 1;

  message Observer {
    string id = 1;
    string expression = 2;
    repeated Vertex placeholder_values = 3;
    bool on_create = 4;
    bool on_change = 5;
    bool on_delete = 6;
  }
}

message Find {
  bool vertices = 1;
  optional string with_type_name = 2;
  bool edges = 3;
  optional string with_label = 4;
  repeated Vertex from = 6;
  repeated Vertex to = 5;
}

message Match {
  string expression = 1;
  repeated Vertex placeholder_values = 2;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

message Response {
  oneof response {
    ConnectResponse connect = 1;
    ApplyUpdatesResponse apply_updates = 2;
    CommitResponse commit = 3;
    RollbackResponse rollback = 4;
    ObserveResponse observe = 5;
    FindResponse find = 6;
    MatchResponse match = 7;
  }
}

message ConnectResponse {
  Status status = 1;
  bool created = 2;
  enum Status { OK = 0; NOT_CONNECTED = 1; }
}

message ApplyUpdatesResponse {
  Status status = 1;
  enum Status { OK = 0; NOT_CONNECTED = 1; }
}

message CommitResponse {
  Status status = 1;
  repeated Observation observation = 2;

  enum Status {
    OK = 0;
    NOT_CONNECTED = 1;
    REQUIRES_REPEATED_COMMIT = 2;
  }

  message Observation {
    string observer_id = 1;
    ObservationType type = 2;
    map<string, Vertex> variables = 3;

    enum ObservationType {
      ON_CREATE = 0;
      ON_CHANGE = 1;
      ON_DELETE = 2;
    }
  }
}

message RollbackResponse {
  Status status = 1;
  enum Status { OK = 0; NOT_CONNECTED = 1; }
}

message ObserveResponse {
  Status status = 1;
  enum Status { OK = 0; NOT_CONNECTED = 1; }
}

message FindResponse {
  Status status = 1;
  repeated Vertex vertices = 2;
  repeated Edge edges = 3;

  enum Status { OK = 0; NOT_CONNECTED = 1; }
}

message MatchResponse {
  Status status = 1;
  repeated Subgraph subgraphs = 2;

  enum Status { OK = 0; NOT_CONNECTED = 1; }
}
