//flynt-disable missing-autovelo-def
//flynt-disable required-annotation-breaking-change
//flynt-disable http-mapping-breaking-change
//flynt-disable missing-taggable-annotation
//flynt-disable update-missing-inferred-fieldmask
//flynt-disable missing-entity-id-field
//flynt-disable update-must-support-http-patch

syntax = "proto3";

package com.wixpress.cloud.data.api.externaldatabase;

import "google/protobuf/empty.proto";
import "google/api/annotations.proto";
import "google/protobuf/struct.proto";
import "google/protobuf/wrappers.proto";
import "wix/api/annotations.proto";
import "wix/api/callback.proto";
import "wix/api/entity.proto";
import "wix/api/permissions.proto";
import "wix/api/validations.proto";
import "wix/sdk/definitions.proto";
import "com/wixpress/cloud/data/api/common.proto";
import "com/wixpress/cloud/data/api/externaldatabase/upstream/externaldatabase/wix/common/paging.proto";

// Manage external database connections.
service ExternalDatabaseConnectionService {
  option (wix.api.service_entity).message = "com.wixpress.cloud.data.api.externaldatabase.ExternalDatabaseConnection";
  option (wix.api.service_exposure) = PUBLIC;
  option (wix.api.service_maturity) = GA;

  // Retrieves the specified external database connection.
  rpc GetExternalDatabaseConnection (GetExternalDatabaseConnectionRequest) returns (GetExternalDatabaseConnectionResponse) {
    option (wix.api.required) = "GetExternalDatabaseConnectionRequest.name";
    option (wix.api.permission).name = "WIX_DATA.GET_EXTERNAL_DATABASE_CONNECTION";
    option (google.api.http).get = "/v1/external-database-connections/{name=**}";

    option (wix.api.crud) = {
      method: GET_ITEM
      get_options: {
        id_field: "name",
        item_field: "external_database_connection"
      }
    };
  }

  // Lists all of the site's external database connections of all adaptor types.
  rpc ListExternalDatabaseConnections (ListExternalDatabaseConnectionsRequest) returns (ListExternalDatabaseConnectionsResponse) {
    option (wix.api.permission).name = "WIX_DATA.LIST_EXTERNAL_DATABASE_CONNECTIONS";
    option (google.api.http).get = "/v1/external-database-connections";
  }

  // Creates a new external database connection.
  //
  // Before you can establish a connection between a site and an external database, you must prepare an external database adaptor.
  rpc CreateExternalDatabaseConnection (CreateExternalDatabaseConnectionRequest) returns (CreateExternalDatabaseConnectionResponse) {
    option (wix.api.required) = "CreateExternalDatabaseConnectionRequest.external_database_connection";
    option (wix.api.writable) = "CreateExternalDatabaseConnectionRequest.external_database_connection.name";
    option (wix.api.required) = "CreateExternalDatabaseConnectionRequest.external_database_connection.name";
    option (wix.api.required) = "CreateExternalDatabaseConnectionRequest.connection_type";
    option (wix.api.permission).name = "WIX_DATA.CREATE_EXTERNAL_DATABASE_CONNECTION";
    option (google.api.http).post = "/v1/external-database-connections";
    option (.wix.sdk.signature) = {
      params: "external_database_connection",
      params: "connection_type"
    };

    // Triggered when an external database connection is created.
    option (wix.api.emits) = {
      event_type: CREATED,
    };

    option (wix.api.crud) = {
      method: CREATE
      create_options: {
        item_field: "external_database_connection"
      }
    };
  }

  // Updates an external database connection.
  //
  // > **Note:** After an external database connection is updated, it only contains the values provided in the request. All previous values are lost.
  rpc UpdateExternalDatabaseConnection (UpdateExternalDatabaseConnectionRequest) returns (UpdateExternalDatabaseConnectionResponse) {
    option (wix.api.required) = "UpdateExternalDatabaseConnectionRequest.external_database_connection";
    option (wix.api.required) = "UpdateExternalDatabaseConnectionRequest.external_database_connection.name";
    option (wix.api.permission).name = "WIX_DATA.UPDATE_EXTERNAL_DATABASE_CONNECTION";
    option (google.api.http).put = "/v1/external-database-connections/{externalDatabaseConnection.name=**}";

    // Triggered when an external database connection is updated.
    option (wix.api.emits) = {
      event_type: UPDATED
    };

    option (wix.api.crud) = {
      method: UPDATE
      update_options: {
        item_field: "external_database_connection"
      }
    };
  }

  // Deletes an external database connection.
  //
  // > **Note:** Once an external database connection is deleted, it can't be restored. To reconnect the database, create a new external database connection.
  rpc DeleteExternalDatabaseConnection (DeleteExternalDatabaseConnectionRequest) returns (DeleteExternalDatabaseConnectionResponse) {
    option (wix.api.required) = "DeleteExternalDatabaseConnectionRequest.name";
    option (wix.api.permission).name = "WIX_DATA.DELETE_EXTERNAL_DATABASE_CONNECTION";
    option (google.api.http).delete = "/v1/external-database-connections/{name=**}";

    // Triggered when an external database connection is deleted.
    option (wix.api.emits) = {
      event_type: DELETED
    };

    option (wix.api.crud) = {
      method: DELETE
      delete_options: {
        id_field: "name"
      }
    };
  }
}

