// Copyright (c) 2019 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.historyservice.v1;
option go_package = "go.temporal.io/server/api/historyservice/v1;historyservice";

import "google/protobuf/duration.proto";
import "google/protobuf/timestamp.proto";

import "dependencies/gogoproto/gogo.proto";

import "temporal/api/common/v1/message.proto";
import "temporal/api/history/v1/message.proto";
import "temporal/api/taskqueue/v1/message.proto";
import "temporal/api/enums/v1/workflow.proto";
import "temporal/api/workflow/v1/message.proto";
import "temporal/api/query/v1/message.proto";
import "temporal/api/failure/v1/message.proto";

import "temporal/server/api/history/v1/message.proto";
import "temporal/server/api/enums/v1/common.proto";
import "temporal/server/api/enums/v1/workflow.proto";
import "temporal/server/api/enums/v1/task.proto";
import "temporal/server/api/workflow/v1/message.proto";
import "temporal/server/api/namespace/v1/message.proto";
import "temporal/server/api/replication/v1/message.proto";
import "temporal/server/api/persistence/v1/workflow_mutable_state.proto";

// TODO: remove these dependencies
import "temporal/api/workflowservice/v1/request_response.proto";
import "temporal/server/api/adminservice/v1/request_response.proto";

message StartWorkflowExecutionRequest {
    string namespace_id = 1;
    temporal.api.workflowservice.v1.StartWorkflowExecutionRequest start_request = 2;
    temporal.server.api.workflow.v1.ParentExecutionInfo parent_execution_info = 3;
    int32 attempt = 4;
    google.protobuf.Timestamp workflow_execution_expiration_time = 5 [(gogoproto.stdtime) = true];
    temporal.api.enums.v1.ContinueAsNewInitiator continue_as_new_initiator = 6;
    temporal.api.failure.v1.Failure continued_failure = 7;
    temporal.api.common.v1.Payloads last_completion_result = 8;
    google.protobuf.Duration first_workflow_task_backoff = 9 [(gogoproto.stdduration) = true];
}

message StartWorkflowExecutionResponse {
    string run_id = 1;
}

message GetMutableStateRequest {
    string namespace_id = 1;
    temporal.api.common.v1.WorkflowExecution execution = 2;
    int64 expected_next_event_id = 3;
    bytes current_branch_token = 4;
}

message GetMutableStateResponse {
    temporal.api.common.v1.WorkflowExecution execution = 1;
    temporal.api.common.v1.WorkflowType workflow_type = 2;
    int64 next_event_id = 3;
    int64 previous_started_event_id = 4;
    int64 last_first_event_id = 5;
    temporal.api.taskqueue.v1.TaskQueue task_queue = 6;
    temporal.api.taskqueue.v1.TaskQueue sticky_task_queue = 7;
    reserved 8;
    reserved 9;
    reserved 10;
    // (-- api-linter: core::0140::prepositions=disabled
    //     aip.dev/not-precedent: "to" is used to indicate interval. --)
    google.protobuf.Duration sticky_task_queue_schedule_to_start_timeout = 11 [(gogoproto.stdduration) = true];
    reserved 12;
    bytes current_branch_token = 13;
    reserved 14;
    temporal.server.api.enums.v1.WorkflowExecutionState workflow_state = 15;
    temporal.api.enums.v1.WorkflowExecutionStatus workflow_status = 16;
    temporal.server.api.history.v1.VersionHistories version_histories = 17;
    bool is_sticky_task_queue_enabled = 18;
    int64 last_first_event_txn_id = 19;
}

message PollMutableStateRequest {
    string namespace_id = 1;
    temporal.api.common.v1.WorkflowExecution execution = 2;
    int64 expected_next_event_id = 3;
    bytes current_branch_token = 4;
}

