syntax = "proto3";

// If you make any changes make sure you run: make regenerate-proto

package gitlab.agent.agent_tracker.rpc;

option go_package = "gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v18/internal/module/agent_tracker/rpc";

import "google/protobuf/timestamp.proto";
import "pkg/entity/agentk/agentk.proto";
import "buf/validate/validate.proto";

message GetConnectedAgentksByAgentIDsRequest {
  repeated int64 agent_ids = 1;
}

message GetConnectedAgentksByAgentIDsResponse {
  repeated ConnectedAgentk agents = 1 [json_name = "agents"];
}

message CountAgentsByAgentVersionsRequest {
  int32 agent_type = 1 [
    (buf.validate.field).int32.in = 0, // agentk
    (buf.validate.field).int32.in = 1 // agentw
  ];
}

message CountAgentsByAgentVersionsResponse {
  map<string, int64> agent_versions = 1 [json_name = "agent_versions"];
}

// ConnectedAgentk contains information about a connected agentk.
message ConnectedAgentk {
  // Information about the agent sent by the agent.
  entity.agentk.Meta agent_meta = 1 [json_name = "agent_meta", (buf.validate.field).required = true];
  // When the connection was established.
  google.protobuf.Timestamp connected_at = 2 [json_name = "connected_at", (buf.validate.field).timestamp.gt = {}];
  // Uniquely identifies a particular agentk->kas connection.
  // Randomly generated when an agent connects.
  int64 connection_id = 3 [json_name = "connection_id"];
  // GitLab-wide unique id of the agent.
  int64 agent_id = 4 [json_name = "agent_id"];
  // Id of the configuration project.
  int64 project_id = 5 [json_name = "project_id"];
  // Warnings for this agent
  repeated AgentWarning warnings = 6 [json_name = "warnings"];
}

message AgentWarning {
  oneof warning {
    option (buf.validate.oneof).required = true;

    AgentWarningVersion version = 1 [json_name = "version"];
  }
}

message AgentWarningVersion {
  string message = 1 [json_name = "message", (buf.validate.field).string.min_bytes = 1];
  string type = 2 [
    json_name = "type",
    (buf.validate.field).string.in = "UNKNOWN",
    (buf.validate.field).string.in = "OUTDATED",
    (buf.validate.field).string.in = "TOO_NEW"
  ];
}

service AgentTracker {
  // Get connected agentks for a list of agent ids.
  rpc GetConnectedAgentksByAgentIDs (GetConnectedAgentksByAgentIDsRequest) returns (GetConnectedAgentksByAgentIDsResponse) {
    option idempotency_level = NO_SIDE_EFFECTS;
  }

  // Count agents by agent versions for a specific agent type.
  rpc CountAgentsByAgentVersions (CountAgentsByAgentVersionsRequest) returns (CountAgentsByAgentVersionsResponse) {
    option idempotency_level = NO_SIDE_EFFECTS;
  }
}
