syntax = "proto3";

package devvit.plugin.scheduler;

import "devvit/actor/scheduler/action.proto";
import "devvit/runtime/runtime_common.proto";
import "google/protobuf/empty.proto";
import "google/protobuf/timestamp.proto";

option go_package = "github.snooguts.net/reddit/reddit-devplatform-monorepo/go-common/generated/protos/types/devvit/plugin/scheduler";
option java_package = "com.reddit.devvit.plugin.scheduler";

message ScheduledActionRequest {
  oneof schedule {
    // A time in the future to execute the action
    google.protobuf.Timestamp when = 1;

    // A crontab parseable string
    string cron = 2;
  }

  // Action data to send back to the Actor
  devvit.actor.scheduler.ScheduledAction action = 3;
}

message ScheduledActionResponse {
  // Opaque ID for the scheduled action
  string id = 1;
}

message CancelActionRequest {
  // Opaque ID returned from a previous call to Schedule()
  string id = 1;
}

message StorableAction {
  string id = 1;
  string actor_hostname = 2;
  ScheduledActionRequest request = 3;
  map<string, devvit.runtime.Strings> metadata = 9;

  enum State {
    WAITING = 0;
    CANCELED = 1;
    FAILED = 2;
    DELIVERED = 3;
  }
  optional State state = 10;
}

message ListActionRequest {
  google.protobuf.Timestamp before = 1;
  google.protobuf.Timestamp after = 2;
}

message GetActionResponse {
  string id = 1;
  ScheduledActionRequest request = 2;
}

message ListActionResponse {
  repeated GetActionResponse actions = 1;
}

// Provides an API for Actors to execute an action sometime in the future.
//
// Actors will need to implement the SchedulerHandler interface which the Scheduler will call with
// the provided ScheduledAction as an argument.
//
// The Scheduler will fail with an error if the time provided is in the past.
service Scheduler {
  // This schedules an action
  rpc Schedule(ScheduledActionRequest) returns (ScheduledActionResponse);

  // This cancels any scheduled action
  rpc Cancel(CancelActionRequest) returns (google.protobuf.Empty);

  // This lists all scheduled actions for this installation.
  rpc List(ListActionRequest) returns (ListActionResponse);
}
