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

syntax = "proto3";

package clippackage;

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

service ClipManager {
  // GetAll (Server Stream)
  //
  rpc GetAll(GetAllRequest) returns (stream Clip) {}

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

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

  // GetBackfillClips (Unary)
  //
  rpc GetBackfillClips(GetBackfillClipsRequest) returns (GetBackfillClipsResponse) {}

  // GetErrorClips (Unary)
  //
  rpc GetErrorClips(GetErrorClipsRequest) returns (GetErrorClipsResponse) {}

  // GetDocumentsUpdatedBetweenDates (Server Stream)
  //
  rpc GetDocumentsUpdatedBetweenDates(GetDocumentsUpdatedBetweenDatesRequest) returns (stream Clip) {}

  // UpdateContentMamUniqueIdsByIds (Unary)
  //
  rpc UpdateContentMamUniqueIdsByIds(UpdateContentMamUniqueIdsByIdsRequest) returns (UpdateContentMamUniqueIdsByIdsResponse) {}

  // UpdateFields (Unary)
  //
  rpc UpdateFields(UpdateFieldsRequest) returns (UpdateFieldsResponse) {}

  // Create (Unary)
  //
  rpc Create(CreateRequest) returns (CreateResponse) {}

  // DeleteByIds (Unary)
  //
  rpc DeleteByIds(DeleteByIdsRequest) returns (DeleteByIdsResponse) {}

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

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

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

  // GetComScoreById (Unary)
  //
  rpc GetComScoreById(Id) returns (GetComScoreByIdResponse) {}

  // GetAllPaged (Unary)
  //
  rpc GetAllPaged(GetAllPagedRequest) returns (GetAllPagedResponse) {}

  // GetByCodeAndActiveRegion (Unary)
  //
  rpc GetByCodeAndActiveRegion(GetByCodeAndActiveRegionRequest) returns (GetByCodeAndActiveRegionResponse) {}

  // GetByTag (Unary)
  //
  rpc GetByTag(GetByTagRequest) returns (GetByTagResponse) {}

  // GetPromotionalClips (Unary)
  //
  rpc GetPromotionalClips(GetPromotionalClipsRequest) returns (GetPromotionalClipsResponse) {}

  // updateByIdReturnUpdated (Unary)
  //
  // Deprecated. To be replaced by UpdateFields which has the same signature.
  rpc updateByIdReturnUpdated(UpdateByIdReturnUpdatedRequest) returns (UpdateByIdReturnUpdatedResponse) {}

  // GetClipsByIds (Unary)
  //
  // Deprecated. Will be removed once client (FW IVI) no longer using.
  rpc GetClipsByIds(GetClipsByIdsRequest) returns (GetClipsByIdsResponse) {}
}

message CFeedsSyncAllIdsRequest {
  bool published = 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;
}

message GetBackfillClipsRequest {}

message GetBackfillClipsResponse {
  repeated IdUpdated backfill_clips = 1;
}

message GetAllRequest {
  string region = 1;
}

message GetErrorClipsRequest {}

message GetErrorClipsResponse {
  repeated IdUpdated error_clips = 1;
}

message IdUpdated {
  string id = 1;
  string updated_at = 2;
}

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

message GetByIdRequest {
  string id = 1;
}

message GetByIdResponse {
  Clip document = 1;
}

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

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

message GetByIdsResponse {
  repeated Clip documents = 1;
}

message UpdateByIdReturnUpdatedRequest {
  string id = 1;
  Clip document = 2;
  repeated string update_fields = 3;
}

message UpdateByIdReturnUpdatedResponse {
  Clip document = 1;
}

message UpdateFieldsRequest {
  string id = 1;
  Clip document = 2;
  repeated string update_fields = 3;
}

message UpdateFieldsResponse {
  Clip document = 1;
}

