syntax = "proto3";

package global.nextgen.v1;

import "global/nextgen/v1/cloud_storage.proto";
import "google/api/field_behavior.proto";
import "google/protobuf/timestamp.proto";

service ExportService {
  rpc ListExports(ListExportsRequest) returns (ListExportsResponse);
  rpc GetExport(GetExportRequest) returns (Export);
  rpc CreateExport(CreateExportRequest) returns (Export);
  rpc DeleteExport(DeleteExportRequest) returns (Export);
  rpc CancelExport(CancelExportRequest) returns (Export);
}

message Export {
  // State enum for export operations
  enum State {
    STATE_UNSPECIFIED = 0;
    RUNNING = 1;
    SUCCEEDED = 2;
    FAILED = 3;
    CANCELED = 4;
    DELETED = 5;
  }

  string export_id = 1 [(google.api.field_behavior) = OPTIONAL];
  string display_name = 2 [(google.api.field_behavior) = OPTIONAL];
  string tidb_id = 3 [(google.api.field_behavior) = REQUIRED];
  string created_by = 4 [(google.api.field_behavior) = OUTPUT_ONLY];
  State state = 5 [(google.api.field_behavior) = OUTPUT_ONLY];
  ExportOptions export_options = 6 [(google.api.field_behavior) = REQUIRED];
  CloudStorage cloud_storage = 7 [(google.api.field_behavior) = REQUIRED];
  string reason = 8 [(google.api.field_behavior) = OUTPUT_ONLY];
  // Snapshot TSO to export. When omitted during creation, the regional service generates one from the current time.
  uint64 snapshot_tso = 9 [(google.api.field_behavior) = OPTIONAL];
  optional google.protobuf.Timestamp snapshot_time = 10 [(google.api.field_behavior) = OUTPUT_ONLY];
  string username = 11 [(google.api.field_behavior) = REQUIRED];
  string password = 12 [(google.api.field_behavior) = INPUT_ONLY];
  google.protobuf.Timestamp create_time = 100 [(google.api.field_behavior) = OUTPUT_ONLY];
  optional google.protobuf.Timestamp update_time = 101 [(google.api.field_behavior) = OUTPUT_ONLY];
  optional google.protobuf.Timestamp complete_time = 102 [(google.api.field_behavior) = OUTPUT_ONLY];
}

message ExportParquetCompressionType {
  enum Enum {
    PARQUET_COMPRESSION_TYPE_UNSPECIFIED = 0;
    GZIP = 1;
    SNAPPY = 2;
    ZSTD = 3;
    NONE = 4;
  }
}

message ExportOptions {
  enum FileType {
    FILE_TYPE_UNSPECIFIED = 0;
    SQL = 1;
    CSV = 2;
    PARQUET = 3;
  }

  enum CompressionType {
    COMPRESSION_TYPE_UNSPECIFIED = 0;
    GZIP = 1;
    SNAPPY = 2;
    ZSTD = 3;
    NONE = 4;
  }

  message CSVFormat {
    string separator = 1 [(google.api.field_behavior) = OPTIONAL];
    optional string delimiter = 2 [(google.api.field_behavior) = OPTIONAL];
    optional string null_value = 3 [(google.api.field_behavior) = OPTIONAL];
    bool skip_header = 4 [(google.api.field_behavior) = OPTIONAL];
    // The CSV output dialect. When omitted, Dumpling uses its default CSV encoding.
    optional ExportCSVDialect.Enum dialect = 5 [(google.api.field_behavior) = OPTIONAL];
  }

  message ExportCSVDialect {
    enum Enum {
      // The dialect is not specified.
      EXPORT_CSV_DIALECT_UNSPECIFIED = 0;
      // The standard CSV dialect.
      DEFAULT = 1;
      // The Snowflake CSV dialect.
      SNOWFLAKE = 2;
      // The BigQuery CSV dialect.
      BIGQUERY = 3;
      // The Redshift CSV dialect.
      REDSHIFT = 4;
    }
  }

  message Filter {
    message Table {
      repeated string patterns = 1 [(google.api.field_behavior) = REQUIRED];
      string where = 2 [(google.api.field_behavior) = OPTIONAL];
      repeated string partitions = 3 [(google.api.field_behavior) = OPTIONAL];
    }
    oneof type {
      string sql = 1 [(google.api.field_behavior) = OPTIONAL];
      ExportOptions.Filter.Table table = 2 [(google.api.field_behavior) = OPTIONAL];
    }
  }

  message ParquetFormat {
    ExportParquetCompressionType.Enum compression = 1 [(google.api.field_behavior) = OPTIONAL];
  }

  FileType file_type = 1 [(google.api.field_behavior) = REQUIRED];
  CompressionType compression = 2 [(google.api.field_behavior) = REQUIRED];
  ExportOptions.Filter filter = 3 [(google.api.field_behavior) = REQUIRED];
  optional CSVFormat csv_format = 4 [(google.api.field_behavior) = OPTIONAL];
  // Controls whether Dumpling escapes special characters with a backslash.
  // When omitted, Dumpling keeps its default behavior (escaping enabled).
  optional bool escape_backslash = 5 [(google.api.field_behavior) = OPTIONAL];
  optional ParquetFormat parquet_format = 6 [(google.api.field_behavior) = OPTIONAL];
}

message ListExportsRequest {
  string cluster_id = 1 [(google.api.field_behavior) = REQUIRED];
  int32 page_size = 100 [(google.api.field_behavior) = OPTIONAL];
  string page_token = 101 [(google.api.field_behavior) = OPTIONAL];
  int32 skip = 102 [(google.api.field_behavior) = OPTIONAL];
}

message ListExportsResponse {
  repeated Export exports = 1;
  int32 total_size = 2;
  string next_page_token = 3;
}

message GetExportRequest {
  string cluster_id = 1 [(google.api.field_behavior) = REQUIRED];
  string export_id = 2 [(google.api.field_behavior) = REQUIRED];
}

message CreateExportRequest {
  string cluster_id = 1 [(google.api.field_behavior) = REQUIRED];
  Export export = 2 [(google.api.field_behavior) = REQUIRED];
  bool validate_only = 3 [(google.api.field_behavior) = OPTIONAL];
}

message DeleteExportRequest {
  string cluster_id = 1 [(google.api.field_behavior) = REQUIRED];
  string export_id = 2 [(google.api.field_behavior) = REQUIRED];
}

message CancelExportRequest {
  string cluster_id = 1 [(google.api.field_behavior) = REQUIRED];
  string export_id = 2 [(google.api.field_behavior) = REQUIRED];
}
