syntax = "proto3";

package wix.dealerBackoffice.api.v1;

import "google/protobuf/timestamp.proto";
import "google/protobuf/wrappers.proto";
import "google/protobuf/field_mask.proto";
import "google/api/annotations.proto";
import "wix/api/annotations.proto";
import "wix/api/validations.proto";
import "wix/http/field_mask.proto";
import "wix/api/entity.proto";
import "wix/api/callback.proto";
import "wix/api/sla.proto";
import "wix/dealerBackoffice/api/v1/upstream/wix/common/query.proto";
import "wix/dealerBackoffice/api/v1/upstream/wix/common/paging.proto";
import "wix/api/permissions.proto";
import "wix/personalize/optimization_test/v1/test.proto";
import "wix/personalize/variants_generator/v1/variant.proto";

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

// flynt-disable field-type-breaking-change
message OfferTest {
  // flynt-disable missing-taggable-annotation
  option (wix.api.entity) = {
    fqdn:"wix.dealer.v1.offer_test"
    segment: USERS
    domain_events_read_permission: "DEALER_BACKOFFICE.OFFER_TEST_DOMAIN_EVENTS_READ"
    query_options: {
      wql: {
        pattern: {
          operator: ALL_APPLICABLE_OPERATORS
          field: "id"
          field: "offer_id"
          field: "experiment_id"
          field: "offer_schema_key"
          field: "status"
          sort: BOTH
        }
      }
    }
  };

  option (wix.api.domain_event) = {
    event_type: CREATED
    exposure: PUBLIC
    maturity: ALPHA
  };
  option (wix.api.domain_event) = {
    event_type: UPDATED
    exposure: PUBLIC
    maturity: ALPHA
  };
  option (wix.api.domain_event) = {
    event_type: DELETED
    exposure: PUBLIC
    maturity: ALPHA
  };

  // Unique test id
  google.protobuf.StringValue id = 1 [(wix.api.format) = GUID, (wix.api.readOnly) = true];

  // The offer the test belongs to
  google.protobuf.StringValue offer_id = 2 [(wix.api.format) = GUID];

  // The experiment id in Petri
  google.protobuf.StringValue experiment_id = 3 [(wix.api.format) = GUID];

  // The text key in the offer schema
  google.protobuf.StringValue offer_schema_key = 4 [(wix.api.maxLength) = 300];

  // Variants list of id and the variant text
  repeated Variant variants = 5 [(wix.api.maxSize) = 10];

  // The test id in the Optimization Tester Service
  google.protobuf.StringValue optimization_test_id = 6 [(wix.api.format) = GUID];

  // The time the test created
  google.protobuf.Timestamp started_at = 7 [(wix.api.readOnly) = true];

  // The time the test ended
  google.protobuf.Timestamp ended_at = 8 [(wix.api.readOnly) = true];

  // The kpis send to Petri
  repeated string kpis = 9 [(wix.api.format) = GUID, (wix.api.maxSize) = 10];

  // The requested duration for the test in days
  google.protobuf.Int32Value duration_in_days = 10;

  // Slack Addresses for the users to send updates about the test
  // flynt-disable renamed-field-breaking-change
  repeated string email_addresses = 11 [(wix.api.format) = EMAIL, (wix.api.maxSize) = 10];

  // The status of the test in Petri
  TestStatus status = 12;

  // The result of the test in Wixer
  repeated wix.personalize.optimization_test.v1.Test.MvtResults experiment_results = 13 [(wix.api.maxSize) = 10];

  // The user id created the offer test
  google.protobuf.StringValue created_by = 14 [(wix.api.format) = GUID, (wix.api.readOnly) = true];

  // The user id last updated the offer test
  google.protobuf.StringValue updated_by = 15 [(wix.api.format) = GUID, (wix.api.readOnly) = true];

  reserved 16;

  // The name of the test.
  google.protobuf.StringValue name = 17 [(wix.api.maxLength) = 200];

  // The creation date of the test. Read-only.
  google.protobuf.Timestamp created_at = 18 [(wix.api.readOnly) = true];

  // The last update date of the test. Read-only.
  google.protobuf.Timestamp updated_at = 19 [(wix.api.readOnly) = true];

  google.protobuf.StringValue wixer_id = 20 [(wix.api.maxLength) = 10];

  // The ab test id in Petri
  google.protobuf.StringValue ab_test_id = 21 [(wix.api.maxLength) = 60];

  // The test's ownership-tag
  google.protobuf.StringValue ownership_tag = 22 [(wix.api.maxLength) = 100];

  message Variant {
    // The variant id, A for the original text, the rest variants B, C...
    string key = 1 [(wix.api.maxLength) = 1];

    // The text the variant presents
    string text = 2 [(wix.api.maxLength) = 10000];

    // The persona voice describes the variant.
    wix.personalize.variants_generator.v1.Persona persona = 3;

    // Is the persona text was edited.
    google.protobuf.BoolValue custom = 4;
  }

  // test statuses
  enum TestStatus {
    UNKNOWN_TEST_STATUS = 0;
    NOT_STARTED = 1;
    ACTIVE = 2;
    ENDED = 3;
  }
}

