syntax = "proto3";

package wix.dealerBackoffice.api.v1;

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

import "wix/api/annotations.proto";
import "wix/api/validations.proto";
import "wix/api/permissions.proto";
import "wix/api/entity.proto";
import "wix/dealerBackoffice/api/v1/targeting.proto";
import "wix/dealerBackoffice/api/v1/managers.proto";

option java_package = "com.wix.dealerBackoffice.api.v1";

// Endpoint for managing dealer audiences (see description of Audience entity)
service AudienceService {
  option (wix.api.service_entity).message = "wix.dealerBackoffice.api.v1.Audience";
  option (wix.api.service_maturity) = ALPHA;
  option (wix.api.service_exposure) = INTERNAL;

  // Get all audiences by ids.
  // No exceptions are thrown if some/all audiences were not found.
  // Worst case scenario (no audiences are found by provided id list) — you get empty array.
  rpc GetAudiencesBulk (GetAudiencesBulkRequest) returns (GetAudiencesBulkResponse) {
    option (google.api.http).get = "/v1/dealer/backoffice/audiences/bulk";
    option (wix.api.required) = "GetAudiencesBulkRequest.audience_ids";
    option (wix.api.permission).name = "DEALER_BACKOFFICE.READ_AUDIENCE";
  }

  // Get an audience by id.
  // GrpcStatus.NOT_FOUND is thrown if requested audience is not found in DB
  rpc GetAudience (GetAudienceRequest) returns (GetAudienceResponse) {
    option (google.api.http).get = "/v1/dealer/backoffice/audiences/{audience_id}";
    option (wix.api.required) = "GetAudienceRequest.audience_id";
    option (wix.api.permission).name = "DEALER_BACKOFFICE.READ_AUDIENCE";
    option (wix.api.crud) = {
      method: GET_ITEM
      get_options: {
        id_field: "audience_id"
      }
    };
  }

  // List all audiences
  rpc ListAudiences (ListAudiencesRequest) returns (ListAudiencesResponse) {
    option (google.api.http).get = "/v1/dealer/backoffice/audiences";
    option (wix.api.permission).name = "DEALER_BACKOFFICE.READ_AUDIENCE";
  }

  rpc CreateAudience (CreateAudienceRequest) returns (CreateAudienceResponse) {
    option (google.api.http).post = "/v1/dealer/backoffice/audiences";
    option (wix.api.permission).name = "DEALER_BACKOFFICE.MUTATE_AUDIENCE";
    option (wix.api.required) = "CreateAudienceRequest.audience";
    option (wix.api.crud) = {
      method: CREATE
      create_options: {
        item_field: "audience"
      }
    };
    option (wix.api.callback) = {
      event_type: CREATED
    };
  }

  // Update audience.
  // Cannot update audience criteria or external criteria. Use SetAudienceCriteria instead.
  rpc updateAudience (UpdateAudienceRequest) returns (UpdateAudienceResponse) {
    option (google.api.http).put = "/v1/dealer/backoffice/audiences";
    option (wix.api.permission).name = "DEALER_BACKOFFICE.MUTATE_AUDIENCE";
  }

  rpc ArchiveAudience (ArchiveAudienceRequest) returns (ArchiveAudienceResponse) {
    option (wix.api.required) = "ArchiveAudienceRequest.audience_id";
    option (google.api.http).post = "/v1/dealer/backoffice/audiences/archive";
    option (wix.api.permission).name = "DEALER_BACKOFFICE.MUTATE_AUDIENCE";
  }

  // Set specific audience criteria
  rpc SetAudienceCriteria(SetAudienceCriteriaRequest) returns (SetAudienceCriteriaResponse) {
    option (google.api.http).put = "/v1/dealer/backoffice/audiences/{audience_id}/criteria";
    option (wix.api.permission).name = "DEALER_BACKOFFICE.MUTATE_AUDIENCE";
  }

  // Migrate audience to new Nile Audience Service
  rpc MigrateAudience (MigrateAudienceRequest) returns (MigrateAudienceResponse) {
    option (google.api.http).put = "/v1/dealer/backoffice/audiences/{audience_id}/migrate";
    option (wix.api.permission).name = "DEALER_BACKOFFICE.MUTATE_AUDIENCE";
  }
}

