// 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.v2;

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/v2/import_config.proto";
import "google/cloud/retail/v2/purge_config.proto";
import "google/cloud/retail/v2/user_event.proto";
import "google/longrunning/operations.proto";

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

// 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: "/v2/{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 Retail API 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: "/v2/{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: "/v2/{parent=projects/*/locations/*/catalogs/*}/userEvents:purge"
      body: "*"
    };
    option (google.longrunning.operation_info) = {
      response_type: "google.cloud.retail.v2.PurgeUserEventsResponse"
      metadata_type: "google.cloud.retail.v2.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: "/v2/{parent=projects/*/locations/*/catalogs/*}/userEvents:import"
      body: "*"
    };
    option (google.longrunning.operation_info) = {
      response_type: "google.cloud.retail.v2.ImportUserEventsResponse"
      metadata_type: "google.cloud.retail.v2.ImportMetadata"
    };
  }

  // Triggers a user event rejoin operation with latest product catalog. Events
  // will not be annotated with detailed product information if product is
  // missing from the catalog 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 product catalog. It can also be used to
  // correct events joined with wrong product catalog.
  rpc RejoinUserEvents(RejoinUserEventsRequest)
      returns (google.longrunning.Operation) {
    option (google.api.http) = {
      post: "/v2/{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 RejoinUserEvents method.
message RejoinUserEventsRequest {
  // The scope of user events to be rejoined with the latest product catalog.
  // If the rejoining aims at reducing number of unjoined events, set
  // UserEventRejoinScope to UNJOINED_EVENTS.
  // If the rejoining aims at correcting product 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 all events with the latest product catalog, including both joined
    // events and unjoined events.
    USER_EVENT_REJOIN_SCOPE_UNSPECIFIED = 0;

    // Only rejoin joined events with the latest product catalog.
    JOINED_EVENTS = 1;

    // Only rejoin unjoined events with the latest product catalog.
    UNJOINED_EVENTS = 2;
  }

  // Required. The parent catalog resource name, such as
  // `projects/1234/locations/global/catalogs/default_catalog`.
  string parent = 1 [(google.api.field_behavior) = REQUIRED];

  // The type of the user event rejoin to define the scope and range of the user
  // events to be rejoined with the latest product catalog. Defaults to
  // USER_EVENT_REJOIN_SCOPE_UNSPECIFIED if this field is not set, or set to an
  // invalid integer value.
  UserEventRejoinScope user_event_rejoin_scope = 2;
}

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

// Metadata for RejoinUserEvents method.
message RejoinUserEventsMetadata {}
