// Copyright (c) 2020 Temporal Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

syntax = "proto3";

package temporal.server.api.replication.v1;

option go_package = "go.temporal.io/server/api/replication/v1;repication";

import "google/protobuf/timestamp.proto";

import "dependencies/gogoproto/gogo.proto";

import "temporal/server/api/enums/v1/replication.proto";
import "temporal/server/api/enums/v1/task.proto";
import "temporal/server/api/history/v1/message.proto";

import "temporal/api/common/v1/message.proto";
import "temporal/api/namespace/v1/message.proto";
import "temporal/api/replication/v1/message.proto";
import "temporal/api/history/v1/message.proto";
import "temporal/api/failure/v1/message.proto";

message ReplicationTask {
    temporal.server.api.enums.v1.ReplicationTaskType task_type = 1;
    int64 source_task_id = 2;
    oneof attributes {
        NamespaceTaskAttributes namespace_task_attributes = 3;
        // TODO: deprecate once NDC migration is done.
        HistoryTaskAttributes history_task_attributes = 4;
        SyncShardStatusTaskAttributes sync_shard_status_task_attributes = 5;
        SyncActivityTaskAttributes sync_activity_task_attributes = 6;
        // TODO: deprecate once kafka deprecation is done.
        HistoryMetadataTaskAttributes history_metadata_task_attributes = 7;
        HistoryTaskV2Attributes history_task_v2_attributes = 8;
    }
}

message ReplicationToken {
    int32 shard_id = 1;
    // lastRetrievedMessageId is where the next fetch should begin with.
    int64 last_retrieved_message_id = 2;
    // lastProcessedMessageId is the last messageId that is processed on the passive side.
    // This can be different than lastRetrievedMessageId if passive side supports prefetching messages.
    int64 last_processed_message_id = 3;
}

message SyncShardStatus {
    google.protobuf.Timestamp status_time = 1 [(gogoproto.stdtime) = true];
}

message ReplicationMessages {
    repeated ReplicationTask replication_tasks = 1;
    // This can be different than the last taskId in the above list, because sender can decide to skip tasks (e.g. for completed workflows).
    int64 last_retrieved_message_id = 2;
    // Hint for flow control.
    bool has_more  = 3;
    SyncShardStatus sync_shard_status = 4;
}

message ReplicationTaskInfo {
    string namespace_id = 1;
    string workflow_id = 2;
    string run_id = 3;
    temporal.server.api.enums.v1.TaskType task_type = 4;
    int64 task_id = 5;
    int64 version = 6;
    int64 first_event_id = 7;
    int64 next_event_id = 8;
    int64 scheduled_id = 9;
}

message NamespaceTaskAttributes {
    temporal.server.api.enums.v1.NamespaceOperation namespace_operation = 1;
    string id = 2;
    temporal.api.namespace.v1.NamespaceInfo info = 3;
    temporal.api.namespace.v1.NamespaceConfig config = 4;
    temporal.api.replication.v1.NamespaceReplicationConfig replication_config = 5;
    int64 config_version = 6;
    int64 failover_version = 7;
}

message HistoryTaskAttributes {
    repeated string target_clusters = 1;
    string namespace_id = 2;
    string workflow_id = 3;
    string run_id = 4;
    int64 first_event_id = 5;
    int64 next_event_id = 6;
    int64 version = 7;
    reserved 8;
    temporal.api.history.v1.History history = 9;
    temporal.api.history.v1.History new_run_history = 10;
    reserved 11;
    reserved 12;
    reserved 13;
    reserved 14;
}

message HistoryMetadataTaskAttributes {
    repeated string target_clusters = 1;
    string namespace_id = 2;
    string workflow_id = 3;
    string run_id = 4;
    int64 first_event_id = 5;
    int64 next_event_id = 6;
    int64 version = 7;
}

message SyncShardStatusTaskAttributes {
    string source_cluster = 1;
    int32 shard_id = 2;
    google.protobuf.Timestamp status_time = 3 [(gogoproto.stdtime) = true];
}

message SyncActivityTaskAttributes {
    string namespace_id = 1;
    string workflow_id = 2;
    string run_id = 3;
    int64 version = 4;
    int64 scheduled_id = 5;
    google.protobuf.Timestamp scheduled_time = 6 [(gogoproto.stdtime) = true];
    int64 started_id = 7;
    google.protobuf.Timestamp started_time = 8 [(gogoproto.stdtime) = true];
    google.protobuf.Timestamp last_heartbeat_time = 9 [(gogoproto.stdtime) = true];
    temporal.api.common.v1.Payloads details = 10;
    int32 attempt = 11;
    temporal.api.failure.v1.Failure last_failure = 12;
    string last_worker_identity = 13;
    temporal.server.api.history.v1.VersionHistory version_history = 14;
}

message HistoryTaskV2Attributes {
    // TODO remove this task_id attribute once kafka deprecation is done
    int64 task_id = 1;
    string namespace_id = 2;
    string workflow_id = 3;
    string run_id = 4;
    repeated temporal.server.api.history.v1.VersionHistoryItem version_history_items = 5;
    temporal.api.common.v1.DataBlob events = 6;
    // New run events does not need version history since there is no prior events.
    temporal.api.common.v1.DataBlob new_run_events = 7;
}