message PollMutableStateResponse {
    temporal.api.common.v1.WorkflowExecution execution = 1;
    temporal.api.common.v1.WorkflowType workflow_type = 2;
    int64 next_event_id = 3;
    int64 previous_started_event_id = 4;
    int64 last_first_event_id = 5;
    temporal.api.taskqueue.v1.TaskQueue task_queue = 6;
    temporal.api.taskqueue.v1.TaskQueue sticky_task_queue = 7;
    reserved 8;
    reserved 9;
    reserved 10;
    // (-- api-linter: core::0140::prepositions=disabled
    //     aip.dev/not-precedent: "to" is used to indicate interval. --)
    google.protobuf.Duration sticky_task_queue_schedule_to_start_timeout = 11 [(gogoproto.stdduration) = true];
    bytes current_branch_token = 12;
    reserved 13;
    temporal.server.api.history.v1.VersionHistories version_histories = 14;
    temporal.server.api.enums.v1.WorkflowExecutionState workflow_state = 15;
    temporal.api.enums.v1.WorkflowExecutionStatus workflow_status = 16;
    int64 last_first_event_txn_id = 17;
}

message ResetStickyTaskQueueRequest {
    string namespace_id = 1;
    temporal.api.common.v1.WorkflowExecution execution = 2;
}

message ResetStickyTaskQueueResponse {
}

message RecordWorkflowTaskStartedRequest {
    string namespace_id = 1;
    temporal.api.common.v1.WorkflowExecution workflow_execution = 2;
    int64 schedule_id = 3;
    int64 task_id = 4;
    // Unique id of each poll request. Used to ensure at most once delivery of tasks.
    string request_id = 5;
    temporal.api.workflowservice.v1.PollWorkflowTaskQueueRequest poll_request = 6;
}

message RecordWorkflowTaskStartedResponse {
    temporal.api.common.v1.WorkflowType workflow_type = 1;
    int64 previous_started_event_id = 2;
    int64 scheduled_event_id = 3;
    int64 started_event_id = 4;
    int64 next_event_id = 5;
    int32 attempt = 6;
    bool sticky_execution_enabled = 7;
    temporal.server.api.history.v1.TransientWorkflowTaskInfo workflow_task_info = 8;
    temporal.api.taskqueue.v1.TaskQueue workflow_execution_task_queue = 9;
    reserved 10;
    bytes branch_token = 11;
    google.protobuf.Timestamp scheduled_time = 12 [(gogoproto.stdtime) = true];
    google.protobuf.Timestamp started_time = 13 [(gogoproto.stdtime) = true];
    map<string, temporal.api.query.v1.WorkflowQuery> queries = 14;
}

message RecordActivityTaskStartedRequest {
    string namespace_id = 1;
    temporal.api.common.v1.WorkflowExecution workflow_execution = 2;
    int64 schedule_id = 3;
    int64 task_id = 4;
    // Unique id of each poll request. Used to ensure at most once delivery of tasks.
    string request_id = 5;
    temporal.api.workflowservice.v1.PollActivityTaskQueueRequest poll_request = 6;
}

message RecordActivityTaskStartedResponse {
    temporal.api.history.v1.HistoryEvent scheduled_event = 1;
    google.protobuf.Timestamp started_time = 2 [(gogoproto.stdtime) = true];
    int32 attempt = 3;
    google.protobuf.Timestamp current_attempt_scheduled_time = 4 [(gogoproto.stdtime) = true];
    temporal.api.common.v1.Payloads heartbeat_details = 5;
    temporal.api.common.v1.WorkflowType workflow_type = 6;
    string workflow_namespace = 7;
}

message RespondWorkflowTaskCompletedRequest {
    string namespace_id = 1;
    temporal.api.workflowservice.v1.RespondWorkflowTaskCompletedRequest complete_request = 2;
}

message RespondWorkflowTaskCompletedResponse {
    RecordWorkflowTaskStartedResponse started_response = 1;
}

message RespondWorkflowTaskFailedRequest {
    string namespace_id = 1;
    temporal.api.workflowservice.v1.RespondWorkflowTaskFailedRequest failed_request = 2;
}

message RespondWorkflowTaskFailedResponse {
}

message RecordActivityTaskHeartbeatRequest {
    string namespace_id = 1;
    temporal.api.workflowservice.v1.RecordActivityTaskHeartbeatRequest heartbeat_request = 2;
}

message RecordActivityTaskHeartbeatResponse {
    bool cancel_requested = 1;
}

message RespondActivityTaskCompletedRequest {
    string namespace_id = 1;
    temporal.api.workflowservice.v1.RespondActivityTaskCompletedRequest complete_request = 2;
}

message RespondActivityTaskCompletedResponse {
}

