// 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.common.v1;

import "google/protobuf/timestamp.proto";
import "validate/validate.proto";

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

enum Catalog {
  CATALOG_UNSPECIFIED = 0;
  CATALOG_STREAM = 1;
  CATALOG_MEASURE = 2;
  CATALOG_PROPERTY = 3;
  CATALOG_TRACE = 4;
}

// Metadata is for multi-tenant, multi-model use
message Metadata {
  // group contains a set of options, like retention policy, max
  string group = 1;
  // name of the entity
  string name = 2;
  // id is the unique identifier of the entity
  // if id is not set, the system will generate a unique id
  uint32 id = 3;
  // readonly. create_revision is the revision of last creation on this key.
  int64 create_revision = 4;
  // readonly. mod_revision is the revision of last modification on this key.
  int64 mod_revision = 5;
}

// IntervalRule is a structured duration
message IntervalRule {
  enum Unit {
    UNIT_UNSPECIFIED = 0;
    UNIT_HOUR = 1;
    UNIT_DAY = 2;
  }
  // unit can only be UNIT_HOUR or UNIT_DAY
  Unit unit = 1 [(validate.rules).enum.defined_only = true];
  uint32 num = 2 [(validate.rules).uint32.gt = 0];
}

message LifecycleStage {
  // The stage name (e.g., "warm", "cold").
  // This should be a non-empty string.
  string name = 1 [(validate.rules).string.min_len = 1];

  // Number of shards allocated for this stage.
  // Must be greater than zero.
  uint32 shard_num = 2 [(validate.rules).uint32.gt = 0];

  // Defines the interval for data segmentation in this stage.
  // This is a required field and uses the IntervalRule structure.
  IntervalRule segment_interval = 3 [(validate.rules).message.required = true];

  // Specifies the time-to-live for data in this stage before moving to the next.
  // This is also a required field using the IntervalRule structure.
  IntervalRule ttl = 4 [(validate.rules).message.required = true];

  // Node selector specifying target nodes for this stage.
  // Optional; if provided, it must be a non-empty string.
  string node_selector = 5 [(validate.rules).string.min_len = 1];

  // Indicates whether segments that are no longer live should be closed.
  bool close = 6;

  // replicas is the number of replicas for this stage.
  // This is an optional field and defaults to 0.
  // A value of 0 means no replicas, while a value of 1 means one primary shard and one replica.
  // Higher values indicate more replicas.
  uint32 replicas = 7;
}

message ResourceOpts {
  // shard_num is the number of shards
  uint32 shard_num = 1 [(validate.rules).uint32.gt = 0];
  // segment_interval indicates the length of a segment
  IntervalRule segment_interval = 2;
  // ttl indicates time to live, how long the data will be cached
  IntervalRule ttl = 3;
  // stages defines the ordered lifecycle stages. Data progresses through these stages sequentially.
  repeated LifecycleStage stages = 4;
  // default_stages is the name of the default stage
  repeated string default_stages = 5;
  // replicas is the number of replicas. This is used to ensure high availability and fault tolerance.
  // This is an optional field and defaults to 0.
  // A value of 0 means no replicas, while a value of 1 means one primary shard and one replica.
  // Higher values indicate more replicas.
  uint32 replicas = 6;
}

// Group is an internal object for Group management
message Group {
  // metadata define the group's identity
  common.v1.Metadata metadata = 1 [(validate.rules).message.required = true];
  // catalog denotes which type of data the group contains
  common.v1.Catalog catalog = 2;
  // resourceOpts indicates the structure of the underlying kv storage
  ResourceOpts resource_opts = 3;
  // updated_at indicates when resources of the group are updated
  google.protobuf.Timestamp updated_at = 4;
}
