// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation and Dapr Contributors.
// Licensed under the MIT License.
// ------------------------------------------------------------

syntax = "proto3";

package dapr.proto.runtime.v1;

import "google/protobuf/empty.proto";
import "dapr/proto/common/v1/common.proto";

option csharp_namespace = "Dapr.AppCallback.Autogen.Grpc.v1";
option java_outer_classname = "DaprAppCallbackProtos";
option java_package = "io.dapr.v1";
option go_package = "github.com/dapr/dapr/pkg/proto/runtime/v1;runtime";

// AppCallback V1 allows user application to interact with Dapr runtime.
// User application needs to implement AppCallback service if it needs to
// receive message from dapr runtime.
service AppCallback {
  // Invokes service method with InvokeRequest.
  rpc OnInvoke (common.v1.InvokeRequest) returns (common.v1.InvokeResponse) {}

  // Lists all topics subscribed by this app.
  rpc ListTopicSubscriptions(google.protobuf.Empty) returns (ListTopicSubscriptionsResponse) {}

  // Subscribes events from Pubsub
  rpc OnTopicEvent(TopicEventRequest) returns (TopicEventResponse) {}

  // Lists all input bindings subscribed by this app.
  rpc ListInputBindings(google.protobuf.Empty) returns (ListInputBindingsResponse) {}

  // Listens events from the input bindings
  //
  // User application can save the states or send the events to the output
  // bindings optionally by returning BindingEventResponse.
  rpc OnBindingEvent(BindingEventRequest) returns (BindingEventResponse) {}
}

// TopicEventRequest message is compatible with CloudEvent spec v1.0
// https://github.com/cloudevents/spec/blob/v1.0/spec.md
message TopicEventRequest {
  // id identifies the event. Producers MUST ensure that source + id 
  // is unique for each distinct event. If a duplicate event is re-sent
  // (e.g. due to a network error) it MAY have the same id. 
  string id = 1;

  // source identifies the context in which an event happened.
  // Often this will include information such as the type of the
  // event source, the organization publishing the event or the process
  // that produced the event. The exact syntax and semantics behind
  // the data encoded in the URI is defined by the event producer.
  string source = 2;

  // The type of event related to the originating occurrence. 
  string type = 3;

  // The version of the CloudEvents specification. 
  string spec_version = 4;

  // The content type of data value.
  string data_content_type = 5;

  // The content of the event.
  bytes data = 7;

  // The pubsub topic which publisher sent to.
  string topic = 6;

  // The name of the pubsub the publisher sent to.
  string pubsub_name = 8;
}

// TopicEventResponse is response from app on published message
message TopicEventResponse {
  // TopicEventResponseStatus allows apps to have finer control over handling of the message.
  enum TopicEventResponseStatus {
    // SUCCESS is the default behavior: message is acknowledged and not retried or logged.
    SUCCESS = 0;
    // RETRY status signals Dapr to retry the message as part of an expected scenario (no warning is logged).
    RETRY = 1;
    // DROP status signals Dapr to drop the message as part of an unexpected scenario (warning is logged).
    DROP = 2;
  }

  // The list of output bindings.
  TopicEventResponseStatus status = 1;
}

// BindingEventRequest represents input bindings event.
message BindingEventRequest {
  // Required. The name of the input binding component.
  string name = 1;

  // Required. The payload that the input bindings sent
  bytes data = 2;

  // The metadata set by the input binging components.
  map<string,string> metadata = 3;
}

// BindingEventResponse includes operations to save state or
// send data to output bindings optionally.
message BindingEventResponse {
  // The name of state store where states are saved.
  string store_name = 1;

  // The state key values which will be stored in store_name.
  repeated common.v1.StateItem states = 2;

  // BindingEventConcurrency is the kind of concurrency 
  enum BindingEventConcurrency {
    // SEQUENTIAL sends data to output bindings specified in "to" sequentially.
    SEQUENTIAL = 0;
    // PARALLEL sends data to output bindings specified in "to" in parallel.
    PARALLEL = 1;
  }

  // The list of output bindings.
  repeated string to = 3;

  // The content which will be sent to "to" output bindings.
  bytes data = 4;

  // The concurrency of output bindings to send data to
  // "to" output bindings list. The default is SEQUENTIAL.
  BindingEventConcurrency concurrency = 5;
}

// ListTopicSubscriptionsResponse is the message including the list of the subscribing topics.
message ListTopicSubscriptionsResponse {
  // The list of topics.
  repeated TopicSubscription subscriptions = 1;
}

// TopicSubscription represents topic and metadata.
message TopicSubscription {
  // Required. The name of the pubsub containing the topic below to subscribe to.
  string pubsub_name = 1;

  // Required. The name of topic which will be subscribed
  string topic = 2;

  // The optional properties used for this topic's subscription e.g. session id
  map<string,string> metadata = 3;
}

// ListInputBindingsResponse is the message including the list of input bindings.
message ListInputBindingsResponse {
  // The list of input bindings.
  repeated string bindings = 1;
}
