/*
 * Created by jonathan.jesperson@pluto.tv on 10/21/2020.
 * Copyright (c) PlutoTV Inc 2020.
 */

syntax = "proto3";

package timelinepackage;

option go_package = "github.com/Pluto-tv/service-dal/golang_client";
option java_package = "tv.pluto.ip.dal.timeline";

service TimelineManager {
  // GetAll (Server Stream)
  //
  rpc GetAll(GetAllRequest) returns (stream Timeline) {}

  // GetEmptyTimeline (Unary)
  //
  rpc GetEmptyTimeline(GetEmptyTimelineRequest) returns (GetEmptyTimelineResponse) {}

  // GetById (Unary)
  //
  rpc GetById(GetByIdRequest) returns (GetByIdResponse) {}

  // GetByIds (Unary)
  //
  rpc GetByIds(GetByIdsRequest) returns (GetByIdsResponse) {}

  // GetByEpisodeId (Unary)
  //
  rpc GetByEpisodeId(GetByEpisodeIdRequest) returns (GetByEpisodeIdResponse) {}

  // GetDocumentsUpdatedBetweenDatesOnChannel (Server Stream)
  //
  rpc GetDocumentsUpdatedBetweenDatesOnChannel(GetDocumentsUpdatedBetweenDatesOnChannelRequest) returns (stream Timeline) {}

  // GetDocumentsUpdatedBetweenDates (Server Stream)
  // Returns documents greater than start_min and less then start_max
  rpc GetDocumentsBetweenDates(GetDocumentsBetweenDatesRequest) returns (stream Timeline) {}

  // CFeedsSyncDocuments (Server Stream)
  //
  rpc CFeedsSyncDocuments(CFeedsSyncDocumentsRequest) returns (stream Timeline) {}

  // CFeedsSyncAllIds (Server Stream)
  //
  rpc CFeedsSyncAllIds(CFeedsSyncAllIdsRequest) returns (stream Id) {}

  // GetTimelineById (Unary)
  //
  // Deprecated.Will be removed once client (FW IVI) no longer using
  rpc GetTimelineById(GetTimelineByIdRequest) returns (GetTimelineByIdResponse) {}

  // GetOne (Unary)
  //
  rpc GetOne(GetOneRequest) returns (GetOneResponse) {}

  // DeleteById (Unary)
  //
  rpc DeleteById(DeleteByIdRequest) returns (DeleteByIdResponse) {}

  rpc Count(GetCountRequest) returns (GetCountResponse) {}

  rpc GetTimelinesForAutoSchedule(GetTimelinesForAutoScheduleRequest) returns (GetTimelinesForAutoScheduleResponse) {}

  rpc GetTimelinesForRepeatSchedule(GetTimelinesForRepeatScheduleRequest) returns (GetTimelinesForRepeatScheduleResponse) {}

  rpc UpdateTimelinesBulk(UpdateTimelinesBulkRequest) returns (UpdateTimelinesBulkResponse) {}

  rpc GetPopulatedTimelinesByChannelId(GetPopulatedTimelinesByChannelIdRequest) returns (GetPopulatedTimelinesByChannelIdResponse) {}

  rpc DeleteTimelinesInChannelByDates(DeleteTimelinesInChannelByDatesRequest) returns (DeleteTimelinesInChannelByDatesResponse) {}

  rpc Create(CreateRequest) returns (CreateResponse) {}

  rpc GetTimelinesByChannelId(GetTimelinesByChannelIdRequest) returns (GetTimelinesByChannelIdResponse) {}

  rpc GetTimelineIdsByClipId(GetTimelineIdsByClipIdRequest) returns (GetTimelineIdsByClipIdResponse) {}

  rpc GetTimelinesLiteByChannelId(GetTimelinesLiteByChannelIdRequest) returns (GetTimelinesLiteByChannelIdResponse) {}

  // GetDocumentsBetweenStartDates (Server Stream)
  //
  rpc GetDocumentsBetweenStartDates(GetDocumentsBetweenStartDatesRequest) returns (stream Timeline) {}

  // AggregateByEpisodes (Unary)
  rpc AggregateByEpisodes(AggregateByEpisodesRequest) returns (AggregateByEpisodesResponse) {}

  // GetAllBy (Unary)
  //
  rpc GetAllBy(GetAllByRequest) returns (GetAllByResponse) {}
}

message CreateRequest {
  Timeline document = 1;
}

message CreateResponse {
  Timeline document = 1;
}

// documents filtered by start date
message GetDocumentsBetweenDatesRequest {
  // Expressed as a valid javascript Date, https://en.wikipedia.org/wiki/ISO_8601
  // Start is GTE (inclusive)
  string start = 1;
  // Expressed as a valid javascript Date, https://en.wikipedia.org/wiki/ISO_8601
  // Stop is LT
  string stop = 2;
}

message GetDocumentsUpdatedBetweenDatesRequest {
  // Expressed as a valid javascript Date, https://en.wikipedia.org/wiki/ISO_8601
  string start = 1;
  // Expressed as a valid javascript Date, https://en.wikipedia.org/wiki/ISO_8601
  string stop = 2;
}

