// Licensed to Apache Software Foundation (ASF) under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Apache Software Foundation (ASF) licenses this file to you 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 banyandb.property.v1;

import "banyandb/common/v1/common.proto";
import "banyandb/common/v1/trace.proto";
import "banyandb/model/v1/common.proto";
import "banyandb/model/v1/query.proto";
import "banyandb/property/v1/property.proto";
import "google/api/annotations.proto";
import "protoc-gen-openapiv2/options/annotations.proto";
import "validate/validate.proto";

option go_package = "github.com/apache/skywalking-banyandb/api/proto/banyandb/property/v1";
option java_package = "org.apache.skywalking.banyandb.property.v1";
option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_swagger) = {base_path: "/api"};

message ApplyRequest {
  banyandb.property.v1.Property property = 1 [(validate.rules).message.required = true];
  enum Strategy {
    STRATEGY_UNSPECIFIED = 0;
    STRATEGY_MERGE = 1;
    STRATEGY_REPLACE = 2;
  }
  // strategy indicates how to update a property. It defaults to STRATEGY_MERGE
  Strategy strategy = 2;
}

message ApplyResponse {
  // created indicates whether the property existed.
  // True: the property is absent. False: the property existed.
  bool created = 1;
  uint32 tags_num = 2;
}

message DeleteRequest {
  // groups indicate where the data points are stored.
  string group = 1 [(validate.rules).string.min_len = 1];
  // name is the identity of a property.
  string name = 2 [(validate.rules).string.min_len = 1];
  // id is the identity of item in the property.
  string id = 3;
}

message DeleteResponse {
  bool deleted = 1;
}

message QueryOrder {
  // tag_name is the name of the tag to be ordered.
  string tag_name = 1;
  // order_by is given to specify the sort for a tag.
  model.v1.Sort sort = 12;
}

// QueryRequest is the request contract for query.
message QueryRequest {
  // groups indicate where the data points are stored.
  repeated string groups = 1 [(validate.rules).repeated.min_items = 1];
  // name is created when it receives the first property
  string name = 2;
  // ids is the identities of properties
  repeated string ids = 3;
  // criteria is used to filter properties based on tags
  model.v1.Criteria criteria = 4;
  // tag_projection can be used to select tags of the data points in the response
  repeated string tag_projection = 5;
  uint32 limit = 6;
  // trace is used to enable trace for the query
  bool trace = 7;
  // order_by is given to specify the sort for a tag.
  QueryOrder order_by = 8;
}

// QueryResponse is the response for a query to the Query module.
message QueryResponse {
  // properties are the actual data returned
  repeated banyandb.property.v1.Property properties = 1;
  // trace contains the trace information of the query when trace is enabled
  common.v1.Trace trace = 2;
}

service PropertyService {
  // Apply creates a property if it's absent, or update a existed one based on a strategy.
  rpc Apply(ApplyRequest) returns (ApplyResponse) {
    option (google.api.http) = {
      put: "/v1/property/data/{property.metadata.group}/{property.metadata.name}/{property.id}"
      body: "*"
    };
  }

  rpc Delete(DeleteRequest) returns (DeleteResponse) {
    option (google.api.http) = {delete: "/v1/property/data/{group}/{name}/{id}"};
  }

  rpc Query(QueryRequest) returns (QueryResponse) {
    option (google.api.http) = {
      post: "/v1/property/data/query"
      body: "*"
    };
  }
}

message InternalUpdateRequest {
  bytes id = 1;
  uint64 shard_id = 2;
  banyandb.property.v1.Property property = 3;
}

message InternalDeleteRequest {
  repeated bytes ids = 1;
}

message InternalQueryResponse {
  repeated bytes sources = 1;
  common.v1.Trace trace = 2;
  // deletes indicates the property is deleted timestamps,
  // it's mapping to the sources in the same order
  // if the value is 0, it means the property is not deleted
  repeated int64 deletes = 3;
  // sorted_values contains pre-extracted sort values from shard searches,
  // mapping to sources in the same order for optimized sorting at liaison layer
  repeated bytes sorted_values = 4;
}

message InternalRepairRequest {
  uint64 shard_id = 1;
  bytes id = 2;
  banyandb.property.v1.Property property = 3;
  int64 delete_time = 4;
}

message InternalRepairResponse {}
