// Copyright 2022 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.discoveryengine.v1alpha;

import "google/api/annotations.proto";
import "google/api/client.proto";
import "google/api/field_behavior.proto";
import "google/api/resource.proto";
import "google/longrunning/operations.proto";
import "google/protobuf/timestamp.proto";

option csharp_namespace = "Google.Cloud.DiscoveryEngine.V1Alpha";
option go_package = "cloud.google.com/go/discoveryengine/apiv1alpha/discoveryenginepb;discoveryenginepb";
option java_multiple_files = true;
option java_outer_classname = "SiteSearchEngineServiceProto";
option java_package = "com.google.cloud.discoveryengine.v1alpha";
option objc_class_prefix = "DISCOVERYENGINE";
option php_namespace = "Google\\Cloud\\DiscoveryEngine\\V1alpha";
option ruby_package = "Google::Cloud::DiscoveryEngine::V1alpha";

// Service for managing site search related resources.
service SiteSearchEngineService {
  option (google.api.default_host) = "discoveryengine.googleapis.com";
  option (google.api.oauth_scopes) =
      "https://www.googleapis.com/auth/cloud-platform";

  // Request on-demand recrawl for a list of URIs.
  rpc RecrawlUris(RecrawlUrisRequest) returns (google.longrunning.Operation) {
    option (google.api.http) = {
      post: "/v1alpha/{site_search_engine=projects/*/locations/*/dataStores/*/siteSearchEngine}:recrawlUris"
      body: "*"
      additional_bindings {
        post: "/v1alpha/{site_search_engine=projects/*/locations/*/collections/*/dataStores/*/siteSearchEngine}:recrawlUris"
        body: "*"
      }
    };
    option (google.longrunning.operation_info) = {
      response_type: "google.cloud.discoveryengine.v1alpha.RecrawlUrisResponse"
      metadata_type: "google.cloud.discoveryengine.v1alpha.RecrawlUrisMetadata"
    };
  }
}

// Request message for
// [SiteSearchEngineService.RecrawlUris][google.cloud.discoveryengine.v1alpha.SiteSearchEngineService.RecrawlUris]
// method.
message RecrawlUrisRequest {
  // Required. Full resource name of the
  // [SiteSearchEngine][google.cloud.discoveryengine.v1alpha.SiteSearchEngine],
  // such as
  // `projects/*/locations/*/collections/*/dataStores/*/siteSearchEngine`.
  string site_search_engine = 1 [
    (google.api.field_behavior) = REQUIRED,
    (google.api.resource_reference) = {
      type: "discoveryengine.googleapis.com/SiteSearchEngine"
    }
  ];

  // Required. List of URIs to crawl. At most 10K URIs are supported, otherwise
  // an INVALID_ARGUMENT error is thrown. Each URI should match at least one
  // [TargetSite][google.cloud.discoveryengine.v1alpha.TargetSite] in
  // `site_search_engine`.
  repeated string uris = 2 [(google.api.field_behavior) = REQUIRED];
}

// Response message for
// [SiteSearchEngineService.RecrawlUris][google.cloud.discoveryengine.v1alpha.SiteSearchEngineService.RecrawlUris]
// method.
message RecrawlUrisResponse {
  // Details about why a particular URI failed to be crawled. Each FailureInfo
  // contains one FailureReason per CorpusType.
  message FailureInfo {
    // Details about why crawling failed for a particular CorpusType, e.g.
    // DESKTOP and MOBILE crawling may fail for different reasons.
    message FailureReason {
      // CorpusType for the failed crawling operation.
      enum CorpusType {
        // Default value.
        CORPUS_TYPE_UNSPECIFIED = 0;

        // Denotes a crawling attempt for the desktop version of a page.
        DESKTOP = 1;

        // Denotes a crawling attempt for the mobile version of a page.
        MOBILE = 2;
      }

      // DESKTOP, MOBILE, or CORPUS_TYPE_UNSPECIFIED.
      CorpusType corpus_type = 1;

      // Reason why the URI was not crawled.
      string error_message = 2;
    }

    // URI that failed to be crawled.
    string uri = 1;

    // List of failure reasons by corpus type (e.g. desktop, mobile).
    repeated FailureReason failure_reasons = 2;
  }

  // Details for a sample of up to 10 `failed_uris`.
  repeated FailureInfo failure_samples = 1;

  // URIs that were not crawled before the LRO terminated.
  repeated string failed_uris = 2;
}

// Metadata related to the progress of the
// [SiteSearchEngineService.RecrawlUris][google.cloud.discoveryengine.v1alpha.SiteSearchEngineService.RecrawlUris]
// operation. This will be returned by the google.longrunning.Operation.metadata
// field.
message RecrawlUrisMetadata {
  // Operation create time.
  google.protobuf.Timestamp create_time = 1;

  // Operation last update time. If the operation is done, this is also the
  // finish time.
  google.protobuf.Timestamp update_time = 2;

  // Unique URIs in the request that don't match any TargetSite in the
  // DataStore, only match TargetSites that haven't been fully indexed, or match
  // a TargetSite with type EXCLUDE.
  repeated string invalid_uris = 3;

  // Total number of unique URIs in the request that are not in invalid_uris.
  int32 valid_uris_count = 4;

  // Total number of URIs that have been crawled so far.
  int32 success_count = 5;

  // Total number of URIs that have yet to be crawled.
  int32 pending_count = 6;

  // Total number of URIs that were rejected due to insufficient indexing
  // resources.
  int32 quota_exceeded_count = 7;
}
