// 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/property/v1/property.proto";

option go_package = "github.com/apache/skywalking-banyandb/api/proto/banyandb/property/v1";
option java_package = "org.apache.skywalking.banyandb.property.v1";

message TreeSlotSHA {
  int32 slot = 1;
  string value = 2;
}

message TreeLeafNode {
  // slot_index is the index of the slot in the tree.
  int32 slot_index = 1;
  // if the slot is empty, means the server side don't have the slot.
  bool exists = 2;
  // if the slot and entity exists, the SHA value of the entity.
  string entity = 3;
  string sha = 4;
}

message TreeRoot {
  string group = 1;
  uint32 shard_id = 2;
  string root_sha = 3;
}

message TreeSlots {
  repeated TreeSlotSHA slot_sha = 1;
}

message PropertyMissing {
  string entity = 1;
}

message RootCompare {
  bool tree_found = 1;
  bool root_sha_match = 2;
}

message DifferTreeSummary {
  // if the nodes is empty, mean the server side don't have more tree leaf nodes to send.
  repeated TreeLeafNode nodes = 2;
}

message PropertySync {
  bytes id = 1;
  banyandb.property.v1.Property property = 2;
  int64 delete_time = 3;
}

enum PropertySyncFromType {
  PROPERTY_SYNC_FROM_TYPE_UNSPECIFIED = 0;
  PROPERTY_SYNC_FROM_TYPE_MISSING = 1; // client missing but server existing
  PROPERTY_SYNC_FROM_TYPE_SYNC = 2; // client existing but server missing or SHA value mismatches
}

message PropertySyncWithFrom {
  PropertySyncFromType from = 1;
  PropertySync property = 2;
}

message WaitNextDifferData {}

message RepairRequest {
  oneof data {
    // compare stage
    TreeRoot tree_root = 1;
    TreeSlots tree_slots = 2;
    // repair stage
    // case 1: client missing but server existing
    PropertyMissing property_missing = 3;
    // case 2: client existing but server missing
    // case 3: SHA value mismatches
    PropertySync property_sync = 4;
    // wait next differ tree summary for process
    WaitNextDifferData wait_next_differ = 5;
  }
}

message RepairResponse {
  oneof data {
    // compare stage
    RootCompare root_compare = 1;
    DifferTreeSummary differ_tree_summary = 2;
    // repair stage
    // case 1: return from PropertyMissing
    // case 3: return if the client is older
    PropertySyncWithFrom property_sync = 3;
  }
}

service RepairService {
  rpc Repair(stream RepairRequest) returns (stream RepairResponse) {}
}