message DeleteByIdRequest {
  string id = 1;
}

message DeleteByIdResponse {
  int64 deleted_count = 1;
}

message CFeedsSyncAllIdsRequest {
  string limiter_date = 1;
}

message Id {
  string id = 1;
}

message CFeedsSyncDocumentsRequest {
  // Expressed as a valid javascript Date, https://en.wikipedia.org/wiki/ISO_8601
  string last_run_at = 1;
  // Expressed as a valid javascript Date, https://en.wikipedia.org/wiki/ISO_8601
  string limiter_date = 2;
}

message GetDocumentsUpdatedBetweenDatesOnChannelRequest {
  string start = 1;
  string stop = 2;
  repeated string channel_ids = 3;
}

message GetByEpisodeIdRequest {
  string id = 1;
  // how many results to return default 1000
  int32 limit = 2;
  // where to begin page default 0
  int32 offset = 3;
  // how to sort results
  repeated GetAllPagedSort sort = 4;
  // a range to filter the start
  QueryTimeRange start = 5;
  // a range to filter the stop
  QueryTimeRange stop = 6;
  repeated string projection = 7;
}

// QueryTimeRange expresses a range for times
// any of the following values may optional be set
message QueryTimeRange {
  // Expressed as a valid javascript Date, https://en.wikipedia.org/wiki/ISO_8601
  // (Optional) Time greater than to start
  string begin = 1;
  // Expressed as a valid javascript Date, https://en.wikipedia.org/wiki/ISO_8601
  // (Optional) Time less than equals
  string end = 2;
}

message GetByEpisodeIdResponse {
  repeated Timeline documents = 1;
}

message GetByIdRequest {
  string id = 1;
  repeated string projection = 2;
}

// GetAllRequest
// 'region' and 'channel_ids' are mutually exclusive filters with
// `region` having precedence
message GetAllRequest {
  string region = 1;
  repeated string channel_ids = 2;
}

message GetByIdResponse {
  Timeline document = 1;
}

message GetByIdsRequest {
  repeated string ids = 1;
  repeated GetAllPagedSort sort = 2;
  repeated string projection = 3;
}

message GetAllPagedSort {
  // field to sort on
  string field = 1;
  // sort direction 1 | -1
  int32 value = 2;
}

message GetByIdsResponse {
  repeated Timeline documents = 1;
}

message Timeline {
  string id = 1;
  string created_at = 2;
  string updated_at = 3;
  string start = 4;
  string stop = 5;
  string episode = 6;
  string channel = 7;
  bool featured = 8;
  string manual_override_marker = 9;
}

message TimelineAutoSchedule {
  string start = 1;
  string stop = 2;
  string episode = 3;
  string channel = 4;
}

message TimelineRepeatSchedule {
  string start = 1;
  string stop = 2;
  string episode = 3;
  string channel = 4;
  string end = 5;
  int32 allotment = 6;
  int32 duration = 7;
  Series series = 8;
  string title = 9;
  int32 season = 10;
  int32 number = 11;
  bool flagged = 12;
  bool featured = 13;
  string last_aired = 14;
  string first_aired = 15;
  NextTimeline next_timeline = 16;
  string status = 17;
  bool published = 18;
  AvailabilityWindows availability_windows = 19;
  bool has_region_availability = 20;
  bool fulfilled = 21;
  bool provider_is_jw_only = 22;
  bool provider_is_jw_mixed = 23;
  bool has_empty_sources = 24;
  bool has_linear_availability_windows = 25;
  bool has_linear_availability_window_for_date = 26;
}

message GetEmptyTimelineRequest {}

message GetEmptyTimelineResponse {
  EmptyTimeline timeline = 1;
}
message EmptyTimeline {
  string id = 1;
  bool featured = 2;
}

// Deprecated. Will be removed once client (FW IVI) no longer using
message GetTimelineByIdRequest {
  string id = 1;
}

message GetTimelineByIdResponse {
  Timeline timeline = 1;
}

message GetOneSort {
  string field = 1;
  int32 value = 2;
}

message GetOneRequest {
  GetOneSort sort = 2;
}

message GetOneResponse {
  Timeline document = 1;
}

message GetCountRequest {
  string start_initial = 1;
  string start_final = 2;
}

message GetCountResponse {
  int32 count = 1;
}

message Channel {
  string id = 1;
}

message GetTimelinesForAutoScheduleRequest {
  Channel channel = 1;
  string start = 2;
  string stop = 3;
}

message GetTimelinesForAutoScheduleResponse {
  repeated TimelineAutoSchedule timelines = 1;
}

message GetTimelinesForRepeatScheduleRequest {
  Channel channel = 1;
  string start = 2;
  GetTimeslinesForRepeatScheduleParams params = 3;
}