message Clip {
  reserved 54 to 56; // framerate attrs originally had int32 and need to be double. No services are using 54-56 yet, can reserve.

  string id = 1;
  string name = 2;
  string partner = 3;
  string description = 4;
  Author author = 5;
  Advertising advertising = 6;
  Image featured_image = 7;
  string thumbnail = 8;
  repeated Thumbnail thumbnails = 9;
  string pluto_avails_i_d = 10;
  bool published = 11;
  bool live_broadcast = 12;
  bool promotional = 13;
  string provider = 14;
  string code = 15;
  string partner_code = 16;
  string internal_code = 17;
  string md5 = 18;
  string url = 19;
  repeated Fragment fragments = 20;
  double in_point = 21;
  double out_point = 22;
  string type = 23;
  repeated string tags = 24;
  int32 score = 25;
  int32 votes = 26;
  string uploaded = 27;
  string updated = 28;
  Meta meta = 29;
  bool flagged = 30;
  bool captions_required = 31;
  string captioned = 32;
  repeated Cc cc = 33;
  string copyright = 34;
  string language = 35;
  string rating_reason = 36;
  string original_release_date = 37;
  string genre = 38;
  string sub_genre = 39;
  string summary = 40;
  repeated Cc subtitles = 41;
  repeated string directors = 42;
  repeated string actors = 43;
  repeated string writers = 44;
  repeated string producers = 45;
  repeated int32 breakpoints = 46 [packed = true];
  repeated string breakpoints_s_m_p_t_e = 47;
  repeated string breakpoints_ingestion = 48;
  DistributeAs distribute_as = 49;
  string programming_type = 50;
  string color = 51;
  string category = 52;
  string ad_sparx_stream_id = 53;
  string tmsid = 57;
  string created_at = 58;
  string updated_at = 59;
  repeated Source sources = 60;
  string rating = 61;
  double duration = 62;
  Origin origin = 63;
  repeated Transcode transcodes = 64;
  string active_region = 65;

  string content_status = 66; // not found in inf-lib-schemas
  repeated string region_filter = 67; // not found in inf-lib-schemas
  AvailabilityWindows availability_windows = 68; // not found in inf-lib-schemas
  string encoding_profile = 69; // not found in inf-lib-schemas
  string content_u_u_i_d = 70; // not found in inf-lib-schemas

  // work around for migrating attributes originally created with the incorrect datatype
  // ARCH-2221: validate framerate, ffprobeFramerate, feedFramerate is Double
  double framerate = 71;
  // work around for migrating attributes originally created with the incorrect datatype
  // ARCH-2221: validate framerate, ffprobeFramerate, feedFramerate is Double
  double ffprobe_framerate = 72;
  // work around for migrating attributes originally created with the incorrect datatype
  // ARCH-2221: validate framerate, ffprobeFramerate, feedFramerate is Double
  double feed_framerate = 73;

  repeated Markup markups = 74;
  repeated TimedText timed_text = 75;
  repeated DiscreteAudio discrete_audio = 76;
  repeated string production_company = 77;
  repeated string production_countries = 78;
  int32 production_year = 79;
  Highlight highlight = 80;

  map<string, RegionOverrides> region_overrides = 81;
  map<string, LanguageOverrides> language_overrides = 82;
  TranscodeObject transcode = 83; // Deprecated - kept here for compatibility with the publishing server. See transcodes below. Transcode status of video, updated during a transcode operation. This object MUST only be used by the publishing server and the methods in models/encoding/index. clip.contentStatus is set to 'processing' while job is not done. When done, transcode.state and contentStatus will be set to ready and sources m3u8 link will be set. The additional attributes in this object are required by the publishing server.
  IngestionStatus ingestion_status = 84; // This field will have all information regards the ingestion status for the "document". It will be mainly used when an ingestion fails and we will need to re-process the clip. It was built thinking only in Clip ingestion, but it can be used for any Model that uses Encoding schema.
}

message TranscodeObject {
  string state = 1; // e.g., 'origin', 'downloading', 'downloaded', 'uploading', 'uploaded', 'submitted', 'initializing', 'waiting', 'running', 'ready', 'aborted', 'error', 'processing', 'unknown'
  string updated_at = 2; // used to tell if jobs are overdue, or at what time processing was completed
  string job_id = 3; // A zencoder job ID (for resolving issues)
  string error = 4; // Used if state is error (human readable error.message), so we know whether to consider restarting a job
  string code = 5;
  TranscodeSource source = 6;
  TranscodeProfile profile = 7;
}