service OfferTestService {
  option (wix.api.service_entity).message = "wix.dealerBackoffice.api.v1.OfferTest";
  option (wix.api.service_maturity) = ALPHA;
  option (wix.api.service_exposure) = INTERNAL;

  option (wix.api.event) = {
    topic: "dealer.offer-test-stopped-event"
    segment: USERS
    exposure: INTERNAL
    payload: "wix.dealerBackoffice.api.v1.OfferTestStoppedEvent"
  };

  rpc GetOfferTest (GetOfferTestRequest) returns (GetOfferTestResponse) {
    option (google.api.http).get = "/v1/dealer/backoffice/offer-tests/{test_id}";
    option (wix.api.required) = "GetOfferTestRequest.test_id";
    option (wix.api.permission).name = "DEALER_BACKOFFICE.READ_OFFER";
    option (wix.api.crud) = {
      method: GET_ITEM
      get_options: {
        id_field: "test_id",
        item_field: "offer_test"
      }
    };
  }

  rpc QueryOfferTests (QueryOfferTestsRequest) returns (QueryOfferTestsResponse) {
    option (google.api.http).post = "/v1/dealer/backoffice/offer-tests/query";
    option (wix.api.permission).name = "DEALER_BACKOFFICE.READ_OFFER";
  }

  // flynt-disable required-annotation-breaking-change
  rpc CreateOfferTest (CreateOfferTestRequest) returns (CreateOfferTestResponse) {
    option (google.api.http).post = "/v1/dealer/backoffice/offer-tests";
    option (wix.api.required) = "CreateOfferTestRequest.offer_test.offer_id";
    option (wix.api.required) = "CreateOfferTestRequest.offer_test.name";
    option (wix.api.required) = "CreateOfferTestRequest.offer_test.offer_schema_key";
    option (wix.api.required) = "CreateOfferTestRequest.offer_test.variants";
    option (wix.api.permission).name = "DEALER_BACKOFFICE.CREATE_OFFER";
    option (wix.api.sla).response_time_ms = 50000;
    option (wix.api.crud) = {
      method: CREATE
      create_options: {
        item_field: "offer_test"
      }
    };
    option (wix.api.emits) = {
      event_type: CREATED
    };
  }

  rpc UpdateOfferTest (UpdateOfferTestRequest) returns (UpdateOfferTestResponse) {
    option (google.api.http).patch = "/v1/dealer/backoffice/offer-tests/{offer_test.id}";
    option (wix.api.required) = "UpdateOfferTestRequest.offer_test.id";
    option (wix.api.permission).name = "DEALER_BACKOFFICE.UPDATE_OFFER";


    option (wix.http.infer_fieldmask) = {
        from: "offer_test"
        fieldmask: "field_mask"
      };
      option (wix.api.crud) = {
        method: UPDATE
        update_options: {
            item_field: "offer_test"
            fieldmask_field: "field_mask"
        }
      };
    option (wix.api.emits) = {
      event_type: UPDATED
    };
  }

  rpc StartOfferTest(StartOfferTestRequest) returns (StartOfferTestResponse) {
    option (wix.api.permission).name = "DEALER_BACKOFFICE.UPDATE_OFFER";
    option (google.api.http).post = "/v1/dealer/backoffice/offer-tests/{test_id}/start";
    option (wix.api.sla).response_time_ms = 50000;
    option (wix.api.required) = "StartOfferTestRequest.test_id";
  }

  rpc StopOfferTest(StopOfferTestRequest) returns (StopOfferTestResponse) {
    option (wix.api.permission).name = "DEALER_BACKOFFICE.UPDATE_OFFER";
    option (google.api.http).post = "/v1/dealer/backoffice/offer-tests/{test_id}/stop";
    option (wix.api.sla).response_time_ms = 50000;
    option (wix.api.required) = "StopOfferTestRequest.test_id";
  }

  rpc DeleteOfferTest (DeleteOfferTestRequest) returns (DeleteOfferTestResponse) {
    option (google.api.http).delete = "/v1/dealer/backoffice/offer-tests/{test_id}";
    option (wix.api.permission).name = "DEALER_BACKOFFICE.UPDATE_OFFER";
    option (wix.api.required) = "DeleteOfferTestRequest.test_id";
    option (wix.api.emits) = {
      event_type: DELETED
    };
    option (wix.api.crud) = {
      method: DELETE
      delete_options: {
        id_field: "test_id"
      }
    };
  }

  rpc SetTimeLeft (SetTimeLeftRequest) returns (SetTimeLeftResponse) {
    option (google.api.http).put = "/v1/dealer/backoffice/offer-tests/{test_id}/time-left";
    option (wix.api.required) = "SetTimeLeftRequest.test_id";
    option (wix.api.required) = "SetTimeLeftRequest.time_left_in_days";
    option (wix.api.permission).name = "DEALER_BACKOFFICE.UPDATE_OFFER";
  }
}

