syntax = "proto3";

package berty.account.v1;

import "pushtypes/bertypushtypes.proto";
import "protocoltypes.proto";

option go_package = "berty.tech/berty/go/pkg/accounttypes";

service AccountService {
  /// Account

  // OpenAccount starts a Berty node.
  rpc OpenAccount (OpenAccount.Request) returns (OpenAccount.Reply);

  // OpenAccountWithProgress is similar to OpenAccount, but also streams the progress.
  rpc OpenAccountWithProgress (OpenAccountWithProgress.Request) returns (stream OpenAccountWithProgress.Reply);

  // CloseAccount closes the currently opened account.
  rpc CloseAccount (CloseAccount.Request) returns (CloseAccount.Reply);

  // CloseAccountWithProgress is similar to CloseAccount, but also streams the progress.
  rpc CloseAccountWithProgress (CloseAccountWithProgress.Request) returns (stream CloseAccountWithProgress.Reply);

  // ListAccounts retrieves a list of local accounts.
  rpc ListAccounts (ListAccounts.Request) returns (ListAccounts.Reply);

  // DeleteAccount deletes an account.
  rpc DeleteAccount (DeleteAccount.Request) returns (DeleteAccount.Reply);

  // ImportAccount imports existing data.
  rpc ImportAccount (ImportAccount.Request) returns (ImportAccount.Reply);

  // ImportAccountWithProgress is similar to ImportAccount, but also streams the progress.
  rpc ImportAccountWithProgress (ImportAccountWithProgress.Request) returns (stream ImportAccountWithProgress.Reply);

  // CreateAccount creates a new account.
  rpc CreateAccount (CreateAccount.Request) returns (CreateAccount.Reply);

  // UpdateAccount update account's metadata.
  rpc UpdateAccount (UpdateAccount.Request) returns (UpdateAccount.Reply);

  // GetGRPCListenerAddrs return current listeners addrs available on this bridge.
  rpc GetGRPCListenerAddrs (GetGRPCListenerAddrs.Request) returns (GetGRPCListenerAddrs.Reply);

  // LogfileList returns a list of logfiles that can be used with Tyber.
  rpc LogfileList (LogfileList.Request) returns (LogfileList.Reply);

  // StreamLogfile stream the lastest log file for the given account
  rpc StreamLogfile (StreamLogfile.Request) returns (stream StreamLogfile.Reply);

  // GetUsername returns the name of the device/user using Android/iOS/universal API
  rpc GetUsername(GetUsername.Request) returns (GetUsername.Reply);

  // NetworkConfigSet
  rpc NetworkConfigSet (NetworkConfigSet.Request) returns (NetworkConfigSet.Reply);

  // NetworkConfigGet
  rpc NetworkConfigGet (NetworkConfigGet.Request) returns (NetworkConfigGet.Reply);

  // NetworkConfigGetPreset
  rpc NetworkConfigGetPreset (NetworkConfigGetPreset.Request) returns (NetworkConfigGetPreset.Reply);

  // PushReceive
  rpc PushReceive (PushReceive.Request) returns (PushReceive.Reply);

  // PushPlatformTokenRegister
  rpc PushPlatformTokenRegister (PushPlatformTokenRegister.Request) returns (PushPlatformTokenRegister.Reply);

  // AppStoragePut puts a value by key in a storage secured by the device enclave if possible
  rpc AppStoragePut(AppStoragePut.Request) returns (AppStoragePut.Reply);

  // AppStorageGet gets a value by key in a storage secured by the device enclave if possible
  rpc AppStorageGet(AppStorageGet.Request) returns (AppStorageGet.Reply);

  // AppStorageRemove removes a value by key in a storage secured by the device enclave if possible
  rpc AppStorageRemove(AppStorageRemove.Request) returns (AppStorageRemove.Reply);

  // GetOpenedAccount returns details of the opened account
  rpc GetOpenedAccount(GetOpenedAccount.Request) returns (GetOpenedAccount.Reply);
}

message AppStoragePut {
  message Request {
    string key = 1;
    bytes value = 2;
    bool global = 3;
  }
  message Reply {}
}

message AppStorageGet {
  message Request {
    string key = 1;
    bool global = 2;
  }
  message Reply {
    bytes value = 2;
  }
}

message AppStorageRemove {
  message Request {
    string key = 1;
    bool global = 2;
  }
  message Reply {}
}

message GetOpenedAccount {
  message Request {}
  message Reply {
    string account_id = 1;
    repeated string listeners = 2;
  }
}

message OpenAccount {
  message Request {
    repeated string args = 1;
    string account_id = 2;
    string logger_filters = 3;
    NetworkConfig network_config = 4;
    string session_kind = 5;
  }
  message Reply {
    AccountMetadata account_metadata = 1;
  }
}