message GetTimeslinesForRepeatScheduleParams {
  string series = 1;
  string cycle = 2;
  int32 season = 3;
  string repeat_interval = 4;
  int32 repeat_period = 5;
  // expects day of week the should be repeated.
  // Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
  repeated string repeat_days = 6;
  string end = 7;
  string end_date = 8;
  int32 occurrences = 9;
  string month_interval = 10;
  string local_day = 11;
  string episode = 12;
}

message GetTimelinesForRepeatScheduleResponse {
  repeated TimelineRepeatSchedule timelines = 1;
}

message UpdateTimelinesBulkRequest {
  repeated string delete_ids = 1;
  repeated Timeline create_objects = 2;
}

message UpdateTimelinesBulkResponse {}

message GetPopulatedTimelinesByChannelIdRequest {
  string channel_id = 1;
  int64 start = 2;
  int64 stop = 3;
  repeated string timelines_to_exclude = 4;
}

message GetPopulatedTimelinesByChannelIdResponse {
  repeated TimelineByChannelId documents = 1;
}

message ClipSource {
  Clip clip = 1;
}

message Author {
  string name = 1;
}

message Episode {
  string id = 1;
  string name = 2;
  int32 allotment = 3;
  int32 duration = 4;
  bool published = 5;
  bool is_feed = 6;
  string status = 7;
  bool flagged = 8;
  Series series = 9;
  int32 number = 10;
  int32 season = 11;
  int32 ad_pods_duration = 12;
  bool featured = 13;
  string first_aired = 14;
  repeated string region_filter = 15;
  string active_region = 16;
  string last_aired = 17;
  NextTimeline next_timeline = 18;
  AvailabilityWindows availability_windows = 19;
  string rating = 20;
}

message NextTimeline {
  string id = 1;
  string start = 2;
  string channel = 3;
}

message Series {
  string id = 1;
  string name = 2;
}

message TimelineByChannelId {
  string id = 1;
  string start = 2;
  string stop = 3;
  Episode episode = 4;
  bool featured = 5;
  string manual_override_marker = 6;
  bool all_clips_ready = 7;
  bool is_available_now = 8;
  bool has_region_availability = 9;
  bool provider_is_jw_only = 10;
  bool provider_is_jw_mixed = 11;
  bool has_empty_sources = 12;
  bool has_linear_availability_windows = 13;
  bool has_linear_availability_window_for_date = 14;
  bool has_ad_pods = 15;
  bool fulfilled = 16;
  string author = 17;
  bool has_live_broadcast = 18;
}

message Window {
  string start_date = 1;
  string end_date = 2;
  string id = 3;
  int32 current_screenings = 4;
  int32 screenings = 5;
}

message AvailabilityWindows {
  repeated Window _a_v_o_d = 1 [json_name = "AVOD"];
  repeated Window linear = 2;
}

message Clip {
  string provider = 1;
  repeated Source sources = 2;
  string content_status = 3;
  Author author = 4;
}

message Source {
  string id = 1;
  string file = 2;
  string type = 3;
  string encryption = 4;
}

message DeleteTimelinesInChannelByDatesRequest {
  string start = 1;
  string end = 2;
  string channel_id = 3;
}

message DeleteTimelinesInChannelByDatesResponse {
  int64 deleted_count = 1;
}

message GetTimelinesByChannelIdRequest {
  string channel_id = 1;
  string start = 2;
  string stop = 3;
}

message GetTimelinesByChannelIdResponse {
  repeated Timeline documents = 1;
}

message GetTimelineIdsByClipIdRequest {
  string clip_id = 1;
  string from = 2;
}

message GetTimelineIdsByClipIdResponse {
  repeated string timeline_ids = 1;
}

message EpisodeLite {
  string id = 1;
  bool published = 2;
  bool featured = 3;
}

message TimelineLite {
  string id = 1;
  string start = 2;
  string stop = 3;
  string manual_override_marker = 4;
  EpisodeLite episode = 5;
}

message GetTimelinesLiteByChannelIdRequest {
  string channel_id = 1;
  int64 start = 2;
  int64 stop = 3;
}

message GetTimelinesLiteByChannelIdResponse {
  repeated TimelineLite documents = 1;
}

message GetDocumentsBetweenStartDatesRequest {
  string start_begin = 1;
  string start_end = 2;
  string channel_id = 3;
}

message AggregateByEpisodesRequest {
  repeated string episode_ids = 1;
  string channel_id = 2;
  string min_start = 3;
}

message AggregateByEpisodesResponse {
  repeated AggregateTimeline timelines = 1;
}

message AggregateTimeline {
  string id = 1;
  string start = 2;
  string timeline = 3;
  string channel = 4;
}

message Filter {
  // field to filter on
  string field = 1;
  // value for the filter
  string value = 2;
  // type of the filter string (default), number, bool
  string type = 3;
  optional string operator = 4;
}

message DateFilter {
  string date = 1;
  string operator = 2;
}

message GetAllByRequest {
  repeated Filter filters = 1;
  repeated DateFilter start = 2;
  repeated DateFilter stop = 3;
  string channel_id = 4;
  string distinct = 5;
}

message GetAllByResponse {
  repeated string documents = 1;
}
