syntax = "proto3";

package tidb_cloud_open_api.nextgen.v1beta2;

import "google/api/annotations.proto";
import "google/api/field_behavior.proto";
import "google/protobuf/empty.proto";
import "google/protobuf/timestamp.proto";
// buf:lint:ignore IMPORT_USED
import "google/rpc/status.proto"; // This is used by `responses.value.schema.json_schema.ref` in the swagger definition.
import "protoc-gen-openapiv2/options/annotations.proto";
import "tidb_cloud_open_api/common/v1beta1/region.proto";
import "tidb_cloud_open_api/common/v1beta1/service_plan.proto";
import "tidb_cloud_open_api/nextgen/v1beta2/common.proto";
import "tidb_cloud_open_api/nextgen/v1beta2/tidb.proto";

option go_package = "tidb_cloud_open_api/nextgen/v1beta2";
option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_swagger) = {
  swagger: "2.0"
  host: "cloud.tidbapi.com"
  base_path: "/v1beta2"
  info: {
    version: "v1beta2"
    title: "TiDB Cloud Premium API"
    description: "TiDB Cloud Premium API"
  }
  schemes: HTTPS
  consumes: "application/json"
  produces: "application/json"

  responses: {
    key: "400"
    value: {
      description: "A request field is invalid."
      schema: {
        json_schema: {ref: ".google.rpc.Status"}
      }
    }
  }
  responses: {
    key: "401"
    value: {
      description: "The API key cannot be authenticated."
      schema: {
        json_schema: {ref: ".google.rpc.Status"}
      }
    }
  }
  responses: {
    key: "403"
    value: {
      description: "The API key does not have permission to access the resource."
      schema: {
        json_schema: {ref: ".google.rpc.Status"}
      }
    }
  }
  responses: {
    key: "429"
    value: {
      description: "You have exceeded the rate limit."
      schema: {
        json_schema: {ref: ".google.rpc.Status"}
      }
    }
  }
  responses: {
    key: "500"
    value: {
      description: "Internal server error."
      schema: {
        json_schema: {ref: ".google.rpc.Status"}
      }
    }
  }
};

// support br api in org level
service BackupService {
  // List the backup list for the org
  rpc ListBackups(ListBackupsRequest) returns (ListBackupsResponse) {
    option (google.api.http) = {get: "/backups"};
    option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation) = {
      summary: "List backups"
      tags: "public"
      tags: "Backup"
      description: "Lists all backups in your organization. You can filter results by TiDB Cloud Premium instance, state, service plan, region, or time range."
      extensions: {
        key: "x-code-samples"
        value: {
          list_value: {
            values: {
              struct_value: {
                fields: {
                  key: "lang"
                  value: {string_value: "curl"}
                }
                fields: {
                  key: "label"
                  value: {string_value: "curl"}
                }
                fields: {
                  key: "source"
                  value: {string_value: "curl --location 'https://cloud.tidbapi.com/v1beta2/backups' \\\n--digest --user 'YOUR_PUBLIC_KEY:YOUR_PRIVATE_KEY' \\\n--header 'Accept: application/json'"}
                }
              }
            }
          }
        }
      }
    };
  }

  // Delete a backup by ID
  rpc DeleteBackup(DeleteBackupRequest) returns (google.protobuf.Empty) {
    option (google.api.http) = {delete: "/backups/{backup_id}"};
    option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation) = {
      summary: "Delete a backup"
      tags: "public"
      tags: "Backup"
      description: "Deletes a backup by `backupId`."
      extensions: {
        key: "x-code-samples"
        value: {
          list_value: {
            values: {
              struct_value: {
                fields: {
                  key: "lang"
                  value: {string_value: "curl"}
                }
                fields: {
                  key: "label"
                  value: {string_value: "curl"}
                }
                fields: {
                  key: "source"
                  value: {string_value: "curl --location --request DELETE 'https://cloud.tidbapi.com/v1beta2/backups/{backup_id}' \\\n--digest --user 'YOUR_PUBLIC_KEY:YOUR_PRIVATE_KEY' \\\n--header 'Accept: application/json'"}
                }
              }
            }
          }
        }
      }
    };
  }

  rpc ListDeletedTidbs(ListDeletedTidbsRequest) returns (ListDeletedTidbsResponse) {
    option (google.api.http) = {get: "/backups:listDeletedTidbs"};
    option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation) = {
      summary: "List deleted TiDB Cloud Premium instances with backups"
      description: "Lists all deleted TiDB Cloud Premium instances that still have backups available."
    };
  }
}

