syntax = "proto3";

package wix.infra.dataextensions.v1;
option java_package = "com.wixpress.infra.dataextensions.v1";

import "google/api/annotations.proto";
import "wix/api/annotations.proto";
import "wix/api/permissions.proto";
import "wix/api/callback.proto";
import "wix/api/entity.proto";
import "wix/api/validations.proto";
import "wix/infra/dataextensions/v1/data_extension_schema.proto";
import "google/protobuf/empty.proto";
import "wix/common/domainevents/domainevents.proto";
import "wix/api/subscription.proto";
import "versions_messages.proto";
import "wix/devcenter/appsruntimewriter/v1/app_runtime_writer.proto";
import "google/protobuf/wrappers.proto";

/**
  Manages DataExtensionSchemas
 */
service DataExtensionSchemaService {
  option (wix.api.service_maturity) = GA;
  option (wix.api.service_exposure) = PUBLIC;
  option (wix.api.service_entity).message = "wix.infra.dataextensions.v1.DataExtensionSchema";

  option (wix.api.event) = {
    topic: "data_extension_schema.reindex"
    segment: PUBLIC
    payload: ".wix.dataextensions.reindex.ReindexEvent"
    exposure: INTERNAL
  };

  // Creates a user-defined data extension schema. 
  rpc CreateDataExtensionSchema (CreateDataExtensionSchemaRequest) returns (CreateDataExtensionSchemaResponse) {
    option (wix.api.crud) = {
      method: CREATE
    };
    option (wix.api.exposure) = PUBLIC;
    option (google.api.http) = {
      post: "/v1/schemas"
    };
    option (wix.api.required) = "CreateDataExtensionSchemaRequest.data_extension_schema";
    option (wix.api.required) = "CreateDataExtensionSchemaRequest.data_extension_schema.json_schema";
    option (wix.api.required) = "CreateDataExtensionSchemaRequest.data_extension_schema.fqdn";
    option (wix.api.required) = "CreateDataExtensionSchemaRequest.data_extension_schema.namespace";
    option (wix.api.writable) = "CreateDataExtensionSchemaRequest.data_extension_schema.fqdn";
    option (wix.api.writable) = "CreateDataExtensionSchemaRequest.data_extension_schema.namespace";
    option (wix.api.emits) = {
      event_type: CREATED
    };
    option (wix.api.permission).name = "DATA_EXTENSION_SCHEMA.WRITE";
  }

  // Updates a user-defined data extension schema, overriding the existing data.
  rpc UpdateDataExtensionSchema (UpdateDataExtensionSchemaRequest) returns (UpdateDataExtensionSchemaResponse) {
    option (wix.api.exposure) = PUBLIC;
    option (google.api.http) = {
      put: "/v1/schemas"
    };
    option (wix.api.required) = "UpdateDataExtensionSchemaRequest.data_extension_schema";
    option (wix.api.required) = "UpdateDataExtensionSchemaRequest.data_extension_schema.id";
    option (wix.api.required) = "UpdateDataExtensionSchemaRequest.data_extension_schema.revision";
    option (wix.api.required) = "UpdateDataExtensionSchemaRequest.data_extension_schema.json_schema";
    option (wix.api.emits) = {
      event_type: UPDATED
    };
    option (wix.api.permission).name = "DATA_EXTENSION_SCHEMA.WRITE";
  }

  // Retrieves a list of global and user-defined data extension schemas for a given FQDN.
  rpc ListDataExtensionSchemas (ListDataExtensionSchemasRequest) returns (ListDataExtensionSchemasResponse) {
    option (wix.api.exposure) = PUBLIC;
    option (wix.api.required) = "ListDataExtensionSchemasRequest.fqdn";
    option (google.api.http) = {
      get: "/v1/schemas"
    };
    option (wix.api.permission).name = "DATA_EXTENSION_SCHEMA.READ";
  }

  // Delete demo data extension schema (only allowed for demo entity fqdn: wix.prime.auto.*.demo_entity)
  rpc DeleteDemoDataExtensionSchema (DeleteDemoDataExtensionSchemaRequest) returns (DeleteDemoDataExtensionSchemaResponse) {
    option (wix.api.exposure) = PRIVATE;
    option (wix.api.required) = "DeleteDemoDataExtensionSchemaRequest.data_extension_schema_id";
    option (wix.api.emits) = {
      event_type: DELETED
    };
    option (wix.api.permission).name = "DATA_EXTENSION_SCHEMA.MANAGE_APP_DEFINED";
  }

  // Delete global data extension schema
  rpc DeleteGlobalExtensionSchema (DeleteGlobalExtensionSchemaRequest) returns (DeleteGlobalExtensionSchemaResponse) {
    option (wix.api.exposure) = PRIVATE;
    option (wix.api.required) = "DeleteGlobalExtensionSchemaRequest.fqdn";
    option (wix.api.required) = "DeleteGlobalExtensionSchemaRequest.namespace";
    option (wix.api.emits) = {
      event_type: DELETED
    };
    option (wix.api.permission).name = "DATA_EXTENSION_SCHEMA.MANAGE_APP_DEFINED";
  }

  // Deletes schemas of whitelisted metasite ids
  rpc DeleteByWhiteListedMetaSite (DeleteByWhiteListedMetaSiteRequest) returns (DeleteByWhiteListedMetaSiteResponse) {
    option (wix.api.exposure) = PUBLIC;
    option (wix.api.required) = "DeleteByWhiteListedMetaSiteRequest.meta_site_id";
    option (google.api.http) = {
      post: "/v1/schemas/delete-by-white-listed-meta-site"
    };
    option (wix.api.emits) = {
      event_type: DELETED
    };
  }

  rpc ConsumeDataExtensionSchemaEvents(wix.common.domainevents.DomainEvent) returns (google.protobuf.Empty) {
    option (wix.api.exposure) = INTERNAL;
    option (wix.api.subscription) = {
      entity: "wix.infra.dataextensions.v1.DataExtensionSchema",
    };
  }

  rpc AppRuntimeDataEventHandler (wix.devcenter.appsruntimewriter.v1.AppRuntimeDataEvent) returns (google.protobuf.Empty) {
    option (wix.api.exposure) = INTERNAL;
    option (wix.api.subscription) = {
      topic: "app-runtime-data-event"
      segment: USERS
    };
  }

  // Deletes a user defined field from the specified data extension schema.
  rpc DeleteUserDefinedFields (DeleteUserDefinedFieldsRequest) returns (DeleteUserDefinedFieldsResponse) {
    option (google.api.http) = {
      post: "/v1/schemas/delete-user-defined-fields"
    };

    option (wix.api.exposure) = PUBLIC;
    option (wix.api.required) = "DeleteUserDefinedFieldsRequest.data_extension_schema_id";
    option (wix.api.required) = "DeleteUserDefinedFieldsRequest.fields_to_delete";
    option (wix.api.emits) = {
      event_type: UPDATED
    };
    option (wix.api.permission).name = "DATA_EXTENSION_SCHEMA.DELETE_USER_DEFINED_FIELDS";
  }
}

