// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

package google.cloud.retail.v2alpha;

import "google/api/annotations.proto";
import "google/api/client.proto";
import "google/api/field_behavior.proto";
import "google/api/httpbody.proto";
import "google/cloud/retail/v2alpha/export_config.proto";
import "google/cloud/retail/v2alpha/import_config.proto";
import "google/cloud/retail/v2alpha/purge_config.proto";
import "google/cloud/retail/v2alpha/user_event.proto";
import "google/longrunning/operations.proto";

option csharp_namespace = "Google.Cloud.Retail.V2Alpha";
option go_package = "google.golang.org/genproto/googleapis/cloud/retail/v2alpha;retail";
option java_multiple_files = true;
option java_outer_classname = "UserEventServiceProto";
option java_package = "com.google.cloud.retail.v2alpha";
option objc_class_prefix = "RETAIL";
option php_namespace = "Google\\Cloud\\Retail\\V2alpha";
option ruby_package = "Google::Cloud::Retail::V2alpha";

// Service for ingesting end user actions on the customer website.
service UserEventService {
  option (google.api.default_host) = "retail.googleapis.com";
  option (google.api.oauth_scopes) =
      "https://www.googleapis.com/auth/cloud-platform";

  // Writes a single user event.
  rpc WriteUserEvent(WriteUserEventRequest) returns (UserEvent) {
    option (google.api.http) = {
      post: "/v2alpha/{parent=projects/*/locations/*/catalogs/*}/userEvents:write"
      body: "user_event"
    };
  }

  // Writes a single user event from the browser. This uses a GET request to
  // due to browser restriction of POST-ing to a 3rd party domain.
  //
  // This method is used only by the Recommendations AI JavaScript pixel and
  // Google Tag Manager. Users should not call this method directly.
  rpc CollectUserEvent(CollectUserEventRequest) returns (google.api.HttpBody) {
    option (google.api.http) = {
      get: "/v2alpha/{parent=projects/*/locations/*/catalogs/*}/userEvents:collect"
    };
  }

  // Deletes permanently all user events specified by the filter provided.
  // Depending on the number of events specified by the filter, this operation
  // could take hours or days to complete. To test a filter, use the list
  // command first.
  rpc PurgeUserEvents(PurgeUserEventsRequest)
      returns (google.longrunning.Operation) {
    option (google.api.http) = {
      post: "/v2alpha/{parent=projects/*/locations/*/catalogs/*}/userEvents:purge"
      body: "*"
    };
    option (google.longrunning.operation_info) = {
      response_type: "google.cloud.retail.v2alpha.PurgeUserEventsResponse"
      metadata_type: "google.cloud.retail.v2alpha.PurgeMetadata"
    };
  }

  // Bulk import of User events. Request processing might be
  // synchronous. Events that already exist are skipped.
  // Use this method for backfilling historical user events.
  //
  // Operation.response is of type ImportResponse. Note that it is
  // possible for a subset of the items to be successfully inserted.
  // Operation.metadata is of type ImportMetadata.
  rpc ImportUserEvents(ImportUserEventsRequest)
      returns (google.longrunning.Operation) {
    option (google.api.http) = {
      post: "/v2alpha/{parent=projects/*/locations/*/catalogs/*}/userEvents:import"
      body: "*"
    };
    option (google.longrunning.operation_info) = {
      response_type: "google.cloud.retail.v2alpha.ImportUserEventsResponse"
      metadata_type: "google.cloud.retail.v2alpha.ImportMetadata"
    };
  }

  // Export of user events.
  //
  // Operation.response is of type ExportResponse.
  // Operation.metadata is of type ExportMetadata.
  rpc ExportUserEvents(ExportUserEventsRequest)
      returns (google.longrunning.Operation) {
    option (google.api.http) = {
      post: "/v2alpha/{parent=projects/*/locations/*/catalogs/*}/userEvents:export"
      body: "*"
    };
    option (google.longrunning.operation_info) = {
      response_type: "google.cloud.retail.v2alpha.ExportUserEventsResponse"
      metadata_type: "google.cloud.retail.v2alpha.ExportMetadata"
    };
  }

  // Triggers a user event rejoin operation with latest catalog data. Events
  // will not be annotated with detailed catalog information if catalog item is
  // missing at the time the user event is ingested, and these events are stored
  // as unjoined events with a limited usage on training and serving. This API
  // can be used to trigger a 'join' operation on specified events with latest
  // version of catalog items. It can also be used to correct events joined with
  // wrong catalog items.
  rpc RejoinUserEvents(RejoinUserEventsRequest)
      returns (google.longrunning.Operation) {
    option (google.api.http) = {
      post: "/v2alpha/{parent=projects/*/locations/*/catalogs/*}/userEvents:rejoin"
      body: "*"
    };
    option (google.longrunning.operation_info) = {
      response_type: "RejoinUserEventsResponse"
      metadata_type: "RejoinUserEventsMetadata"
    };
  }
}

