syntax = "proto3";

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

import "google/protobuf/wrappers.proto";
import "wix/api/validations.proto";
import "wix/api/annotations.proto";
import "google/protobuf/timestamp.proto";

/**
  DataExtensionSchema is the main entity of DataExtensionSchemaService
 */
message DataExtensionSchema {
  google.protobuf.StringValue id = 1 [(wix.api.format) = GUID, (wix.api.readOnly) = true, (wix.api.immutable) = true]; // Unique identifier for a schema
  google.protobuf.StringValue fqdn = 2 [(wix.api.immutable) = true, (wix.api.maxLength) = 255]; // FQDN of the entity that this schema extends
  /*
  Namespace of this extension. may be:
    * `@company/app1`
        An app in dev-center that defines this extension for itself.
    * `_user_fields`
        A user that defines this extension for self usage.
  */
  google.protobuf.StringValue namespace = 3 [(wix.api.maxLength) = 164];

  /*
  Schema definition of this extension
  */
  DataExtensionTypedSchema schema = 5;
  google.protobuf.Timestamp updated_date = 6 [(wix.api.readOnly) = true]; // last updated
  google.protobuf.Timestamp created_date = 7 [(wix.api.readOnly) = true]; // creation date
  google.protobuf.Int64Value revision = 8 [(wix.api.readOnly) = true]; // revision of data record

  google.protobuf.StringValue app_def_id = 101 [(wix.api.format) = GUID, (wix.api.readOnly) = true, (wix.api.field_exposure) = PRIVATE]; // app def if of the app in the given namespace

  google.protobuf.Int32Value max_limit_bytes = 9 [(wix.api.readOnly) = true]; // Maximum allowed schema size in bytes
  google.protobuf.Int32Value current_size_bytes = 10 [(wix.api.readOnly) = true]; // Current schema size in bytes

  string extension_point = 11 [(wix.api.immutable) = true, (wix.api.maxLength) = 20]; // the name of the extension point this schema is extending. by default it will be "ROOT"
  State state = 12 [(wix.api.readOnly) = true, (wix.api.field_exposure) = PRIVATE]; // Indicates whether the schema is in production or draft state.

  enum State {
    UNKNOWN_STATUS = 0 [(.wix.api.enum_exposure) = PRIVATE, (.wix.api.enum_maturity) = NOT_IMPLEMENTED];
    PRODUCTION = 1 [(.wix.api.enum_exposure) = PRIVATE]; // Production schema, this is normal state for both user-defined and app-defined schemas.
    DRAFT = 2 [(.wix.api.enum_exposure) = PRIVATE]; // Draft schema, this is meant for app-defined schemas during app development.
  }
}

message DataExtensionTypedSchema {
  repeated DataExtensionSchemaField properties = 1 [(wix.api.maxSize) = 1000]; // list of properties in this schema
}

message DataExtensionSchemaField {
  string name = 1 [(wix.api.maxLength) = 64]; //Field name
  google.protobuf.StringValue title = 2 [(wix.api.maxLength) = 255]; //Field title
  google.protobuf.StringValue description = 3 [(wix.api.maxLength) = 255]; //Field description
  FieldTypeInfo field_type = 4; //Field type info
  Permissions permissions = 5;//Field permissions
}

message FieldTypeInfo {
  oneof types {
    option (wix.api.aligned_with_enum) = {field:"type"; suffix: "type"};

    StringType string_type = 1; // string
    ObjectType object_type = 2; //object
    ArrayType array_type = 3; //array
    NumberType number_type = 4; //number
    IntegerType integer_type = 5; //integer
    BooleanType boolean_type = 6; //boolean
  }
  Type type = 10; // Type of schema field

  enum Type {
    UNKNOWN = 0;
    STRING = 1;
    OBJECT = 2;
    ARRAY = 3;
    NUMBER = 4;
    INTEGER = 5;
    BOOLEAN = 6;
  }
}

message ArrayItemTypeInfo {
  oneof types {
    option (wix.api.aligned_with_enum) = {field:"type"; suffix: "type"};

    StringType string_type = 1; // string
    ObjectType object_type = 2; //object
    NumberType number_type = 4; //number
    IntegerType integer_type = 5; //integer
    BooleanType boolean_type = 6; //boolean
  }
  Type type = 10; // Type of schema field

  enum Type {
    UNKNOWN = 0;
    STRING = 1;
    OBJECT = 2;
    NUMBER = 4;
    INTEGER = 5;
    BOOLEAN = 6;
  }
}

message StringType {
  google.protobuf.Int32Value min_length = 1; //minimum length of the string
  google.protobuf.Int32Value max_length = 2 [(wix.api.max) = 10000]; //maximum length of the string
  Format format = 3; // string format
  bool pii = 4; //is pii
  Display display = 5; //Display options
  bool filterable = 6; //Can be filtered with search
  repeated string enum = 7 [(wix.api.maxSize) = 100, (wix.api.maxLength) = 64]; //If not empty, then this is a string enum
  bool translatable = 8; //Can be translated with multi-lingual

  enum Format {
    NONE = 0;
    GUID = 1;
    HOSTNAME = 2;
    COLOR_HEX = 3;
    URI = 4;
    CURRENCY = 5;
    LANGUAGE = 6;
    DATE = 7;
    DATE_TIME = 8;
    TIME = 9;
    SINGLE_LINE = 10;
    EMAIL = 11;
    PHONE = 12;
  }
}

message ObjectType {
  repeated DataExtensionSchemaField properties = 1 [(wix.api.maxSize) = 1000]; // list of properties in this schema
}

message ArrayType {
  google.protobuf.Int32Value min_items = 1; //minimum size of array
  int32 max_items = 2 [(wix.api.max) = 1000]; //maximum size of array
  ArrayItemTypeInfo item_type = 3; //The type of items the array contains
  bool pii = 4; //is pii
  bool filterable = 5; //Can be filtered with search
}

message NumberType {
  oneof min {
    double minimum = 1; //Minimum allowed value
    double exclusive_minimum = 2; //Exclusive minimum allowed value
  }
  oneof max {
    double maximum = 3; //Maximum allowed value
    double exclusive_maximum = 4; //Exclusive maximum allowed value
  }
  bool pii = 5; //is pii
  Display display = 6; //Display options
  bool filterable = 7; //Can be filtered with search
  repeated double enum = 8 [(wix.api.maxSize) = 100]; //If not empty, then this is a numerical enum
}

message IntegerType {
  oneof min {
    int64 minimum = 1; //Minimum value
    int64 exclusive_minimum = 2; //Exclusive minimum value
  }
  oneof max {
    int64 maximum = 3; //Maximum allowed value
    int64 exclusive_maximum = 4; //Exclusive maximum allowed value
  }
  bool pii = 5; //is pii
  Display display = 6; //Display options
  bool filterable = 7; //Can be filtered with search
  repeated int64 enum = 8 [(wix.api.maxSize) = 100]; //If not empty, then this is an integer enum
}

message BooleanType {
  bool filterable = 1; //Can be filtered with search
}

message Display {
  google.protobuf.StringValue place_holder = 1 [(wix.api.maxLength) = 255]; //Place holder text for field
}

message Permissions {
  PermissionFlags read = 1; //Read permissions
  PermissionFlags write = 2; //Write permissions
}

message PermissionFlags {
  bool users_of_users = 1; //UoU allowed
  bool users = 2; //Users allowed
  bool owning_app = 3; //Owning app allowed
  bool apps = 4; //All apps
}