syntax = "proto3";

import "google/protobuf/empty.proto";
import "google/protobuf/timestamp.proto";
import "metadata.proto";
import "nack_options.proto";


package sink.v1;

service Sink {
  // SinkFn writes the request to a user defined sink.
  rpc SinkFn(stream SinkRequest) returns (stream SinkResponse);

  // IsReady is the heartbeat endpoint for gRPC.
  rpc IsReady(google.protobuf.Empty) returns (ReadyResponse);
}

/**
 * SinkRequest represents a request element.
 */
message SinkRequest {
  message Request {
    repeated string keys = 1;
    bytes value = 2;
    google.protobuf.Timestamp event_time = 3;
    google.protobuf.Timestamp watermark = 4;
    string id = 5;
    map<string, string> headers = 6;
    // Metadata is the metadata of the message
    metadata.Metadata metadata = 7;
  }
  // Required field indicating the request.
  Request request = 1;
  // Required field indicating the status of the request.
  // If eot is set to true, it indicates the end of transmission.
  TransmissionStatus status = 2;
  // optional field indicating the handshake message.
  optional Handshake handshake = 3;
}

/*
 * Handshake message between client and server to indicate the start of transmission.
 */
message Handshake {
  // Required field indicating the start of transmission.
  bool sot = 1;
}

/**
 * ReadyResponse is the health check result.
 */
message ReadyResponse {
  bool ready = 1;
}

/**
 * TransmissionStatus is the status of the transmission.
 */
message TransmissionStatus {
  bool eot = 1;
}

/*
  * Status is the status of the response.
 */
enum Status {
  SUCCESS = 0;
  FAILURE = 1;
  FALLBACK = 2;
  SERVE = 3;
  ON_SUCCESS = 4;
  NACK = 5;
}

/**
 * SinkResponse is the individual response of each message written to the sink.
 */
message SinkResponse {
  message Result {
    message Message {
      bytes value = 1;
      repeated string keys = 2;
      metadata.Metadata metadata = 3;
    }
    // id is the ID of the message, can be used to uniquely identify the message.
    string id = 1;
    // status denotes the status of persisting to sink. It can be SUCCESS, FAILURE, or FALLBACK.
    Status status = 2;
    // err_msg is the error message, set it if success is set to false.
    string err_msg = 3;
    optional bytes serve_response = 4;
    // on_success_msg is the message to be sent to on_success sink.
    optional Message on_success_msg = 5;
    // Per message options to nack back to the source with.
    optional nack_options.NackOptions nack_options = 6;
  }
  repeated Result results = 1;
  optional Handshake handshake = 2;
  optional TransmissionStatus status = 3;
}