//flynt-disable renamed-field-breaking-change, missing-string-validations
syntax = "proto3";
import "google/protobuf/timestamp.proto";
import "google/protobuf/struct.proto";
import "google/protobuf/wrappers.proto";
import "wix/api/annotations.proto";

package wix.common.domainevents;


option java_multiple_files = true;
option java_package = "com.wix.common.domainevents";

message EntityCreatedEvent {
    string entity_as_json = 2 [(wix.api.pii) = true];

    // Indicates the event was triggered by a restore-from-trashbin operation for a previously deleted entity
    google.protobuf.BoolValue triggered_by_undelete = 3 [(.wix.api.field_exposure) = PRIVATE];

    // Indicates the event was triggered by a restore-from-trashbin operation for a previously deleted entity
    RestoreInfo restore_info = 4;

    // WIP
    google.protobuf.StringValue additional_metadata_as_json = 5 [(wix.api.pii) = true, (.wix.api.field_exposure) = PRIVATE];
}

message EntityUpdatedEvent {
    // Since platformized APIs only expose PATCH and not PUT we can't assume that the fields sent from the client are the actual diff.
    // This means that to generate a list of changed fields (as opposed to sent fields) one needs to traverse both objects.
    // We don't want to impose this on all developers and so we leave this traversal to the notification recipients which need it.
    string current_entity_as_json = 2 [(wix.api.pii) = true];

    // This field is currently part of the of the EntityUpdatedEvent msg, but scala/node libraries which implements the domain events standard
    // wont populate it / have any reference to it in the API.
    // The main reason for it is that fetching the old entity from the DB will have a performance hit on an update operation so unless truly needed,
    // the developer should send only the new (current) entity.
    // An additional reason is not wanting to send this additional entity over the wire (kafka) since in some cases it can be really big
    //Developers that must reflect the old entity will have to implement their own domain event sender mechanism which will follow the DomainEvent proto message.
    google.protobuf.StringValue previous_entity_as_json = 3 [(wix.api.pii) = true, deprecated = true, (.wix.api.field_exposure) = INTERNAL];

    reserved 4;

    // WIP - This property will hold both names and previous values of the updated fields of the entity.
    // For more details please see [adr](https://docs.google.com/document/d/1PdqsOM20Ph2HAkmx8zvUnzzk3Sekp3BR9h34wSvsRnI/edit#heading=h.phlw87mh2imx) or [issue](https://github.com/wix-private/nile-tracker/issues/363)
    map<string, google.protobuf.Value> modified_fields = 5 [(wix.api.pii) = true, (.wix.api.field_exposure) = PRIVATE];

    // WIP
    google.protobuf.StringValue additional_metadata_as_json = 6 [(wix.api.pii) = true, (.wix.api.field_exposure) = PRIVATE];
}

message EntityDeletedEvent {
    // Indicates if the entity is sent to trash-bin. only available when trash-bin is enabled
    google.protobuf.BoolValue moved_to_trash = 1 [(.wix.api.field_exposure) = PRIVATE];

    // Entity that was deleted.
    google.protobuf.StringValue deleted_entity_as_json = 2 [(wix.api.pii) = true];

    // WIP
    google.protobuf.StringValue additional_metadata_as_json = 3 [(wix.api.pii) = true, (.wix.api.field_exposure) = PRIVATE];
}

message ActionEvent {
    string body_as_json = 2 [(wix.api.pii) = true];
}

message DomainEvent {
    option (.wix.api.decomposition_minimum_fields) = {
        field: "id"
        field: "entity_fqdn"
        field: "slug"
        field: "entity_id"
        field: "event_time"
    };
    // Event ID. With this ID you can easily spot duplicated events and ignore them.
    string id = 1;
    // Fully Qualified Domain Name of an entity. This is a unique identifier assigned to the API main business entities.
    // For example, `wix.stores.catalog.product`, `wix.bookings.session`, `wix.payments.transaction`.
    string entity_fqdn = 2;
    // Event action name, placed at the top level to make it easier for users to dispatch messages.
    // For example: `created`/`updated`/`deleted`/`started`/`completed`/`email_opened`.
    string slug = 3;
    // ID of the entity associated with the event.
    string entity_id = 4;
    oneof body {
        EntityCreatedEvent created_event = 5;
        EntityUpdatedEvent updated_event = 6;
        EntityDeletedEvent deleted_event = 7;
        ActionEvent action_event = 8;
    }
    reserved 12;
    // Event timestamp in [ISO-8601](https://en.wikipedia.org/wiki/ISO_8601) format and UTC time. For example, `2020-04-26T13:57:50.699Z`.
    google.protobuf.Timestamp event_time = 9;
    // Whether the event was triggered as a result of a privacy regulation application
    // (for example, GDPR).
    google.protobuf.BoolValue triggered_by_anonymize_request = 10;

    // If present, indicates the action that triggered the event.
    google.protobuf.StringValue originated_from = 11;

    // A sequence number that indicates the order of updates to an entity. For example, if an entity was updated at 16:00 and then again at 16:01, the second update will always have a higher sequence number.
    // You can use this number to make sure you're handling updates in the right order. Just save the latest sequence number on your end and compare it to the one in each new message. If the new message has an older (lower) number, you can safely ignore it.
    google.protobuf.Int64Value entity_event_sequence = 13;
}

// Used for batch consumers
message DomainEvents {
  repeated DomainEvent domain_events = 1;
}

message RestoreInfo {
  google.protobuf.Timestamp deleted_date = 1;
}
