syntax = "proto3";

package apigateway.v1;

// ============================================================================
// Gateway Auth Service
// ============================================================================

service GatewayAuthService {
  rpc Authorize(AuthorizeRequest) returns (AuthorizeResponse);
}

message Subject {
  string subject_id = 1;
  string org_id = 2;
  string project_id = 3;
  repeated string roles = 4;
  repeated string scopes = 5;
}

message AuthorizeRequest {
  string request_id = 1;
  string token = 2;
  string method = 3;
  string path = 4;
  string raw_query = 5;
  map<string, string> headers = 6;
  string client_ip = 7;
  string route_name = 8;
}

message AuthorizeResponse {
  bool allow = 1;
  string reason = 2;
  Subject subject = 3;
  map<string, string> headers = 4;
  uint32 ttl_seconds = 5;
}

// ============================================================================
// Gateway Credit Service
// ============================================================================

service GatewayCreditService {
  rpc Reserve(CreditReserveRequest) returns (CreditReserveResponse);
  rpc Commit(CreditCommitRequest) returns (CreditCommitResponse);
  rpc Rollback(CreditRollbackRequest) returns (CreditRollbackResponse);
}

message CreditReserveRequest {
  string request_id = 1;
  string subject_id = 2;
  string org_id = 3;
  string project_id = 4;
  string route_name = 5;
  string method = 6;
  string path = 7;
  string raw_query = 8;
  uint64 units = 9;
  map<string, string> attributes = 10;
}

message CreditReserveResponse {
  bool allow = 1;
  string reservation_id = 2;
  string reason = 3;
  uint64 remaining = 4;
}

message CreditCommitRequest {
  string reservation_id = 1;
  uint64 units = 2;
}

message CreditCommitResponse {
  bool success = 1;
  string reason = 2;
}

message CreditRollbackRequest {
  string reservation_id = 1;
}

message CreditRollbackResponse {
  bool success = 1;
  string reason = 2;
}
