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

import "banyandb/common/v1/common.proto";
import "banyandb/database/v1/database.proto";
import "banyandb/database/v1/rpc.proto";
import "google/protobuf/timestamp.proto";

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

service FODCService {
  // Bi-directional stream for agent registration and heartbeat
  rpc RegisterAgent(stream RegisterAgentRequest) returns (stream RegisterAgentResponse);

  // Bi-directional stream for metrics
  // Agent sends StreamMetricsRequest (metrics data), Proxy sends StreamMetricsResponse (metrics requests)
  rpc StreamMetrics(stream StreamMetricsRequest) returns (stream StreamMetricsResponse);

  // Bi-directional stream for cluster topology
  rpc StreamClusterTopology(stream StreamClusterTopologyRequest) returns (stream StreamClusterTopologyResponse);

  // Bi-directional stream for lifecycle data
  rpc StreamLifecycle(stream StreamLifecycleRequest) returns (stream StreamLifecycleResponse);
}

message RegisterAgentRequest {
  string node_role = 1;
  map<string, string> labels = 2;
  string pod_name = 4;
  repeated string container_names = 5;
}

message RegisterAgentResponse {
  bool success = 1;
  string message = 2;
  int64 heartbeat_interval_seconds = 3;
  string agent_id = 4;
}

message StreamMetricsRequest {
  repeated Metric metrics = 1;
  google.protobuf.Timestamp timestamp = 2;
}

message Metric {
  string name = 1;
  map<string, string> labels = 2;
  double value = 3;
  string description = 4;
  google.protobuf.Timestamp timestamp = 5;
}

message StreamMetricsResponse {
  google.protobuf.Timestamp start_time = 1; // Optional start time for time window
  google.protobuf.Timestamp end_time = 2; // Optional end time for time window
}

message Call {
  string id = 1;
  string target = 2;
  string source = 3;
}

message Topology {
  repeated banyandb.database.v1.Node nodes = 1;
  repeated Call calls = 2;
}

message StreamClusterTopologyRequest {
  google.protobuf.Timestamp timestamp = 1;
  Topology topology = 2;
}

message StreamClusterTopologyResponse {
  // Request topology from agent
  bool request_topology = 1;
}

message StreamLifecycleRequest {
  google.protobuf.Timestamp timestamp = 1;
  string pod_name = 2;
  LifecycleData lifecycle_data = 3;
}

message StreamLifecycleResponse {
  bool request_lifecycle = 1;
}

message LifecycleData {
  repeated LifecycleReport reports = 1;
  repeated GroupLifecycleInfo groups = 2;
}

message GroupLifecycleInfo {
  string name = 1;
  string catalog = 2;
  banyandb.common.v1.ResourceOpts resource_opts = 3;
  repeated banyandb.database.v1.DataInfo data_info = 4;
}

message LifecycleReport {
  string filename = 1;
  string report_json = 2;
}

// GroupLifecycleService provides group lifecycle information from liaison nodes.
// Registered on the internal gRPC server so FODC agent can access it.
service GroupLifecycleService {
  // InspectAll lists all groups and returns their full lifecycle info in one call.
  rpc InspectAll(InspectAllRequest) returns (InspectAllResponse);
}

message InspectAllRequest {}

message InspectAllResponse {
  repeated GroupLifecycleInfo groups = 1;
}
