syntax = "proto2";

package wix.api;

import "google/protobuf/descriptor.proto";
import "wix/api/annotations.proto";
import "wix/api/callback.proto";

option java_multiple_files = true;
option java_outer_classname = "EntityProto";
option java_package = "com.wix.api";

/*
Marks that message X is the "main" entity of service Y
Key requirement for DomainEvents used in callback/event

package com.wixpress.examples;

service BlooperService {
    option (wix.api.service_entity).message = "com.wixpress.examples.Blooper"; // the "main" entity
    rpc Update (BlooperRequest) returns (BlooperResponse) {
        //...
    };
}

message Blooper {
   option (wix.api.entity) = {
       fqdn: "wix.examples.Blooper" // A fully qualified domain name for this entity. Example: wix.stores.catalog.product, wix.bookings.session, wix.payments.transaction
       exposure: PUBLIC // Optional exposure field. Will be used by systems like corvid and webhooks to decide if to expose events related to this entity to external parties
   };
}
*/

extend google.protobuf.ServiceOptions {
    optional ServiceEntity service_entity = 50500;
}

message ServiceEntity {
    required string message = 1;
    optional string namespace = 50501; // use to avoid naming collisions between functions listed under the same fqdn/entity. Business Schema service shall use it using this hierarchy: [Entity > namespace > action]
}

extend google.protobuf.MessageOptions {
    optional Entity entity = 50500;
    repeated DomainEvent domain_event = 50501;
}

message Entity {
    required string fqdn = 1;
    optional Exposure exposure = 2 [deprecated = true];
    repeated MessageAppendix appendix_files = 3; // declare external configuration info
    optional string old_fqdn_for_backwards_compatibility = 4; // Topic name for domain events will be based on this attribute if present (only for known legacy FQDNs)
    optional Segment.SegmentName segment = 5; // the cluster to which domain events will be produced
    repeated string implements = 6; // FQDNs of interfaces (i.e. SPIs) that this entity implements. Must begin with `wix.interfaces.`
    optional Extensible extensible = 7; // Indicates that this entity can be extended by apps using data-extension mechanism.
    optional bool trashbin = 8; // Indicates whether trash-bin is exposed on this entity.
    optional string plural = 9; // plural form of this entity. e.g. singular: `entity`; plural: `entities`;
    optional string domain_events_read_permission = 10; // permission to consume all event types of this entity from topic. affects internal/external consumers. For external webhook subscribers - MAY be overridden by event/callback `permission` field.
    optional string app_def_id = 11; // the app in devcenter that owns this entity
    optional DomainEventOptions domain_events = 12;
    optional Taggable taggable = 13; // Indicates whether this entity supports tagging
    optional QueryOptions query_options = 14;
    optional Exposure events_exposure = 15; // default events exposure, may be overridden in `wix.api.domain_event`
    optional Maturity events_maturity = 16; // default events maturity, may be overridden in `wix.api.domain_event`
    optional Persistence persistence = 17; // persistence config: type and tenancy model
    optional PermissionCatalogOptions permission_options = 18; // options for permissions catalog auto generation
}

message PermissionCatalogOptions {
    optional bool auto_generate_permissions = 1; // indicates whether to auto-generate permissions in the catalog for this entity
    optional bool auto_generate_scopes = 2; // indicates whether to auto-generate scopes in the catalog for this entity
}

message Persistence {
    enum Type {
        STANDARD = 0; // multiple records per tenant
        SINGLETON = 1; // single record per tenant
        NONE = 2; // zero records per tenant - stateless
    }
    optional Type type = 1;
}

message Extensible {

    optional string nested_extension_field_path = 1;
    // Indicates whether this entity supports filtering by extended fields which are marked in schema as `x-wix-filterable`
    optional bool filterable = 2;
    repeated NestedExtensionPointMapping nested_extension_point_mappings = 3; // mappings for nested extensionPoint
}

message NestedExtensionPointMapping {
    required string extension_point = 1; // the logical name of the extension point
    required string path = 2; // the path to the parent object that contains the `extended_fields` field
}

message MessageAppendix {
    enum Type {
        UNKNOWN_APPENDIX_TYPE = 0;
        AUTO_VELO = 1; // auto velo configuration
        DOCS_CONF = 2; // wix docs configuration: `documentation.yaml`
        MULTILINGUAL = 3; // wix multilingual configuration
        SEARCH = 4; // nile search configuration
        AUTO_SDK = 5; // auto sdk configuration 
    }
    required Type type = 1; // appendix type
    required string path = 2; // absolute from module root
}

message DomainEventOptions {
    optional bool event_sequence_number = 1; // indicated whenever `entity_event_sequence` in the envelope is set/populated for CUD events.
    optional bool deleted_include_entity = 2; // indicated whenever `deleted_entity_as_json` is set/populated for EntityDeletedEvent events.
    optional bool updated_include_modified_fields = 3; // indicated whenever `modified_fields` is set/populated for EntityUpdatedEvent events.
}

message Taggable {}

message QueryOptions {
    required Wql wql = 1; // mark fields that are available for filtering and sorting via WixQueryLanguage
    repeated Search search = 2; // mark fields that are available for search
    repeated NamedQueryOptions additional_query_options = 10; //additional query options
}

message NamedQueryOptions {
    required string name  = 1; // additional options should be named
    required Wql wql = 2; // mark fields that are available for filtering and sorting via WixQueryLanguage
    repeated Search search = 3; // mark fields that are available for search
}
