syntax = "proto3";

package obscura.v1;

option java_package = "xyz.obscura.server.contracts";
option java_outer_classname = "ObscuraProtocol";

// ==============================================================================
// Gateway Protocol (WebSocket)
// Endpoint: /v1/gateway
// ==============================================================================

// The top-level frame exchanged over the WebSocket connection.
message WebSocketFrame {
  oneof payload {
    // Server -> Client: A new message has arrived.
    Envelope envelope = 10;

    // Client -> Server: Acknowledge receipt of a message (Triggers DB deletion).
    AckMessage ack = 11;

    // Server -> Client: Notification that one-time pre-keys are running low.
    PreKeyStatus pre_key_status = 12;
  }
}

message PreKeyStatus {
  int32 one_time_pre_key_count = 1;
  int32 min_threshold = 2; // The server's configured threshold
}

// Represents a message waiting in the queue for the connected device.
// The client MUST reply with an `AckMessage` containing this message's ID.
message Envelope {
  string id = 1;            // Unique UUID of the message in the server queue.
  string source_user_id = 2;// UUID of the sender (Unencrypted metadata).
  uint64 timestamp = 3;     // Server-generated timestamp (ms) when the message was received.
  EncryptedMessage message = 4;
}

message EncryptedMessage {
  enum Type {
    TYPE_UNSPECIFIED = 0;
    TYPE_PREKEY_MESSAGE = 1; // Initial handshake
    TYPE_ENCRYPTED_MESSAGE = 2; // Standard encrypted message
  }
  Type type = 1;
  bytes content = 2; // The actual ciphertext
}

// Sent by Client to confirm processing. Server hard-deletes the message upon receipt.
message AckMessage {
  string message_id = 1;    // The UUID from the Envelope.
}
