syntax = "proto3";

import "google/protobuf/empty.proto";
import "google/protobuf/timestamp.proto";

package intranet.d.backend.service.provider_proto;

option java_multiple_files = true;
option java_package = "ru.yandex.intranet.d.backend.service.provider_proto";
option java_outer_classname = "ProvidersCommonProto";
option go_package = "a.yandex-team.ru/intranet/d/backend/service/provider_proto;providerpb";

service AccountsService {
    // Update provided resources
    rpc UpdateProvision(UpdateProvisionRequest) returns (UpdateProvisionResponse) {};
    // Get one account by id
    rpc GetAccount(GetAccountRequest) returns (Account) {};
    // Create account
    rpc CreateAccount(CreateAccountRequest) returns (Account) {};
    // Create account and provide
    rpc CreateAccountAndProvide(CreateAccountAndProvideRequest) returns (Account) {};
    // Delete account
    rpc DeleteAccount(DeleteAccountRequest) returns (google.protobuf.Empty) {};
    // Rename account
    rpc RenameAccount(RenameAccountRequest) returns (Account) {};
    // Move account to another folder
    rpc MoveAccount(MoveAccountRequest) returns (Account) {};
    // Move provisions between accounts
    rpc MoveProvision(MoveProvisionRequest) returns (MoveProvisionResponse) {};
    // List all accounts
    rpc ListAccounts(ListAccountsRequest) returns (ListAccountsResponse) {};
    // List all accounts by folder
    rpc ListAccountsByFolder(ListAccountsByFolderRequest) returns (ListAccountsByFolderResponse) {};
    // Set free tier account flag to false
    rpc RevokeFreeTier(RevokeFreeTierRequest) returns (Account) {};
}

// User ID
message UserID {
    // Either Passport uid or something else in the future
    oneof user_identifier {
      PassportUID passport_uid = 1;
    }
    // Either Staff login or something else in the future
    oneof user_key {
      StaffLogin staff_login = 10;
    }
}

// Passport UID
message PassportUID {
    // Passport UID
    string passport_uid = 1;
}

// Staff login
message StaffLogin {
    // Staff login
    string staff_login = 1;
}

// Last provided amount update
message LastUpdate {
    // Unix timestamp
    google.protobuf.Timestamp timestamp = 1;
    // Operation id
    string operation_id = 2;
    // Update author
    UserID author = 3;
}

// Resource amount
message Amount {
    // Value
    int64 value = 1;
    // Unit key
    string unit_key = 2;
}

// Resource key
message ResourceKey {
    // Compound resource key
    CompoundResourceKey compound_key = 1;
}

// Compound resource key
message CompoundResourceKey {
    // Resource type key
    string resource_type_key = 1;
    // Resource segment keys
    repeated ResourceSegmentKey resource_segment_keys = 2;
}

// Resource segment key
message ResourceSegmentKey {
    // Resource segmentation key
    string resource_segmentation_key = 1;
    // Resource segment key
    string resource_segment_key = 2;
}

// Accounts space key
message AccountsSpaceKey {
    // Compound accounts space key
    CompoundAccountsSpaceKey compound_key = 1;
}

// Compound accounts space key
message CompoundAccountsSpaceKey {
    // Resource segment keys
    repeated ResourceSegmentKey resource_segment_keys = 1;
}

// Current version
message CurrentVersion {
    int64 version = 1;
}

// Resource provision
message Provision {
    // Resource key
    ResourceKey resource_key = 1;
    // Provided amount
    Amount provided = 2;
    // Allocated amount
    Amount allocated = 3;
    // Last update for this allocation
    LastUpdate last_update = 4;
    // Current version of the provision
    CurrentVersion version = 5;
}

// Account
message Account {
    // Account id
    string account_id = 1;
    // Account key
    string key = 2;
    // Account display name
    string display_name = 3;
    // Folder id
    string folder_id = 4;
    // 'Account is deleted' flag
    bool deleted = 5;
    // Account resource provisions
    repeated Provision provisions = 6;
    // Accounts space key
    AccountsSpaceKey accounts_space_key = 7;
    // Current version of the account and its provisions
    CurrentVersion version = 8;
    // Last update for this account
    LastUpdate last_update = 9;
    // 'Account is free tier' flag
    bool free_tier = 10;
}

