syntax = "proto3";

package nominal.edge.v1;

import "google/api/annotations.proto";
import "nominal/gen/v1/alias.proto";
import "nominal/gen/v1/error.proto";

option java_multiple_files = true;
option java_package = "io.nominal.edge.v1";

message CreateDatasetSyncRequest {}

message CreateSyncRequest {
  // rid in the local environment
  string source_rid = 1 [(nominal.gen.v1.alias) = {java_name: "io.nominal.api.rids.EdgeSyncSourceRid"}];
  // rid in the data center environment
  string target_rid = 2 [(nominal.gen.v1.alias) = {java_name: "io.nominal.api.rids.EdgeSyncTargetRid"}];
  // secret to be used for communication with the data center
  string secret_rid = 3 [(nominal.gen.v1.alias) = {java_name: "io.nominal.secrets.api.SecretRid"}];
  // whether or not the sync should be enabled
  bool enabled = 4;
  oneof request {
    CreateDatasetSyncRequest dataset = 5;
  }
}

message CreateSyncResponse {}

message GetSyncRequest {
  string source_rid = 1 [(nominal.gen.v1.alias) = {java_name: "io.nominal.api.rids.EdgeSyncSourceRid"}];
}

message DatasetSync {}

message Sync {
  string source_rid = 1 [(nominal.gen.v1.alias) = {java_name: "io.nominal.api.rids.EdgeSyncSourceRid"}];
  string target_rid = 2 [(nominal.gen.v1.alias) = {java_name: "io.nominal.api.rids.EdgeSyncTargetRid"}];
  string secret_rid = 3 [(nominal.gen.v1.alias) = {java_name: "io.nominal.secrets.api.SecretRid"}];
  bool enabled = 4;
  oneof sync {
    DatasetSync dataset = 5;
  }
}

message GetSyncResponse {
  Sync sync = 1;
}

message UpdateSyncRequest {
  string source_rid = 1 [(nominal.gen.v1.alias) = {java_name: "io.nominal.api.rids.EdgeSyncSourceRid"}];
  optional string target_rid = 2 [(nominal.gen.v1.alias) = {java_name: "io.nominal.api.rids.EdgeSyncTargetRid"}];
  optional string secret_rid = 3 [(nominal.gen.v1.alias) = {java_name: "io.nominal.secrets.api.SecretRid"}];
  optional bool enabled = 4;
}

message UpdateSyncResponse {
  Sync sync = 1;
}

message DeleteSyncRequest {
  string source_rid = 1 [(nominal.gen.v1.alias) = {java_name: "io.nominal.api.rids.EdgeSyncSourceRid"}];
}

message DeleteSyncResponse {}

// buf:lint:ignore ENUM_ZERO_VALUE_SUFFIX
enum EdgeSyncServiceError {
  EDGE_SYNC_SERVICE_ERROR_SYNC_NOT_FOUND = 0 [(nominal.gen.v1.error) = {
    code: "NOT_FOUND"
    message: "SyncNotFound"
  }];
  EDGE_SYNC_SERVICE_ERROR_SYNC_ALREADY_EXISTS = 1 [(nominal.gen.v1.error) = {
    code: "ALREADY_EXISTS"
    message: "SyncAlreadyExists"
  }];
}

// Manages resource syncing from the edge side.
service EdgeSyncService {
  // Creates a sync which allows the specified dataset to be backfilled from edge environments.
  // Throws EDGE_SYNC_SERVICE_ERROR_SYNC_ALREADY_EXISTS if a sync already exists.
  rpc CreateSync(CreateSyncRequest) returns (CreateSyncResponse) {
    option (google.api.http) = {
      put: "/edge/sync/v1/{source_rid}"
      body: "*"
    };
  }

  // Retrieves a sync for the requested resource, throwing EDGE_SYNC_SERVICE_ERROR_SYNC_NOT_FOUND if there is no sync.
  rpc GetSync(GetSyncRequest) returns (GetSyncResponse) {
    option (google.api.http) = {get: "/edge/sync/v1/{source_rid}"};
  }

  // Updates a sync for the requested resource, throwing EDGE_SYNC_SERVICE_ERROR_SYNC_NOT_FOUND if there is no sync.
  rpc UpdateSync(UpdateSyncRequest) returns (UpdateSyncResponse) {
    option (google.api.http) = {post: "/edge/sync/v1/{source_rid}"};
  }

  // Deletes a sync which allows the specified resource to be backfilled from edge environments.
  rpc DeleteSync(DeleteSyncRequest) returns (DeleteSyncResponse) {
    option (google.api.http) = {delete: "/edge/sync/v1/{source_rid}"};
  }
}