message ListDataExtensionSchemasRequest {
  string fqdn = 1 [(wix.api.maxLength) = 255]; // [Fully qualified domain name](https://dev.wix.com/docs/rest/articles/getting-started/fqdns).
  repeated string namespaces = 2 [(wix.api.maxSize) = 100, (wix.api.maxLength) = 164]; // Namespaces within the given entity.
  repeated RequestedField fields = 3 [(wix.api.maxSize) = 5]; // Additional fields that are hidden by default. For example, fields with `"x-wix-archived": true`.
  repeated string extension_points = 4 [(wix.api.maxSize) = 20, (wix.api.maxLength) = 10]; // Extension points within the given entity.
}

message ListDataExtensionSchemasResponse {
  repeated DataExtensionSchema data_extension_schemas = 1; // Requested schemas.
}

message UpdateDataExtensionSchemaRequest {
  DataExtensionSchema data_extension_schema = 1; // Schema to update.
}

message UpdateDataExtensionSchemaResponse {
  DataExtensionSchema data_extension_schema = 1; // Updated schema.
}

message CreateDataExtensionSchemaRequest {
  DataExtensionSchema data_extension_schema = 1; // Schema to create.
}

message CreateDataExtensionSchemaResponse {
  DataExtensionSchema data_extension_schema = 1; // Created schema.
}

enum RequestedField {
  // Undefined requested field.
  UNKNOWN_REQUESTED_FIELD = 0;
  // Returns `x-wix-archived` fields in `DataExtensionSchema.json_schema`.
  ARCHIVED = 1;
}

message DeleteDemoDataExtensionSchemaRequest {
  string data_extension_schema_id = 1 [(wix.api.format) = GUID]; // Schema ID.
}

message DeleteGlobalExtensionSchemaRequest {
  string fqdn = 1 [(wix.api.maxLength) = 255]; // fqdn
  string namespace = 2 [(wix.api.maxLength) = 164]; // namespace
  string extension_point = 3 [(wix.api.maxLength) = 20]; // extension point
  DataExtensionSchema.State state = 4; // state
}

message DeleteGlobalExtensionSchemaResponse {}

message DeleteDemoDataExtensionSchemaResponse {}

message DeleteByWhiteListedMetaSiteRequest{
  // Meta site id
  string meta_site_id = 1 [(wix.api.format) = GUID];
}

message DeleteByWhiteListedMetaSiteResponse {
  // has more
  bool has_more = 1;
}

message DeleteUserDefinedFieldsRequest {
  google.protobuf.StringValue data_extension_schema_id = 1 [(wix.api.format) = GUID]; // Schema ID.
  repeated string fields_to_delete = 2 [(wix.api.minSize) = 1, (wix.api.maxSize) = 10, (wix.api.maxLength) = 1000]; // List of fields paths to delete. For example, ["size", "personalization.specialInstructions"].
}

message DeleteUserDefinedFieldsResponse {
  DataExtensionSchema data_extension_schema = 1; // Updated schema.
}