// An external database connection defines a connection between an external database and a Wix site.
message ExternalDatabaseConnection {
  option (.wix.api.entity) = {
    fqdn: "wix.data.v1.external_database_connection",
    app_def_id: "e593b0bd-b783-45b8-97c2-873d42aacaf4",
    segment: USERS,
    appendix_files: {
      type: AUTO_VELO
      path: "appendices/velo"
    },
    appendix_files: {
      type: DOCS_CONF
      path: "appendices/docs"
    },
    domain_events_read_permission: "WIX_DATA.GET_EXTERNAL_DATABASE_CONNECTION"
  };

  // Triggered when an external database connection is created.
  option (.wix.api.domain_event) = {
    event_type: CREATED
    exposure: PUBLIC
  };

  // Triggered when an external database connection is updated.
  option (.wix.api.domain_event) = {
    event_type: UPDATED
    exposure: PUBLIC
  };

  // Triggered when an external database connection is deleted.
  option (.wix.api.domain_event) = {
    event_type: DELETED
    exposure: PUBLIC
  };

  message ConnectionStatus {
    enum CauseOfFailure {
      // No connection failure.
      NONE = 0;

      // General communication failure.
      COMMUNICATION_FAILURE = 1;

      // External database host is unreachable.
      DESTINATION_HOST_UNREACHABLE = 2;

      // Unauthorized to access the external database.
      UNAUTHORIZED = 3;

      // `endpoint` is not set.
      DESTINATION_ENDPOINT_NOT_DEFINED = 4;
    }
    enum CollectionsFound {
      // Attempt to connect to the external database failed, so status is unknown.
      UNKNOWN = 0;
      // External database has collections.
      YES = 1;
      // External database does not have any collections.
      NO = 2;
    }

    // Whether the connection was established successfully.
    bool successful = 1;

    // Whether and why the connection attempt failed.
    CauseOfFailure cause_of_failure = 2;

    // Whether the external database has collections.
    CollectionsFound has_collections = 3 [(.wix.api.readOnly) = true];
  };
  message Capabilities {
    // Whether the external database supports creating new collections, updating the structure of existing collections, or deleting them.
    bool collection_modifications_supported = 1;
    // Field types the external database supports. Applies only when `collectionModificationsSupported` is set to `true`.
    repeated FieldType field_types = 2 [(.wix.api.maxSize) = 255];
  }

  // Name of the external database connection.
  //
  // An external database connection can connect to one or more external data collections or tables. These appear as `connectionName/dataCollectionId`.
  string name = 1 [(.wix.api.immutable) = true, (wix.api.minLength) = 1, (wix.api.maxLength) = 255];

  // Base URL for accessing and managing data in the external database. For example: `https://example.com/my-external-database`.
  // Required for STANDALONE connection type.
  google.protobuf.StringValue endpoint = 2 [(wix.api.format) = WEB_URL];

  // Settings specified to the external database connection as part of each request.
  //
  // These settings can relate to authentication, tenancy, or provide any other information necessary for processing a request. Their content and structure depend on the specific requirements of the external database's API.
  google.protobuf.Struct configuration = 3;

  // Status of the external database connection. Includes whether the connection was established successfully, and if not, the reason for the failure.
  ConnectionStatus connection_status = 4 [(.wix.api.readOnly) = true];

  // Protocol version of the external database connection.
  ProtocolVersion protocol_version = 5 [(.wix.api.readOnly) = true, (wix.api.field_exposure) = INTERNAL];

  // Public key used to validate requests to the external database.
  google.protobuf.StringValue public_key = 6 [(.wix.api.readOnly) = true, (.wix.api.maxLength) = 128];

  // Capabilities of the external database.
  Capabilities capabilities = 7 [(.wix.api.readOnly) = true];

  // ID of the app used to integrate this external database.
  google.protobuf.StringValue application_id = 8 [(.wix.api.readOnly) = true, (.wix.api.maxLength) = 128, (wix.api.field_exposure) = INTERNAL];

  enum ProtocolVersion {
    UNKNOWN_PROTOCOL_VERSION = 0  [(.wix.api.enum_maturity) = NOT_IMPLEMENTED];
    V1 = 1;
    V2 = 2;
    V3 = 3;
  }
}


