// Copyright 2015 The gRPC Authors
//
// 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";
// protoc --dart_out=grpc:lib/src/generated -Iprotos protos/mongo_store.proto

message QueryResponse {
    string data = 1;
}

//
message CreateDocumentRequest {
    string collection = 1;
    string payload = 2;
}

//
message InsertManyDocumentsRequest {
    string collection = 1;
    string payload = 2;
}

message Populate {
    /** space delimited path(s) to populate */
    string path = 1;
    /** optional fields to select */
    map<string, int32> select = 2;
    /** optional query conditions to match */
    string match = 3; // extended json query
    /** optional model to use for population */
    string collection = 4;
    /** optional query options like sort, limit, etc */
    string options = 5;
    /** deep populate */
    repeated Populate populate = 6;
}

//
message FindByIdRequest {
    string collection = 1;
    string id = 2;
    map<string, int32> select = 3;
    repeated Populate populate = 6;
}

//
message FindOneRequest {
    string collection = 1;
    string conditions = 2; // extended json query
    map<string, int32> select = 3;
    map<string, int32> sort = 4;
    uint32 skip = 5;
    repeated Populate populate = 6;
}

//
message FindRequest {
    string collection = 1;
    string conditions = 2; // extended json query
    map<string, int32> select = 3;
    map<string, int32> sort = 4;
    uint32 skip = 5;
    uint32 limit = 6;
    repeated Populate populate = 7;
}

//
message FindByIdAndUpdateRequest {
    string collection = 1;
    string id = 2;
    string update = 3;
    map<string, int32> select = 4;
    bool returnNew = 5;
    bool upsert = 6;
}

//
message FindOneAndUpdateRequest {
    string collection = 1;
    string conditions = 2; // extended json query
    string update = 3;
    map<string, int32> sort = 4;
    map<string, int32> select = 5;
    bool returnNew = 6;
    bool upsert = 7;
}

//
message UpdateManyRequest {
    string collection = 1;
    string conditions = 2; // extended json query
    string update = 3;
    bool safe = 4;
    bool upsert = 5;
    bool multi = 6;
}

//
message FindByIdAndDeleteRequest {
    string collection = 1;
    string id = 2;
    map<string, int32> select = 3;
}

//
message FindOneAndDeleteRequest {
    string collection = 1;
    string conditions = 2; // extended json query
    map<string, int32> sort = 3;
    map<string, int32> select = 4;
}

//
message DeleteManyRequest {
    string collection = 1;
    string conditions = 2; // extended json query
}

//
message CountDocumentsRequest {
    string collection = 1;
    string criteria = 2;
}

//
message EstimatedDocumentCountRequest {
    string collection = 1;
}

//
message ChangeStreamOptions {
    string fullDocument = 1;
    int32 maxAwaitTimeMS = 2;
    string resumeAfter = 3;
    string startAfter = 4;
    int32 batchSize = 5;
    string readPreference = 6;
}

//
message WatchRequest {
    string collection = 1;
    string pipeline = 2;
    ChangeStreamOptions options = 3;
}


service MongoStoreService {
    /*=== Insert section ======================================================================*/
    //
    rpc Create (CreateDocumentRequest) returns (QueryResponse);

    //
    rpc InsertMany (InsertManyDocumentsRequest) returns (QueryResponse);

    /*=== Find section ========================================================================*/
    //
    rpc FindById (FindByIdRequest) returns (QueryResponse);

    //
    rpc FindOne (FindOneRequest) returns (QueryResponse);

    //
    rpc Find (FindRequest) returns (QueryResponse);

    //
    rpc FindAndStreamResponse (FindRequest) returns (stream QueryResponse);

    //
    rpc FindLive (FindRequest) returns (stream QueryResponse);

    /*=== Find and update section ==============================================================*/
    //
    rpc FindByIdAndUpdate (FindByIdAndUpdateRequest) returns (QueryResponse);

    //
    rpc FindOneAndUpdate (FindOneAndUpdateRequest) returns (QueryResponse);

    //
    rpc UpdateMany (UpdateManyRequest) returns (QueryResponse);

    /*=== Find and delete section ==============================================================*/
    //
    rpc FindByIdAndDelete (FindByIdAndDeleteRequest) returns (QueryResponse);

    //
    rpc FindOneAndDelete (FindOneAndDeleteRequest) returns (QueryResponse);

    //
    rpc DeleteMany (DeleteManyRequest) returns (QueryResponse);

    /*=== count documents section ==============================================================*/
    //
    rpc CountDocuments (CountDocumentsRequest) returns (QueryResponse);

    //
    rpc EstimatedDocumentCount (EstimatedDocumentCountRequest) returns (QueryResponse);

    /*=== count documents section ==============================================================*/
    rpc Watch (WatchRequest) returns (stream QueryResponse);

}