// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

package google.cloud.integrations.v1alpha;

import "google/cloud/integrations/v1alpha/event_parameter.proto";
import "google/cloud/integrations/v1alpha/product.proto";
import "google/cloud/integrations/v1alpha/task_config.proto";
import "google/protobuf/timestamp.proto";

option go_package = "cloud.google.com/go/integrations/apiv1alpha/integrationspb;integrationspb";
option java_multiple_files = true;
option java_outer_classname = "LogEntriesProto";
option java_package = "com.google.cloud.integrations.v1alpha";

// Log entry to log execution info for the monitored resource
// `integrations.googleapis.com/IntegrationVersion`.
message ExecutionInfo {
  // PostMethod Enum
  enum PostMethod {
    // Default value.
    POST_METHOD_UNSPECIFIED = 0;

    // Sync post.
    POST = 1;

    // Async post with schedule time.
    SCHEDULE = 2;
  }

  // Auto-generated primary key.
  string event_execution_info_id = 1;

  // Name of the integration.
  string integration = 2;

  // Pointer to the active version it is executing.
  string integration_version = 3;

  // The event data user sends as request.
  string project_id = 4;

  // The trigger id of the integration trigger config. If both trigger_id
  // and client_id is present, the integration is executed from the start tasks
  // provided by the matching trigger config otherwise it is executed from the
  // default start tasks.
  string trigger_id = 5;

  // Event parameters come in as part of the request.
  map<string, EventParameter> request_params = 6;

  // Event parameters come out as part of the response.
  map<string, EventParameter> response_params = 7;

  // The ways user posts this event.
  PostMethod post_method = 8;

  // The execution info about this event.
  EventExecutionDetails event_execution_details = 9;

  // Errors, warnings, and informationals associated with the workflow/task.
  // The order in which the errors were added by the workflow/task is
  // maintained.
  repeated ErrorDetail errors = 10;

  // Which Google product the execution_info belongs to. If not set, the
  // execution_info belongs to Integration Platform by default.
  Product product = 11;

  // This is used to de-dup incoming request.
  string request_id = 12;

  // The configuration details for a task.
  repeated TaskConfig task_configs = 13;
}

// Contains the details of the execution info of this event: this includes
// the tasks execution details plus the event execution statistics.
message EventExecutionDetails {
  // Enum EventExecutionState.
  enum EventExecutionState {
    // Default value.
    EVENT_EXECUTION_STATE_UNSPECIFIED = 0;

    // Event is received and waiting for the execution. This happens when
    // firing the event via "postToQueue" or "schedule".
    ON_HOLD = 1;

    // Event is under processing.
    IN_PROCESS = 2;

    // Event execution successfully finished. There's no more change after
    // this state.
    SUCCEEDED = 3;

    // Event execution failed. There's no more change after this state.
    FAILED = 4;

    // Event execution canceled by user. There's no more change after this
    // state.
    CANCELLED = 5;

    // Event execution failed and waiting for retry.
    RETRY_ON_HOLD = 6;

    // Event execution suspended and waiting for manual intervention.
    SUSPENDED = 7;
  }

  // The execution state of this event.
  EventExecutionState event_execution_state = 1;

  // After snapshot migration, this field will no longer be populated, but old
  // execution snapshots will still be accessible.
  repeated EventExecutionSnapshot event_execution_snapshot = 2;

  // Status for the current event execution attempt.
  repeated AttemptStats event_attempt_stats = 3;

  // Next scheduled execution time in case the execution status was
  // RETRY_ON_HOLD.
  google.protobuf.Timestamp next_execution_time = 4;

  // Indicates the number of times the execution has restarted from the
  // beginning.
  int32 event_retries_count = 5;
}

// Contains the snapshot of the event execution for a given checkpoint.
message EventExecutionSnapshot {
  // Metadata for the event/task retry.
  message EventExecutionSnapshotMetadata {
    // The task number associated with this snapshot. Could be empty.
    string task_number = 1;

    // the task name associated with this snapshot. Could be empty.
    string task = 2;

    // the event attempt number this snapshot belongs to.
    int32 event_attempt_num = 3;

    // the task attempt number this snapshot belongs to. Could be empty.
    int32 task_attempt_num = 4;
  }

  // Indicates "right after which checkpoint task's execution" this snapshot
  // is taken.
  string checkpoint_task_number = 1;

  // Indicates when this snapshot is taken.
  google.protobuf.Timestamp snapshot_time = 2;

  // Snapshot metadata.
  EventExecutionSnapshotMetadata event_execution_snapshot_metadata = 3;

  // All of the task execution details at the given point of time.
  repeated TaskExecutionDetails task_execution_details = 4;

  // All of the computed conditions that been calculated.
  repeated ConditionResult condition_results = 5;

  // The parameters in Event object.
  map<string, EventParameter> event_params = 6;

  // The parameters in Event object that differs from last snapshot.
  map<string, EventParameter> diff_params = 7;
}

// Contains the details of the execution of this task.
message TaskExecutionDetails {
  // Enum TaskExecutionState.
  enum TaskExecutionState {
    // Default value.
    TASK_EXECUTION_STATE_UNSPECIFIED = 0;

    // Task is waiting for its precondition tasks to finish to start the
    // execution.
    PENDING_EXECUTION = 1;

    // Task is under processing.
    IN_PROCESS = 2;

    // Task execution successfully finished. There's no more change after
    // this state.
    SUCCEED = 3;

    // Task execution failed. There's no more change after this state.
    FAILED = 4;

    // Task execution failed and cause the whole event execution to fail
    // immediately. There's no more change after this state.
    FATAL = 5;

    // Task execution failed and waiting for retry.
    RETRY_ON_HOLD = 6;

    // Task execution skipped. This happens when its precondition wasn't met,
    // or the event execution been canceled before reach to the task.
    // There's no more changes after this state.
    SKIPPED = 7;

    // Task execution canceled when in progress. This happens when event
    // execution been canceled or any other task fall in fatal state.
    CANCELLED = 8;

    // Task is waiting for its dependency tasks' rollback to finish to start
    // its rollback.
    PENDING_ROLLBACK = 9;

    // Task is rolling back.
    ROLLBACK_IN_PROCESS = 10;

    // Task is rolled back. This is the state we will set regardless of
    // rollback succeeding or failing.
    ROLLEDBACK = 11;

    // Task is a SuspensionTask which has executed once, creating a pending
    // suspension.
    SUSPENDED = 12;
  }

  // Pointer to the task config it used for execution.
  string task_number = 1;

  // The execution state of this task.
  TaskExecutionState task_execution_state = 2;

  // Status for the current task execution attempt.
  repeated AttemptStats task_attempt_stats = 3;
}

// Status for the execution attempt.
message AttemptStats {
  // The start time of the event execution for current attempt. This could be
  // in the future if it's been scheduled.
  google.protobuf.Timestamp start_time = 1;

  // The end time of the event execution for current attempt.
  google.protobuf.Timestamp end_time = 2;
}

// An error, warning, or information message associated with an integration.
message ErrorDetail {
  // The full text of the error message, including any parameters that were
  // thrown along with the exception.
  string error_message = 1;

  // The task try-number, in which, the error occurred.  If zero, the error
  // happened at the event level.
  int32 task_number = 2;
}

// Contains the combined condition calculation results.
message ConditionResult {
  // the current task number.
  string current_task_number = 1;

  // the next task number.
  string next_task_number = 2;

  // the result comes out after evaluate the combined condition. True if there's
  // no combined condition specified.
  bool result = 3;
}