// Single audience request
message GetAudienceRequest {
  // required
  string audience_id = 1 [(wix.api.format) = GUID];
}

// Single audience response
message GetAudienceResponse {
  Audience audience = 1;
}

message ListAudiencesRequest {
}

// List all audiences response
message ListAudiencesResponse {
  repeated Audience audiences = 1;
}

message CreateAudienceRequest {
  Audience audience = 1;
}

message CreateAudienceResponse {
  Audience audience = 1;
}

message UpdateAudienceRequest {
  Audience audience = 1;
}

message UpdateAudienceResponse {
  Audience audience = 1;
}

message ArchiveAudienceRequest {
  string audience_id = 1 [(wix.api.format) = GUID, (wix.api.readOnly) = true];
}

message ArchiveAudienceResponse {}

message SetAudienceCriteriaRequest {
  string audience_id = 1 [(wix.api.format) = GUID, (wix.api.readOnly) = true];

  CustomFilterCriteria criteria = 2;

  ExternalCriteria external_criteria = 3;

  Audience.Level level = 4;
}

message SetAudienceCriteriaResponse {
  Audience audience = 1;
}

// Multi audiences request
message GetAudiencesBulkRequest {
  // required
  repeated string audience_ids = 1 [(wix.api.format) = GUID];
}

// Multi audiences bulk response
message GetAudiencesBulkResponse {
  repeated Audience audiences = 1;
}

message MigrateAudienceRequest {
  string audience_id = 1 [(wix.api.format) = GUID, (wix.api.readOnly) = true];
}

message MigrateAudienceResponse { }

// Audience defines a group of Wix users and used as a filter in dealer offers https://bo.wix.com/wix-docs/rest/internal-tools/dealer-offers-serving/introduction
// When dealer offer has audience filter, dealer filters the offer based on wether user belongs to provided
// audience or not.
//
// Audience targeting currently supports bi profile user groups, site groups and simple bi field value
// conditions https://bo.wix.com/bi-profile-service#/groups
// Dor example 'Editor X users' audience mean "all users who has `editor_x_users` in  bi profile user_groups".
message Audience {
  option (wix.api.entity) = {fqdn:"wix.dealer.v1.audience", segment: USERS};

  enum Level {
    UNKNOWN = 0;
    USER = 1;
    SITE = 2;
  };

  // Unique audience guid
  string guid = 1 [(wix.api.format) = GUID, (wix.api.readOnly) = true];

  // Audience name
  google.protobuf.StringValue name = 2 [(wix.api.minLength) = 1];

  // Audience description
  google.protobuf.StringValue description = 3 [(wix.api.minLength) = 1];

  // URL of wix bi service catalog on profile field used for this audience
  google.protobuf.StringValue field_link = 4 [(wix.api.minLength) = 1];

  // Used as link to bi table for reports of audiences
  google.protobuf.StringValue offline_source_table = 5 [(wix.api.minLength) = 1];

  // Used as link to bi table for reports of audiences
  google.protobuf.StringValue offline_query_condition = 6;

  // Category of the audience (for example "App Funnels")
  google.protobuf.StringValue category = 7 [(wix.api.minLength) = 1];

  // Label of the audience (for example "Events")
  google.protobuf.StringValue label = 8 [(wix.api.minLength) = 1];

  // BI filter criteria, corresponding to this audience.
  // Basically a user belongs to this audience if and only if this criteria for the user is satisfied.
  // Different audiences should have different criterias.
  CustomFilterCriteria criteria = 9;

  // Priority of the audience to be used in BO UI etc.
  google.protobuf.Int32Value ui_order = 10;

  // If this audience is updated in realtime or not (from BI side).
  google.protobuf.BoolValue real_time_update = 11 [(wix.api.readOnly) = true];

  // Who are the managers of the audience in Wix
  ManagerIdsWrapper managers_ids = 12;

  reserved 13;

  // SPI for audience
  ExternalCriteria external_criteria = 14;

  Level level = 15;
}