// New value for resource provision
message ProvisionRequest {
    // Resource key
    ResourceKey resource_key = 1;
    // Provided amount
    Amount provided = 2;
}

// Last known value of resource provision as saved in quota management
message KnownProvision {
    // Resource key
    ResourceKey resource_key = 1;
    // Provided amount
    Amount provided = 2;
}

// Last known value of resource provision as saved in quota management for an account
message KnownAccountProvisions {
    // Account id
    string account_id = 1;
    // Last known value of resource provision as saved in quota management for each resource in an account
    repeated KnownProvision known_provisions = 2;
}

// Provision update request
message UpdateProvisionRequest {
    // Account id
    string account_id = 1;
    // Folder id
    string folder_id = 2;
    // Abc service id
    int64 abc_service_id = 3;
    // Request author
    UserID author = 4;
    // Unique operation id for this update, does not change between retries
    string operation_id = 5;
    // Updated provided amounts
    repeated ProvisionRequest updated_provisions = 6;
    // Last known provided amounts as saved in quota management for each account in the folder
    repeated KnownAccountProvisions known_provisions = 7;
    // Provider id
    string provider_id = 8;
    // Accounts space key
    AccountsSpaceKey accounts_space_key = 9;
}

// Provision update response
message UpdateProvisionResponse {
    // Updated provisions
    repeated Provision provisions = 1;
    // Accounts space key
    AccountsSpaceKey accounts_space_key = 2;
    // Current version of the account and its provisions
    CurrentVersion account_version = 3;
}

// Get account request
message GetAccountRequest {
    // Account id
    string account_id = 1;
    // Include provisions within returned account
    bool with_provisions = 2;
    // Return account even if it is deleted
    bool include_deleted = 3;
    // Folder id
    string folder_id = 4;
    // Abc service id
    int64 abc_service_id = 5;
    // Provider id
    string provider_id = 6;
    // Accounts space key
    AccountsSpaceKey accounts_space_key = 7;
}

// Create account request
message CreateAccountRequest {
    // Folder id
    string folder_id = 1;
    // Account key
    string key = 2;
    // Account display name
    string display_name = 3;
    // Abc service id of the folder
    int64 abc_service_id = 4;
    // Request author
    UserID author = 5;
    // Unique operation id for this update, does not change between retries
    string operation_id = 6;
    // Provider id
    string provider_id = 7;
    // Accounts space key
    AccountsSpaceKey accounts_space_key = 8;
    // 'Account is free tier' flag
    bool free_tier = 9;
    // Abc service slug of the folder
    string abc_service_slug = 10;
}

// Create account and provide request
message CreateAccountAndProvideRequest {
    // Folder id
    string folder_id = 1;
    // Account key
    string key = 2;
    // Account display name
    string display_name = 3;
    // Abc service id of the folder
    int64 abc_service_id = 4;
    // Provided amounts
    repeated ProvisionRequest new_provisions = 5;
    // Request author
    UserID author = 6;
    // Unique operation id for this update, does not change between retries
    string operation_id = 7;
    // Last known provided amounts as saved in quota management for each account in the folder
    repeated KnownAccountProvisions known_provisions = 8;
    // Provider id
    string provider_id = 9;
    // Accounts space key
    AccountsSpaceKey accounts_space_key = 10;
    // 'Account is free tier' flag
    bool free_tier = 11;
    // Abc service slug of the folder
    string abc_service_slug = 12;
}

// Delete account request
message DeleteAccountRequest {
    // Account id
    string account_id = 1;
    // Folder id
    string folder_id = 2;
    // Abc service id of the folder
    int64 abc_service_id = 3;
    // Request author
    UserID author = 4;
    // Unique operation id for this update, does not change between retries
    string operation_id = 5;
    // Provider id
    string provider_id = 6;
    // Accounts space key
    AccountsSpaceKey accounts_space_key = 7;
}

// Rename account request
message RenameAccountRequest {
    // Account id
    string account_id = 1;
    // Updated account display name
    string display_name = 2;
    // Folder id
    string folder_id = 3;
    // Abc service id of the folder
    int64 abc_service_id = 4;
    // Request author
    UserID author = 5;
    // Unique operation id for this update, does not change between retries
    string operation_id = 6;
    // Provider id
    string provider_id = 7;
    // Accounts space key
    AccountsSpaceKey accounts_space_key = 8;
}

