// Copyright 2017 Google Inc.
//
// 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.firestore.v1beta1;

import "google/api/annotations.proto";
import "google/firestore/v1beta1/common.proto";
import "google/firestore/v1beta1/document.proto";
import "google/protobuf/timestamp.proto";

option csharp_namespace = "Google.Cloud.Firestore.V1Beta1";
option go_package = "google.golang.org/genproto/googleapis/firestore/v1beta1;firestore";
option java_multiple_files = true;
option java_outer_classname = "WriteProto";
option java_package = "com.google.firestore.v1beta1";
option objc_class_prefix = "GCFS";


// A write on a document.
message Write {
  // The operation to execute.
  oneof operation {
    // A document to write.
    Document update = 1;

    // A document name to delete. In the format:
    // `projects/{project_id}/databases/{database_id}/documents/{document_path}`.
    string delete = 2;

    // Applies a tranformation to a document.
    // At most one `transform` per document is allowed in a given request.
    // An `update` cannot follow a `transform` on the same document in a given
    // request.
    DocumentTransform transform = 6;
  }

  // The fields to update in this write.
  //
  // This field can be set only when the operation is `update`.
  // None of the field paths in the mask may contain a reserved name.
  // If the document exists on the server and has fields not referenced in the
  // mask, they are left unchanged.
  // Fields referenced in the mask, but not present in the input document, are
  // deleted from the document on the server.
  // The field paths in this mask must not contain a reserved field name.
  DocumentMask update_mask = 3;

  // An optional precondition on the document.
  //
  // The write will fail if this is set and not met by the target document.
  Precondition current_document = 4;
}

// A transformation of a document.
message DocumentTransform {
  // A transformation of a field of the document.
  message FieldTransform {
    // A value that is calculated by the server.
    enum ServerValue {
      // Unspecified. This value must not be used.
      SERVER_VALUE_UNSPECIFIED = 0;

      // The time at which the server processed the request.
      REQUEST_TIME = 1;
    }

    // The path of the field. See [Document.fields][google.firestore.v1beta1.Document.fields] for the field path syntax
    // reference.
    string field_path = 1;

    // The transformation to apply on the field.
    oneof transform_type {
      // Sets the field to the given server value.
      ServerValue set_to_server_value = 2;
    }
  }

  // The name of the document to transform.
  string document = 1;

  // The list of transformations to apply to the fields of the document, in
  // order.
  repeated FieldTransform field_transforms = 2;
}

// The result of applying a write.
message WriteResult {
  // The last update time of the document after applying the write. Not set
  // after a `delete`.
  //
  // If the write did not actually change the document, this will be the
  // previous update_time.
  google.protobuf.Timestamp update_time = 1;

  // The results of applying each [DocumentTransform.FieldTransform][google.firestore.v1beta1.DocumentTransform.FieldTransform], in the
  // same order.
  repeated Value transform_results = 2;
}

// A [Document][google.firestore.v1beta1.Document] has changed.
//
// May be the result of multiple [writes][google.firestore.v1beta1.Write], including deletes, that
// ultimately resulted in a new value for the [Document][google.firestore.v1beta1.Document].
//
// Multiple [DocumentChange][google.firestore.v1beta1.DocumentChange] messages may be returned for the same logical
// change, if multiple targets are affected.
message DocumentChange {
  // The new state of the [Document][google.firestore.v1beta1.Document].
  //
  // If `mask` is set, contains only fields that were updated or added.
  Document document = 1;

  // A set of target IDs of targets that match this document.
  repeated int32 target_ids = 5;

  // A set of target IDs for targets that no longer match this document.
  repeated int32 removed_target_ids = 6;
}

// A [Document][google.firestore.v1beta1.Document] has been deleted.
//
// May be the result of multiple [writes][google.firestore.v1beta1.Write], including updates, the
// last of which deleted the [Document][google.firestore.v1beta1.Document].
//
// Multiple [DocumentDelete][google.firestore.v1beta1.DocumentDelete] messages may be returned for the same logical
// delete, if multiple targets are affected.
message DocumentDelete {
  // The resource name of the [Document][google.firestore.v1beta1.Document] that was deleted.
  string document = 1;

  // A set of target IDs for targets that previously matched this entity.
  repeated int32 removed_target_ids = 6;

  // The read timestamp at which the delete was observed.
  //
  // Greater or equal to the `commit_time` of the delete.
  google.protobuf.Timestamp read_time = 4;
}

// A [Document][google.firestore.v1beta1.Document] has been removed from the view of the targets.
//
// Sent if the document is no longer relevant to a target and is out of view.
// Can be sent instead of a DocumentDelete or a DocumentChange if the server
// can not send the new value of the document.
//
// Multiple [DocumentRemove][google.firestore.v1beta1.DocumentRemove] messages may be returned for the same logical
// write or delete, if multiple targets are affected.
message DocumentRemove {
  // The resource name of the [Document][google.firestore.v1beta1.Document] that has gone out of view.
  string document = 1;

  // A set of target IDs for targets that previously matched this document.
  repeated int32 removed_target_ids = 2;

  // The read timestamp at which the remove was observed.
  //
  // Greater or equal to the `commit_time` of the change/delete/remove.
  google.protobuf.Timestamp read_time = 4;
}

// A digest of all the documents that match a given target.
message ExistenceFilter {
  // The target ID to which this filter applies.
  int32 target_id = 1;

  // The total count of documents that match [target_id][google.firestore.v1beta1.ExistenceFilter.target_id].
  //
  // If different from the count of documents in the client that match, the
  // client must manually determine which documents no longer match the target.
  int32 count = 2;
}