message OpenAccountWithProgress {
  message Request {
    repeated string args = 1;
    string account_id = 2;
    string logger_filters = 3;
    string session_kind = 4;
  }
  message Reply {
    weshnet.protocol.v1.Progress progress = 1;
  }
}

message CloseAccount {
  message Request {}
  message Reply {
  }
}

message CloseAccountWithProgress {
  message Request {}
  message Reply {
    weshnet.protocol.v1.Progress progress = 1;
  }
}

message AccountMetadata {
  string account_id = 1;
  string name = 2;
  string avatar_cid = 3;
  string public_key = 4;
  int64 last_opened = 5;
  int64 creation_date = 6;
  string error = 7;
}

message ListAccounts {
  message Request {}
  message Reply {
    repeated AccountMetadata accounts = 1;
  }
}

message DeleteAccount {
  message Request {
    string account_id = 1;
  }
  message Reply {
  }
}

message ImportAccount {
  message Request {
    string account_id = 1;
    string account_name = 2;
    string backup_path = 3;
    repeated string args = 4;
    string logger_filters = 5;
    NetworkConfig network_config = 6;
    string session_kind = 7;
  }
  message Reply {
    AccountMetadata account_metadata = 1;
  }
}

message ImportAccountWithProgress {
  message Request {
    string account_id = 1;
    string account_name = 2;
    string backup_path = 3;
    repeated string args = 4;
    string logger_filters = 5;
    NetworkConfig network_config = 6;
    string session_kind = 7;
  }
  message Reply {
    weshnet.protocol.v1.Progress progress = 1;
    AccountMetadata account_metadata = 2;
  }
}

message CreateAccount {
  message Request {
    string account_id = 1;
    string account_name = 2;
    NetworkConfig network_config = 3;
  }
  message Reply {
    AccountMetadata account_metadata = 1;
  }
}

message UpdateAccount {
  message Request {
    string account_id = 1;
    string account_name = 2;
    string avatar_cid = 3;
    string public_key = 4;
  }
  message Reply {
    AccountMetadata account_metadata = 1;
  }
}

message GetGRPCListenerAddrs {
  message Request {}
  message Reply {
    repeated Entry entries = 1;
    message Entry {
      string proto = 1;
      string maddr = 2;
    }
  }
}

message LogfileList {
  message Request {}
  message Reply {
    repeated Logfile entries = 1;

    message Logfile {
      string account_id = 1;
      string name = 2;
      string path = 3;
      int64 size = 4;
      string kind = 5;
      int64 time = 6;
      bool latest = 7;
      string errs = 8;
    }
  }
}

message StreamLogfile {
  message Request {
    string account_id = 1;
  }
  message Reply {
    string line = 1;
    string fileName = 2;
  }
}

message GetUsername {
  message Request {}
  message Reply {
    string username = 1;
  }
}

message NetworkConfig {
  enum Flag {
    Undefined = 0;
    Disabled = 1;
    Enabled = 2;
  }

  enum TorFlag {
    TorUndefined = 0;
    TorDisabled = 1;
    TorOptional = 2;
    TorRequired = 3;
  }

  enum DHTFlag {
    DHTUndefined = 0;
    DHTDisabled = 1;
    DHTClient = 2;
    DHTServer = 3;
    DHTAuto = 4;
    DHTAutoServer = 5;
  }

  repeated string bootstrap = 1;
  repeated string rendezvous = 2;
  repeated string static_relay = 3;
  DHTFlag dht = 4;
  Flag bluetooth_le = 5;
  Flag apple_multipeer_connectivity = 6;
  Flag android_nearby = 7;
  TorFlag tor = 8;
  Flag mdns = 9;
  Flag show_default_services = 10;
  Flag allow_unsecure_grpc_connections = 11;
}

enum NetworkConfigPreset {
  Undefined = 0;
  Performance = 1;
  FullAnonymity = 2;
}

message NetworkConfigSet {
  message Request {
    string account_id = 1;
    NetworkConfig config = 2;
  }
  message Reply {}
}

message NetworkConfigGet {
  message Request {
    string account_id = 1;
  }
  message Reply {
    NetworkConfig default_config = 1;
    NetworkConfig current_config = 2;
    bool custom_config_exists = 3;
    repeated string default_bootstrap = 4;
    repeated string default_rendezvous = 5;
    repeated string default_static_relay = 6;
  }
}

message NetworkConfigGetPreset {
  message Request {
    NetworkConfigPreset preset = 1;
    bool has_bluetooth_permission = 2;
  }
  message Reply {
    NetworkConfig config = 1;
  }
}

message PushReceive {
  message Request {
    string payload = 1;
    berty.push.v1.PushServiceTokenType token_type = 2;
  }
  message Reply {
    berty.push.v1.DecryptedPush push_data = 1;
    berty.push.v1.FormatedPush push = 2;
  }
}

message PushPlatformTokenRegister {
  message Request {
    push.v1.PushServiceReceiver receiver = 1;
  }
  message Reply {}
}