message RespondActivityTaskFailedRequest {
    string namespace_id = 1;
    temporal.api.workflowservice.v1.RespondActivityTaskFailedRequest failed_request = 2;
}

message RespondActivityTaskFailedResponse {
}

message RespondActivityTaskCanceledRequest {
    string namespace_id = 1;
    temporal.api.workflowservice.v1.RespondActivityTaskCanceledRequest cancel_request = 2;
}

message RespondActivityTaskCanceledResponse {
}

message SignalWorkflowExecutionRequest {
    string namespace_id = 1;
    temporal.api.workflowservice.v1.SignalWorkflowExecutionRequest signal_request = 2;
    temporal.api.common.v1.WorkflowExecution external_workflow_execution = 3;
    bool child_workflow_only = 4;
}

message SignalWorkflowExecutionResponse {
}

message SignalWithStartWorkflowExecutionRequest {
    string namespace_id = 1;
    // (-- api-linter: core::0140::prepositions=disabled
    //     aip.dev/not-precedent: "with" is needed here. --)
    temporal.api.workflowservice.v1.SignalWithStartWorkflowExecutionRequest signal_with_start_request = 2;
}

message SignalWithStartWorkflowExecutionResponse {
    string run_id = 1;
}

message RemoveSignalMutableStateRequest {
    string namespace_id = 1;
    temporal.api.common.v1.WorkflowExecution workflow_execution = 2;
    string request_id = 3;
}

message RemoveSignalMutableStateResponse {
}

message TerminateWorkflowExecutionRequest {
    string namespace_id = 1;
    temporal.api.workflowservice.v1.TerminateWorkflowExecutionRequest terminate_request = 2;
}

message TerminateWorkflowExecutionResponse {
}

message ResetWorkflowExecutionRequest {
    string namespace_id = 1;
    temporal.api.workflowservice.v1.ResetWorkflowExecutionRequest reset_request = 2;
}

message ResetWorkflowExecutionResponse {
    string run_id = 1;
}

message RequestCancelWorkflowExecutionRequest {
    string namespace_id = 1;
    temporal.api.workflowservice.v1.RequestCancelWorkflowExecutionRequest cancel_request = 2;
    int64 external_initiated_event_id = 3;
    temporal.api.common.v1.WorkflowExecution external_workflow_execution = 4;
    bool child_workflow_only = 5;
}

message RequestCancelWorkflowExecutionResponse {
}

message ScheduleWorkflowTaskRequest {
    string namespace_id = 1;
    temporal.api.common.v1.WorkflowExecution workflow_execution = 2;
    bool is_first_workflow_task = 3;
}

message ScheduleWorkflowTaskResponse {
}

/**
* RecordChildExecutionCompletedRequest is used for reporting the completion of child execution to parent workflow
* execution which started it.  When a child execution is completed it creates this request and calls the
* RecordChildExecutionCompleted API with the workflowExecution of parent.  It also sets the completedExecution of the
* child as it could potentially be different than the ChildExecutionStartedEvent of parent in the situation when
* child creates multiple runs through ContinueAsNew before finally completing.
**/
message RecordChildExecutionCompletedRequest {
    string namespace_id = 1;
    temporal.api.common.v1.WorkflowExecution workflow_execution = 2;
    int64 initiated_id = 3;
    temporal.api.common.v1.WorkflowExecution completed_execution = 4;
    temporal.api.history.v1.HistoryEvent completion_event = 5;
}

message RecordChildExecutionCompletedResponse {
}

message DescribeWorkflowExecutionRequest {
    string namespace_id = 1;
    temporal.api.workflowservice.v1.DescribeWorkflowExecutionRequest request = 2;
}

message DescribeWorkflowExecutionResponse {
    temporal.api.workflow.v1.WorkflowExecutionConfig execution_config = 1;
    temporal.api.workflow.v1.WorkflowExecutionInfo workflow_execution_info = 2;
    repeated temporal.api.workflow.v1.PendingActivityInfo pending_activities = 3;
    repeated temporal.api.workflow.v1.PendingChildExecutionInfo pending_children = 4;
}

