syntax = "proto3";

package global.serverless.v1;

import "global/common/v1/resources.proto";
import "google/api/field_behavior.proto";
import "regional/serverless/v1/resources.proto";

message Pager {
  int64 page = 1; // start from 1
  int64 page_size = 2;
}

enum Sort {
  SORT_UNSPECIFIED = 0;
  SORT_ASC = 1;
  SORT_DESC = 2;
}

message Sorter {
  string order_by = 1;
  Sort sort = 2;
}

enum UserClusterStatus {
  USER_CLUSTER_STATUS_UNSPECIFIED = 0;
  USER_CLUSTER_STATUS_NORMAL = 1;
  USER_CLUSTER_STATUS_CREATING = 2;
  USER_CLUSTER_STATUS_UNAVAILABLE = 3;
  USER_CLUSTER_STATUS_RECOVERING = 4;
  USER_CLUSTER_STATUS_IMPORTING = 5;
  USER_CLUSTER_STATUS_MAINTAINING = 6;
  USER_CLUSTER_STATUS_DELETED = 7;
}

message ClusterProxyError {
  // see https://mariadb.com/kb/en/mariadb-error-codes/ to get all errno.
  uint32 sql_err_no = 1; // the errno returned from tidb when execute this request
  string sql_err_msg = 2; // the errmsg returned from tidb when execute this request
}

message ServerlessServiceCluster {
  string cluster_id = 1;
  regional.serverless.v1.ServerlessRegionalServiceClusterInfo cluster = 2;
  uint64 tenant_id = 3;
  string cloud_provider = 4;
  string region = 5;

  bool has_set_pwd = 6;

  AutomatedBackupPolicy backup_policy = 7;
  SpendLimit spend_limit = 9;

  UserClusterStatus user_status = 10;
  Attributes attributes = 11;
  repeated common.v1.AvailableFeature features = 12;

  message Attributes {
    optional bool with_builtin_cdc_admin_role = 1;
  }

  uint64 project_id = 14;

  optional int64 creation_finished_at = 15;

  string internal_endpoint = 16;

  string root_password = 17 [
    (google.api.field_behavior) = OPTIONAL,
    (google.api.field_behavior) = INPUT_ONLY
  ];
  uint64 original_project_id = 18;
  int32 ru_version = 19;
  string engine_type = 20;
}

message EncryptionConfig {
  bool enhanced_encryption_enabled = 1;
}

message SpendLimit {
  uint32 monthly = 1;
  uint32 daily = 2;

  uint32 free_credit = 3;
}

message AutomatedBackupPolicy {
  string backup_time = 1;
  uint64 retention_days = 2;
}

message AWSPrivateEndpointService {
  string vpc_endpoint_id = 1;
  repeated string az_ids = 2;
}

message GCPPrivateEndpointService {}

message SqlUser {
  enum Type {
    TYPE_UNSPECIFIED = 0;
    TYPE_USER_DEFINED = 1; // User Defined User
    TYPE_DELEGATE = 2; // delegate user
  }
  enum BuiltinRole {
    BUILTIN_ROLE_UNSPECIFIED = 0;
    BUILTIN_ROLE_USER_ADMIN = 1; // db admin role, include the majority of privileges exclude some server management privileges
    //BUILTIN_ROLE_USER_MEMBER = 2; // unused
    //BUILTIN_ROLE_USER_OBSERVER = 3; // unused
    BUILTIN_ROLE_CDC_ADMIN = 4; // used for cdc, enable the cdc component could make changes in the secondary cluster.
  }
  enum AuthMethod {
    AUTH_METHOD_UNSPECIFIED = 0;
    AUTH_METHOD_PASSWORD = 1;
    AUTH_METHOD_TOKEN = 2; // only delegate user can use this method now
  }

  string username = 1;
  SqlUser.BuiltinRole builtin_role = 2; // optional
  repeated string custom_roles = 3; // optional
  string plugin = 4; // optional
  string account_locked = 5; // optional
}

message SqlUserOperator {
  string username = 1; // the operator user must be delegate user
  SqlUser.AuthMethod auth_method = 2;
  // the credential for auth:
  // * use tidb auth token if the auth_method is TOKEN
  // * use encrypted and base64-encoded password if the auth_method is PASSWORD, refer to:
  //   https://github.com/tidbcloud/pkgv2/blob/main/secret/encrypt/encryptor.go#L24
  string credential = 3;
}