message TranscodeSource {
  string url = 1;
  string type = 2; // e.g., 'MP4', 'WebM', 'FLV', 'HLS', 'DASH', 'RTMP', 'MOV'
}

message TranscodeProfile {
  string name = 1;
  ProfileParams params = 2;
}

message ProfileParams {
  int32 audio_channels = 1 [json_name = "audio_channels"];
  bool audio_normalize = 2 [json_name = "audio_normalize"];
  bool audio_pre_normalize = 3 [json_name = "audio_pre_normalize"];
  int32 audio_compression_ratio = 4 [json_name = "audio_compression_ratio"];
  sint32 audio_compression_threshold = 5 [json_name = "audio_compression_threshold"];
  sint32 audio_gain = 6 [json_name = "audio_gain"];
}

message IngestionStatus {
  string previous_content_status = 1;
  string started_at = 2; // date when the process started
  bool processing = 3; // whether the document is current being processed
  string action = 4; // which action this process will perform. (e.g., 'download_and_transcode', 'transcode')
  optional string finished_at = 5; // date when this process finished
  TranscodeObject previous_transcode = 6;
  optional bool can_process = 7; // whether the document can or cannot be processed again
  optional Misc misc = 8; // misc information like content-type and status code
  optional string failed_reason = 9; // e.g., 'media_unavailable', 'media_corrupted', 'invalid_content_type', 'invalid_status_code',
}

message Misc {
  string type = 1;
  int32 status_code = 2;
}

message DiscreteAudio {
  // the type of audio track: audio description or dubbed
  string audio_type = 1;
  // the url path to the audio track in S3
  string uri = 2;
  // any valid language code
  string language = 3;
  // ONLY change this if a new audio file was
  // uploaded. DO NOT change after transcoding.
  string updated_at = 4;
  string id = 5;
}

message TimedText {
  string id = 1;
  string type = 2;
  string language = 3;
  string updated_at = 4;
  string uri = 5;
}

message Markup {
  string id = 1;
  string type = 2;
  int32 start = 3;
  int32 end = 4;
  string object_id = 5;
}

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

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

message Transcode {
  string finished_at = 1;
  string external_i_d = 2;
  string transcoder_name = 3;
  string transcode_mode = 4;
  string id = 5;
  //repeated Output outputs = 6; deprecated
  reserved 6;
  string state = 7; // not found in inf-lib-schemas
  repeated Output output = 8;
  string lib_version = 9;
  string transcoder_version = 10;
  EncryptionParams encryption_params = 11;
}

message EncryptionParams {
  string schema = 1;
  int32 rotation_interval = 2;
  string key_file_name = 3;
}

message Output {
  string url = 1;
  string type = 2;
  string id = 3;
  Dimensions dimensions = 4;
}

message Dimensions {
  int32 width = 1;
  int32 height = 2;
}

message Origin {
  string url = 1;
  string src_url = 2;
  string type = 3; /// options: 'MP4', 'WebM', 'FLV', 'HLS', 'DASH', 'RTMP', 'MOV', 'video/mp4', 'video/webm', 'video/x-flv', 'application/x-mpegURL', 'application/octet-stream', 'video/mpeg', 'video/quicktime', 'video/x-msvideo', 'video/x-ms-wmv', 'binary/octet-stream',
  int32 in_point = 4; /// Used to crop video, in seconds
  int32 out_point = 5; /// Used to crop video, in seconds
  int32 bitrate = 6; /// Bitrate in kbps
  int32 duration = 7; /// Seconds
  int32 height = 8;
  int32 width = 9;
  optional int32 size = 10; /// Optional. Size in bytes,
  optional string md5 = 11; /// Optional, 32-character MD5 digest of video
  bool generatingFile = 12; /// Used to flag when the origin file is being generated by the worker-hls-to-mp4
}

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

message Author {
  string id = 1;
  string name = 2;
  string url = 3;
}

message Advertising {
  string client = 1;
  repeated Schedule schedule = 2;
}

message Schedule {
  string offset = 1;
  string tag = 2;
  string type = 3;
}

message Image {
  string path = 1;
  string title = 2;
}