message ListBackupsRequest {
  enum TidbState {
    TIDB_STATE_UNSPECIFIED = 0;
    ACTIVE = 1;
    DELETED = 2;
  }

  // A list of TiDB Cloud Premium instance IDs. If specified, only backups belonging to these TiDB Cloud Premium instances are returned.
  repeated string tidb_ids = 1 [(google.api.field_behavior) = OPTIONAL];

  // A list of backup states. If specified, only backups with these states are returned.
  // (-- api-linter: core::0216::state-field-output-only=disabled
  //     aip.dev/not-precedent: It in a input param --)
  repeated Backup.State states = 2 [(google.api.field_behavior) = OPTIONAL];

  // A list of service plans. If specified, only backups belonging to these service plans are returned.
  repeated common.v1beta1.ServicePlan service_plans = 3 [(google.api.field_behavior) = OPTIONAL];

  // A list of region IDs. If specified, only backups in these regions are returned.
  repeated string region_ids = 4 [(google.api.field_behavior) = OPTIONAL];

  // A list of TiDB Cloud Premium instance states. If specified, only backups from TiDB Cloud Premium instances in these states are returned.
  // (-- api-linter: core::0216::state-field-output-only=disabled
  //     aip.dev/not-precedent: It in a input param --)
  repeated TidbState tidb_states = 5 [(google.api.field_behavior) = OPTIONAL];

  // A list of trigger types. If specified, only backups with these trigger types are returned.
  repeated Backup.TriggerType trigger_types = 6 [(google.api.field_behavior) = OPTIONAL];

  // The start time of the time range filter. If specified, only backups created after this time are returned.
  google.protobuf.Timestamp start_time = 7 [(google.api.field_behavior) = OPTIONAL];

  // The end time of the time range filter. If specified, only backups created before this time are returned.
  google.protobuf.Timestamp end_time = 8 [(google.api.field_behavior) = OPTIONAL];

  // The maximum number of backups to return. If not specified, at most 10 backups will be returned.
  // The maximum value is `100`. Values greater than `100` are set to `100`.
  int32 page_size = 100 [
    (google.api.field_behavior) = OPTIONAL,
    (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_field) = {
      example: "10"
      default: "10"
      minimum: 1
      maximum: 100
    }
  ];

  // The pagination token received from a previous `ListBackups` request. Use this token to retrieve the next page of results.
  //
  // **Note**: When paginating, all other parameters must match the original request.
  string page_token = 101 [(google.api.field_behavior) = OPTIONAL];

  // The number of individual resources to skip before starting to return results.
  // If the skip value causes the cursor to move past the end of the collection,
  // the response will be 200 OK with an empty result set and no next_page_token.
  int32 skip = 102 [(.google.api.field_behavior) = OPTIONAL];
}

message ListBackupsResponse {
  // A list of backups that match the query.
  repeated Backup backups = 1;

  // The total number of backups that match the query.
  int32 total_size = 2;

  // A token, which can be sent as `pageToken` to retrieve the next page.
  // If this field is omitted, there are no subsequent pages.
  string next_page_token = 3;
}