// Move account request
message MoveAccountRequest {
    // Account id
    string account_id = 1;
    // Source folder id
    string source_folder_id = 2;
    // Destination folder id
    string destination_folder_id = 3;
    // Abc service id of the source folder
    int64 source_abc_service_id = 4;
    // Abc service id of the destination folder
    int64 destination_abc_service_id = 5;
    // Last known provided amounts as saved in quota management for all accounts in source folder
    repeated KnownAccountProvisions known_source_provisions = 6;
    // Last known provided amounts as saved in quota management for all accounts in destination folder
    repeated KnownAccountProvisions known_destination_provisions = 7;
    // Request author
    UserID author = 8;
    // Unique operation id for this update, does not change between retries
    string operation_id = 9;
    // Provider id
    string provider_id = 10;
    // Accounts space key
    AccountsSpaceKey accounts_space_key = 11;
}

// Move provision request
message MoveProvisionRequest {
    // Source account id
    string source_account_id = 1;
    // Destination account id
    string destination_account_id = 2;
    // Source folder id
    string source_folder_id = 3;
    // Destination folder id
    string destination_folder_id = 4;
    // Abc service id of the source folder
    int64 source_abc_service_id = 5;
    // Abc service id of the destination folder
    int64 destination_abc_service_id = 6;
    // Last known provided amounts as saved in quota management for all accounts in the folder of the source account
    repeated KnownAccountProvisions known_source_provisions = 7;
    // Last known provided amounts as saved in quota management for all accounts in the folder of the destination account
    repeated KnownAccountProvisions known_destination_provisions = 8;
    // Updated provided amounts for source account
    repeated ProvisionRequest updated_source_provisions = 9;
    // Updated provided amounts for destination account
    repeated ProvisionRequest updated_destination_provisions = 10;
    // Request author
    UserID author = 11;
    // Unique operation id for this update, does not change between retries
    string operation_id = 12;
    // Provider id
    string provider_id = 13;
    // Accounts space key
    AccountsSpaceKey accounts_space_key = 14;
}

// Provision move response
message MoveProvisionResponse {
    // Updated source provisions
    repeated Provision source_provisions = 1;
    // Updated destination provisions
    repeated Provision destination_provisions = 2;
    // Accounts space key
    AccountsSpaceKey accounts_space_key = 3;
    // Current version of the source account and its provisions
    CurrentVersion source_account_version = 4;
    // Current version of the destination account and its provisions
    CurrentVersion destination_account_version = 5;
}

// Accounts listing page token
message AccountsPageToken {
    // Page token
    string token = 1;
}

// List accounts request
message ListAccountsRequest {
    // Limit per page
    int64 limit = 1;
    // Page token
    AccountsPageToken page_token = 2;
    // Include provisions within returned account
    bool with_provisions = 3;
    // Return deleted account also
    bool include_deleted = 4;
    // Provider id
    string provider_id = 5;
    // Accounts space key
    AccountsSpaceKey accounts_space_key = 6;
}

// List accounts response
message ListAccountsResponse {
    // Next page token
    AccountsPageToken next_page_token = 1;
    // Accounts
    repeated Account accounts = 2;
}

// List accounts by folder request
message ListAccountsByFolderRequest {
    string folder_id = 1;
    // Limit per page
    int64 limit = 2;
    // Page token
    AccountsPageToken page_token = 3;
    // Include provisions within returned account
    bool with_provisions = 4;
    // Return deleted account also
    bool include_deleted = 5;
    // Abc service id of the folder
    int64 abc_service_id = 6;
    // Provider id
    string provider_id = 7;
    // Accounts space key
    AccountsSpaceKey accounts_space_key = 8;
}

// List accounts by folder response
message ListAccountsByFolderResponse {
    // Next page token
    AccountsPageToken next_page_token = 1;
    // Accounts
    repeated Account accounts = 2;
}

// Free tier revoke request
message RevokeFreeTierRequest {
    // Account id
    string account_id = 1;
    // Folder id
    string folder_id = 2;
    // Abc service id
    int64 abc_service_id = 3;
    // Request author
    UserID author = 4;
    // Unique operation id for this update, does not change between retries
    string operation_id = 5;
    // Provider id
    string provider_id = 6;
    // Accounts space key
    AccountsSpaceKey accounts_space_key = 7;
}