message ReplicateEventsV2Request {
    string namespace_id = 1;
    temporal.api.common.v1.WorkflowExecution workflow_execution = 2;
    repeated temporal.server.api.history.v1.VersionHistoryItem version_history_items = 3;
    temporal.api.common.v1.DataBlob events = 4;
    // New run events does not need version history since there is no prior events.
    temporal.api.common.v1.DataBlob new_run_events = 5;
}

message ReplicateEventsV2Response {
}

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

message SyncShardStatusResponse {
}

message SyncActivityRequest {
    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 SyncActivityResponse {
}

message DescribeMutableStateRequest {
    string namespace_id = 1;
    temporal.api.common.v1.WorkflowExecution execution = 2;
}

message DescribeMutableStateResponse {
    temporal.server.api.persistence.v1.WorkflowMutableState cache_mutable_state = 1;
    temporal.server.api.persistence.v1.WorkflowMutableState database_mutable_state = 2;
}

// At least one of the parameters needs to be provided.
message DescribeHistoryHostRequest {
    //ip:port
    string host_address = 1;
    int32 shard_id = 2;
    string namespace_id = 3;
    temporal.api.common.v1.WorkflowExecution workflow_execution = 4;
}

message DescribeHistoryHostResponse {
    int32 shards_number = 1;
    repeated int32
    shard_ids = 2;
    temporal.server.api.namespace.v1.NamespaceCacheInfo namespace_cache = 3;
    string shard_controller_status = 4;
    string address = 5;
}

message CloseShardRequest {
    int32 shard_id = 1;
}

message CloseShardResponse {
}

message RemoveTaskRequest {
    int32 shard_id = 1;
    temporal.server.api.enums.v1.TaskCategory category = 2;
    int64 task_id = 3;
    google.protobuf.Timestamp visibility_time = 4 [(gogoproto.stdtime) = true];
}

message RemoveTaskResponse {
}

message GetReplicationMessagesRequest {
    repeated temporal.server.api.replication.v1.ReplicationToken tokens = 1;
    string cluster_name = 2;
}

message GetReplicationMessagesResponse {
    map<int32, temporal.server.api.replication.v1.ReplicationMessages> shard_messages = 1;
}

message GetDLQReplicationMessagesRequest {
    repeated temporal.server.api.replication.v1.ReplicationTaskInfo task_infos = 1;
}

message GetDLQReplicationMessagesResponse {
    repeated temporal.server.api.replication.v1.ReplicationTask replication_tasks = 1;
}

message QueryWorkflowRequest {
    string namespace_id = 1;
    temporal.api.workflowservice.v1.QueryWorkflowRequest request = 2;
}

message QueryWorkflowResponse {
    temporal.api.workflowservice.v1.QueryWorkflowResponse response = 1;
}

message ReapplyEventsRequest {
    string namespace_id = 1;
    temporal.server.api.adminservice.v1.ReapplyEventsRequest request = 2;
}

message ReapplyEventsResponse {
}

message GetDLQMessagesRequest {
    temporal.server.api.enums.v1.DeadLetterQueueType type = 1;
    int32 shard_id = 2;
    string source_cluster = 3;
    int64 inclusive_end_message_id = 4;
    int32 maximum_page_size = 5;
    bytes next_page_token = 6;
}

message GetDLQMessagesResponse {
    temporal.server.api.enums.v1.DeadLetterQueueType type = 1;
    repeated temporal.server.api.replication.v1.ReplicationTask replication_tasks = 2;
    bytes next_page_token = 3;
}

message PurgeDLQMessagesRequest {
    temporal.server.api.enums.v1.DeadLetterQueueType type = 1;
    int32 shard_id = 2;
    string source_cluster = 3;
    int64 inclusive_end_message_id = 4;
}

message PurgeDLQMessagesResponse {
}

message MergeDLQMessagesRequest {
    temporal.server.api.enums.v1.DeadLetterQueueType type = 1;
    int32 shard_id = 2;
    string source_cluster = 3;
    int64 inclusive_end_message_id = 4;
    int32 maximum_page_size = 5;
    bytes next_page_token = 6;
}

message MergeDLQMessagesResponse {
    bytes next_page_token = 1;
}

message RefreshWorkflowTasksRequest {
    string namespace_id = 1;
    temporal.server.api.adminservice.v1.RefreshWorkflowTasksRequest request = 2;
}

message RefreshWorkflowTasksResponse {
}