// Request message for WriteUserEvent method.
message WriteUserEventRequest {
  // Required. The parent catalog resource name, such as
  // "projects/1234/locations/global/catalogs/default_catalog".
  string parent = 1 [(google.api.field_behavior) = REQUIRED];

  // Required. User event to write.
  UserEvent user_event = 2 [(google.api.field_behavior) = REQUIRED];
}

// Request message for CollectUserEvent method.
message CollectUserEventRequest {
  // Required. The parent catalog name, such as
  // "projects/1234/locations/global/catalogs/default_catalog".
  string parent = 1 [(google.api.field_behavior) = REQUIRED];

  // Required. URL encoded UserEvent proto with a length limit of 2,000,000
  // characters.
  string user_event = 2 [(google.api.field_behavior) = REQUIRED];

  // The url including cgi-parameters but excluding the hash fragment with a
  // length limit of 5,000 characters. This is often more useful than the
  // referer url, because many browsers only send the domain for 3rd party
  // requests.
  string uri = 3;

  // The event timestamp in milliseconds. This prevents browser caching of
  // otherwise identical get requests. The name is abbreviated to reduce the
  // payload bytes.
  int64 ets = 4;
}

// Request message for CatalogRejoin method.
message RejoinUserEventsRequest {
  // The scope of events to be rejoined with latest catalog. If the rejoining
  // aims at reducing number of unjoined events, set UserEventRejoinScope to
  // UNJOINED_EVENTS. If the rejoining aims at correcting catalog information
  // in joined_events, set UserEventRejoinScope to JOINED_EVENTS. If all events
  // needs to be rejoined, set UserEventRejoinScope to
  // USER_EVENT_REJOIN_SCOPE_UNSPECIFIED.
  enum UserEventRejoinScope {
    // Rejoin catalogs with all events including both joined events and
    // unjoined events.
    USER_EVENT_REJOIN_SCOPE_UNSPECIFIED = 0;

    // Only rejoin catalogs with joined events.
    JOINED_EVENTS = 1;

    // Only rejoin catalogs with unjoined events.
    UNJOINED_EVENTS = 2;
  }

  // Required. Full resource name of user event, such as
  // "projects/*/locations/*/catalogs/default_catalog".
  string parent = 1 [(google.api.field_behavior) = REQUIRED];

  // Required. The type of the catalog rejoin to define the scope and range of
  // the user events to be rejoined with catalog items.
  UserEventRejoinScope user_event_rejoin_scope = 2
      [(google.api.field_behavior) = REQUIRED];
}

// Response message for RejoinUserEvents method.
message RejoinUserEventsResponse {
  // Number of user events that were joined with latest catalog items.
  int64 rejoined_user_events_count = 1;
}

// Metadata for RejoinUserEvents method.
message RejoinUserEventsMetadata {}