message GetExternalDatabaseConnectionRequest {
  // Name of the external database connection to retrieve.
  string name = 1 [(.wix.api.maxLength) = 255];
}

message GetExternalDatabaseConnectionResponse {
  // Details of the retrieved external database connection.
  ExternalDatabaseConnection external_database_connection = 1;
}

message ListExternalDatabaseConnectionsRequest {
  // Paging metadata.
  upstream.externaldatabase.wix.common.Paging paging = 1;
}

message ListExternalDatabaseConnectionsResponse {
  // List of external database connections.
  repeated ExternalDatabaseConnection external_database_connections = 1;

  // Paging metadata.
  upstream.externaldatabase.wix.common.PagingMetadata paging_metadata = 2;
}

message CreateExternalDatabaseConnectionRequest {
  // External database connection details.
  ExternalDatabaseConnection external_database_connection = 1;

  // Connection type. The connection type specifies the type of adaptor by which the external collection is integrated with the site.
  ConnectionType connection_type = 2;

  // Whether the database is managed by Wix.
  bool database_managed_by_wix = 3 [(wix.api.field_exposure) = PRIVATE];
}

enum ConnectionType {
  // Unknown connection type.
  UNKNOWN_CONNECTION_TYPE = 0  [(.wix.api.enum_maturity) = NOT_IMPLEMENTED];
  // External database connection based on the legacy [external database collection service plugin](https://dev.wix.com/docs/velo/api-reference/wix-data-v2/service-plugins-spis/external-database-collection-legacy/introduction).
  STANDALONE = 1;
  // External database connection based on the [external database service plugin](https://dev.wix.com/docs/rest/business-solutions/cms/service-plugins/external-database-service-plugin/introduction).
  WIX_SERVICE_PLUGIN = 2;
}

message CreateExternalDatabaseConnectionResponse {
  // Details of the created external database connection.
  ExternalDatabaseConnection external_database_connection = 1;
}

message UpdateExternalDatabaseConnectionRequest {
  // Updated external database connection. The existing connection is replaced with this version.
  ExternalDatabaseConnection external_database_connection = 2;
}

message UpdateExternalDatabaseConnectionResponse {
  // Details of the updated external database connection.
  ExternalDatabaseConnection external_database_connection = 1;
}

message DeleteExternalDatabaseConnectionRequest {
  // Name of the external database connection to delete.
  string name = 1 [(.wix.api.maxLength) = 255];
}

message DeleteExternalDatabaseConnectionResponse {

}