message Thumbnail {
  string base_url = 1;
  string prefix = 2;
  int32 width = 3;
  int32 height = 4;
  string format = 5;
  int32 interval_in_frames = 6;
  int32 interval = 7;
}

message Fragment {
  repeated int32 breakpoints = 1;
  repeated string sub_clips = 2;
}

message Meta {
  int32 views = 1;
  string category = 2;
  string aspect = 3;
  int32 votes = 4;
}

message Cc {
  string uri = 1;
  string language = 2;
  bool added_to_video = 3;
  string updated_at = 4;
  string id = 5;
}

message DistributeAs {
  bool _t_v_o_d = 1 [json_name = "TVOD"];
  bool _s_v_o_d = 2 [json_name = "SVOD"];
  bool _a_v_o_d = 3 [json_name = "AVOD"];
  bool linear = 4;
}

message UpdateContentMamUniqueIdsByIdsRequest {
  repeated ObjectIdContentIdSet object_id_content_id_sets = 1;
}

message ObjectIdContentIdSet {
  string id = 1;
  string content_u_u_i_d = 2;
}

message UpdateContentMamUniqueIdsByIdsResponse {}

message CreateRequest {
  Clip document = 1;
}

message CreateResponse {
  Clip document = 1;
}

message DeleteByIdsRequest {
  repeated string ids = 1;
}

message DeleteByIdsResponse {
  int64 deleted_count = 1;
}

message DeleteByIdRequest {
  string id = 1;
}

message DeleteByIdResponse {
  int64 deleted_count = 1;
}

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

message GetClipsByIdsResponse {
  repeated Clip clips = 1;
}

message GetComScoreByIdResponse {
  ComScore document = 1;
}
message ComScore {
  StreamingTag streaming_tag = 1;
  Metadata metadata = 2;
}

message StreamingTag {
  string customer_c2 = 1;
}

message Metadata {
  string ns_st_ci = 1;
  string ns_st_pr = 2;
  int32 ns_st_cl = 3;
  string ns_st_ddt = 4;
  string ns_st_tdt = 5;
  string ns_st_ge = 6;
  string c3 = 7;
  string c4 = 8;
  string c6 = 9;
  string ns_st_ti = 10;
  string ns_st_ia = 11;
  string ns_st_ce = 12;
}

message GetAllPagedRequest {
  // how many results to return default 1000
  int32 limit = 1;
  // where to begin page default 0
  int32 offset = 2;
  // how to sort results
  repeated GetAllPagedSort sort = 3;
  repeated Filter filters = 4;
  // when set to true the `total count` metadata will not be calculated
  // this can save some performance as count is additional query
  bool disable_total_count = 5;
  repeated string projection = 6;
}

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;
  // operator to filter by (values must be AND or OR)
  string operator = 4;
}

message GetAllPagedResponse {
  repeated Clip documents = 1;
  GetAllPagedMetadata metadata = 2;
}

message GetAllPagedMetadata {
  int32 current_offset = 1;
  int32 total_items = 2;
}

message HighlightMedia {
  string type = 1;
  int32 max_width = 2;
  int32 max_height = 3;
  string url = 4;
  string id = 5;
}

message HighlightOutput {
  int32 start_offset_ms = 1;
  int32 end_offset_ms = 2;
  repeated HighlightMedia medias = 3;
  string id = 4;
  string markup_id = 5;
}

message Highlight {
  repeated HighlightOutput outputs = 1;
}

message Tile {
  string path = 1;
}

message RegionOverrides {
  string breakpoints = 1;
  AvailabilityWindows availability_windows = 2;
  DistributeAs distribute_as = 3;
}

message LanguageOverrides {
  string name = 1;
  string summary = 2;
  string description = 3;
  Tile tile = 4;
  string breakpoints = 5;
  repeated string tags = 6;
}

message GetByTagRequest {
  string tag = 1;
  int32 limit = 2;
}

message GetByTagResponse {
  repeated Clip documents = 1;
}

message GetByCodeAndActiveRegionRequest {
  string code = 1;
  string active_region = 2;
}
message GetByCodeAndActiveRegionResponse {
  Clip document = 1;
}

message GetPromotionalClipsRequest {}
message GetPromotionalClipsResponse {
  repeated Clip documents = 1;
}
