// Copyright 2025 Google LLC
//
// Licensed 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 google.spanner.adapter.v1;

import "google/api/annotations.proto";
import "google/api/client.proto";
import "google/api/field_behavior.proto";
import "google/api/resource.proto";

option csharp_namespace = "Google.Cloud.Spanner.Adapter.V1";
option go_package = "cloud.google.com/go/spanner/adapter/apiv1/adapterpb;adapterpb";
option java_multiple_files = true;
option java_outer_classname = "AdapterProto";
option java_package = "com.google.spanner.adapter.v1";
option php_namespace = "Google\\Cloud\\Spanner\\Adapter\\V1";
option ruby_package = "Google::Cloud::Spanner::Adapter::V1";
option (google.api.resource_definition) = {
  type: "spanner.googleapis.com/Database"
  pattern: "projects/{project}/instances/{instance}/databases/{database}"
};

// Cloud Spanner Adapter API
//
// The Cloud Spanner Adapter service allows native drivers of supported database
// dialects to interact directly with Cloud Spanner by wrapping the underlying
// wire protocol used by the driver in a gRPC stream.
service Adapter {
  option (google.api.default_host) = "spanner.googleapis.com";
  option (google.api.oauth_scopes) =
      "https://www.googleapis.com/auth/cloud-platform,"
      "https://www.googleapis.com/auth/spanner.data";

  // Creates a new session to be used for requests made by the adapter.
  // A session identifies a specific incarnation of a database resource and is
  // meant to be reused across many `AdaptMessage` calls.
  rpc CreateSession(CreateSessionRequest) returns (Session) {
    option (google.api.http) = {
      post: "/v1/{parent=projects/*/instances/*/databases/*}/sessions:adapter"
      body: "session"
    };
    option (google.api.method_signature) = "parent,session";
  }

  // Handles a single message from the client and returns the result as a
  // stream. The server will interpret the message frame and respond with
  // message frames to the client.
  rpc AdaptMessage(AdaptMessageRequest) returns (stream AdaptMessageResponse) {
    option (google.api.http) = {
      post: "/v1/{name=projects/*/instances/*/databases/*/sessions/*}:adaptMessage"
      body: "*"
    };
  }
}

// Message sent by the client to the adapter.
message AdaptMessageRequest {
  // Required. The database session in which the adapter request is processed.
  string name = 1 [
    (google.api.field_behavior) = REQUIRED,
    (google.api.resource_reference) = { type: "spanner.googleapis.com/Session" }
  ];

  // Required. Identifier for the underlying wire protocol.
  string protocol = 2 [(google.api.field_behavior) = REQUIRED];

  // Optional. Uninterpreted bytes from the underlying wire protocol.
  bytes payload = 3 [(google.api.field_behavior) = OPTIONAL];

  // Optional. Opaque request state passed by the client to the server.
  map<string, string> attachments = 4 [(google.api.field_behavior) = OPTIONAL];
}

// Message sent by the adapter to the client.
message AdaptMessageResponse {
  // Optional. Uninterpreted bytes from the underlying wire protocol.
  bytes payload = 1 [(google.api.field_behavior) = OPTIONAL];

  // Optional. Opaque state updates to be applied by the client.
  map<string, string> state_updates = 2
      [(google.api.field_behavior) = OPTIONAL];
}

// A session in the Cloud Spanner Adapter API.
message Session {
  option (google.api.resource) = {
    type: "spanner.googleapis.com/Session"
    pattern: "projects/{project}/instances/{instance}/databases/{database}/sessions/{session}"
    plural: "sessions"
    singular: "session"
  };

  // Identifier. The name of the session. This is always system-assigned.
  string name = 1 [(google.api.field_behavior) = IDENTIFIER];
}

// The request for
// [CreateSessionRequest][Adapter.CreateSessionRequest].
message CreateSessionRequest {
  // Required. The database in which the new session is created.
  string parent = 1 [
    (google.api.field_behavior) = REQUIRED,
    (google.api.resource_reference) = {
      type: "spanner.googleapis.com/Database"
    }
  ];

  // Required. The session to create.
  Session session = 2 [(google.api.field_behavior) = REQUIRED];
}