message DeleteBackupRequest {
  // The ID of the backup to delete.
  string backup_id = 1 [(google.api.field_behavior) = REQUIRED];
}
message ListDeletedTidbsRequest {
  // A list of TiDB Cloud instance IDs. If specified, only deleted TiDB Cloud instances matching these IDs are returned.
  repeated string tidb_ids = 1 [(google.api.field_behavior) = OPTIONAL];
  // Indicates whether to include encryption information in the response.
  bool include_encryption_info = 2 [(google.api.field_behavior) = OPTIONAL];
  // A list of service plans. If specified, only deleted TiDB Cloud instances belonging to these service plans are returned.
  repeated common.v1beta1.ServicePlan service_plans = 3 [(google.api.field_behavior) = OPTIONAL];
  // A list of region ids. If specified, only deleted TiDB Cloud instances belonging to these region ids are returned.
  repeated string region_ids = 4 [(google.api.field_behavior) = OPTIONAL];
  // The start time of the deletion time range filter. If specified, only deleted TiDB Cloud instances deleted after this time are returned.
  google.protobuf.Timestamp start_time = 5 [(google.api.field_behavior) = OPTIONAL];
  // The end time of the deletion time range filter. If specified, only deleted TiDB Cloud instances deleted before this time are returned.
  google.protobuf.Timestamp end_time = 6 [(google.api.field_behavior) = OPTIONAL];
  // The display name to filter by. If specified, only deleted TiDB Cloud instances with a display name containing this value are returned.
  string name = 7 [(google.api.field_behavior) = OPTIONAL];
  // The maximum number of deleted TiDB Cloud instances to return. If not specified, at most 10 will be returned.
  // The maximum value is `100`. Values greater than `100` are set to `100`.
  int32 page_size = 100 [
    (google.api.field_behavior) = OPTIONAL,
    (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_field) = {
      example: "10"
      default: "10"
      minimum: 1
      maximum: 100
    }
  ];
  // The pagination token received from a previous `ListDeletedTidbs` request. Use this token to retrieve the next page of results.
  //
  // **Note**: When paginating, all other parameters must match the original request.
  string page_token = 101 [(google.api.field_behavior) = OPTIONAL];
  // The number of individual resources to skip before starting to return results.
  // If the skip value causes the cursor to move past the end of the collection,
  // the response will be 200 OK with an empty result set and no next_page_token.
  int32 skip = 102 [(.google.api.field_behavior) = OPTIONAL];
}

message ListDeletedTidbsResponse {
  // A list of deleted TiDB Cloud instances that still have backups.
  repeated DeletedTidb items = 1;
  // The total number of deleted TiDB Cloud instances that match the query.
  int32 total_size = 2;
  // A token, which can be sent as `pageToken` to retrieve the next page.
  // If this field is omitted, there are no subsequent pages.
  string next_page_token = 3;
}

message DeletedTidb {
  // The ID of the deleted TiDB Cloud Premium instance.
  string tidb_id = 1;
  // The user-defined name of the deleted TiDB Cloud Premium instance.
  string display_name = 2;
  // The cloud provider where the deleted TiDB Cloud Premium instance was deployed.
  common.v1beta1.Region.CloudProvider cloud_provider = 3;
  // The service plan of the deleted TiDB Cloud Premium instance.
  common.v1beta1.ServicePlan service_plan = 4;
  // The ID of the region where the deleted TiDB Cloud Premium instance was deployed, in the format of `{cloud_provider}-{region_code}`. For example, `aws-us-west-2`.
  string region_id = 5;
  // The display name of the region where the deleted TiDB Cloud Premium instance was deployed.
  string region_display_name = 6;
  // The start time of the point-in-time recovery (PITR) window.
  google.protobuf.Timestamp pitr_start_time = 7;
  // The end time of the point-in-time recovery (PITR) window.
  google.protobuf.Timestamp pitr_end_time = 8;
  // The timestamp when the TiDB Cloud Premium instance was deleted.
  google.protobuf.Timestamp delete_time = 9;
  // The dual-layer data encryption configuration of the deleted TiDB Cloud Premium instance.
  Tidb.DualLayerDataEncryption dual_layer_data_encryption = 10 [(google.api.field_behavior) = OPTIONAL];
}
