syntax = "proto3";

package nexuscrm.runtime.v1;

import "common/v1/common.proto";

option go_package = "github.com/pingcap-inc/nexuscrm-contract/wire-go/gen/runtime/v1;runtimev1";

service ExternalEffectDispatchService {
  rpc ClaimExternalEffects(ClaimExternalEffectsRequest) returns (ClaimExternalEffectsResponse);
  rpc CompleteExternalEffect(CompleteExternalEffectRequest) returns (CompleteExternalEffectResponse);
}

message ClaimExternalEffectsRequest {
  string worker_id = 1;
  uint32 limit = 2;
  optional uint64 min_catalog_revision = 3;
}

message ExternalEffectHeader {
  string name = 1;
  repeated string values = 2;
}

message ExternalEffectQueryParameter {
  string name = 1;
  string value = 2;
}

message ExternalEffectRetryPolicy {
  uint32 max_attempts = 1;
  uint64 base_delay_millis = 2;
  uint32 multiplier_milli = 3;
  uint64 max_delay_millis = 4;
  repeated uint32 retryable_status_codes = 5;
}

message ExternalEffectOperation {
  string operation_id = 1;
  string request_digest = 2;
  string method = 3;
  string relative_path = 4;
  repeated ExternalEffectQueryParameter query = 5;
  repeated ExternalEffectHeader headers = 6;
  bytes body = 7;
  string credential_ref = 8;
  string endpoint_class = 9;
  ExternalEffectRetryPolicy retry_policy = 10;
}

message ExternalEffectLease {
  string effect_id = 1;
  string claim_token = 2;
  int64 lease_expires_at_unix_millis = 3;
  repeated ExternalEffectOperation operations = 4;
}

message ClaimExternalEffectsResponse {
  repeated ExternalEffectLease effects = 1;
  nexuscrm.common.v1.ConsistencyReceipts receipts = 2;
}

enum ExternalEffectOperationOutcome {
  EXTERNAL_EFFECT_OPERATION_OUTCOME_UNSPECIFIED = 0;
  EXTERNAL_EFFECT_OPERATION_OUTCOME_RESPONSE_RECEIVED = 1;
  EXTERNAL_EFFECT_OPERATION_OUTCOME_NO_RESPONSE = 2;
  EXTERNAL_EFFECT_OPERATION_OUTCOME_NOT_DISPATCHED = 3;
  EXTERNAL_EFFECT_OPERATION_OUTCOME_SKIPPED = 4;
}

message ExternalEffectOperationResult {
  string operation_id = 1;
  string request_digest = 2;
  ExternalEffectOperationOutcome outcome = 3;
  uint32 status_code = 4;
  reserved 5;
  bytes body = 6;
  string failure_code = 7;
}

message CompleteExternalEffectRequest {
  string effect_id = 1;
  string claim_token = 2;
  repeated ExternalEffectOperationResult results = 3;
  uint64 catalog_revision = 4;
}

enum ExternalEffectCompletionState {
  EXTERNAL_EFFECT_COMPLETION_STATE_UNSPECIFIED = 0;
  EXTERNAL_EFFECT_COMPLETION_STATE_FINALIZED = 1;
  EXTERNAL_EFFECT_COMPLETION_STATE_RETRYING = 2;
  EXTERNAL_EFFECT_COMPLETION_STATE_AMBIGUOUS = 3;
  EXTERNAL_EFFECT_COMPLETION_STATE_FAILED = 4;
}

message CompleteExternalEffectResponse {
  string effect_id = 1;
  ExternalEffectCompletionState state = 2;
  nexuscrm.common.v1.ConsistencyReceipts receipts = 3;
}
