syntax = "proto3";

package pdpb;

// TSO Service
service Tso {
  rpc GetTimestamp(TsoRequest) returns (TsoResponse);
}

message TsoRequest {
  uint32 count = 1;
}

message TsoResponse {
  uint64 timestamp = 1; // Physical << 16 | Logical
  uint32 count = 2;
}

// Cluster Management Service
service Pd {
  // Store Registration
  rpc RegisterStore(RegisterStoreRequest) returns (RegisterStoreResponse);
  
  // Region Discovery
  rpc GetRegion(GetRegionRequest) returns (GetRegionResponse);
  // Region Listing (bootstrap/refresh)
  rpc ListRegions(ListRegionsRequest) returns (ListRegionsResponse);
}

message RegisterStoreRequest {
  string addr = 1; // e.g., "127.0.0.1:50051"
  uint64 store_id = 2; // Optional requested store ID (0 = auto-assign)
}

message RegisterStoreResponse {
  uint64 store_id = 1;
  uint64 cluster_id = 2; // Verify cluster match
}

message GetRegionRequest {
  bytes key = 1;
}

message GetRegionResponse {
  Region region = 1;
  Store leader = 2;
}

// Move directives (simplified): add replica to target store, then remove source.
message MoveRegionRequest {
  uint64 region_id = 1;
  uint64 from_store = 2;
  uint64 to_store = 3;
}

message MoveRegionResponse {
  bool accepted = 1;
}

message ListRegionsRequest {}

message ListRegionsResponse {
  repeated Region regions = 1;
  repeated Store stores = 2;
}

message Region {
  uint64 id = 1;
  bytes start_key = 2;
  bytes end_key = 3; // empty = infinity
  repeated uint64 peers = 4;
  uint64 leader_id = 5;
  // In future: repeated Peer peers = 4;
}

message Store {
  uint64 id = 1;
  string addr = 2;
}