message GetOfferTestRequest {
  string test_id = 1 [(wix.api.format) = GUID];
}

message GetOfferTestResponse {
  OfferTest offer_test = 1;
}

// Supported queries:
//
// filter:
//  query: { filter: { offerId: '<uuid>'}}
//  query: { filter: { offerId: { $in: ['<uuid>', '<uuid>']}}}
//  query: { filter: { offerSchemaKey: 'key'}}
//  query: { filter: { offerSchemaKey: { $in: ['key1', 'key2']}}}
//  query: { filter: { optimizationTestId: '<uuid>'}}
//  query: { filter: { optimizationTestId: { $in: ['<uuid>', '<uuid>']}}}
//  query: { filter: { status: 'ACTIVE'}}
//  query: { filter: { status: { $in: ['ACTIVE', 'NOT_STARTED', 'ENDED']}}}

message QueryOfferTestsRequest {
  wix.dealerBackoffice.api.v1.upstream.wix.common.Query query = 1;
}

message QueryOfferTestsResponse {
  repeated OfferTest offer_tests = 1;
  wix.dealerBackoffice.api.v1.upstream.wix.common.PagingMetadata paging_metadata = 2;
}

message CreateOfferTestRequest {
  OfferTest offer_test = 1;
}

message CreateOfferTestResponse {
  OfferTest offer_test = 1;
}

message UpdateOfferTestRequest {
  OfferTest offer_test = 1;
  google.protobuf.FieldMask field_mask = 2;
}

message UpdateOfferTestResponse {
  OfferTest offer_test = 1;
}

message StartOfferTestRequest {
  string test_id = 1 [(wix.api.format) = GUID];
}

message StartOfferTestResponse {
  OfferTest offer_test = 1;
}

message StopOfferTestRequest {
  string test_id = 1 [(wix.api.format) = GUID];
}

message StopOfferTestResponse {
  OfferTest offer_test = 1;
}

message DeleteOfferTestRequest {
  string test_id = 1 [(wix.api.format) = GUID];
}

message DeleteOfferTestResponse {}

message OfferTestStoppedEvent {
  string offer_test_id = 1 [(wix.api.format) = GUID];
  string offer_id = 2 [(wix.api.format) = GUID];
}

message SetTimeLeftRequest {
  string test_id = 1 [(wix.api.format) = GUID];
  int32 time_left_in_days = 2;
}

message SetTimeLeftResponse {
  OfferTest offer_test = 1;
}
