syntax = "proto3";

package exchange_rates;

import "google/protobuf/struct.proto";

option csharp_namespace = "ExchangeRates";
option java_multiple_files = true;
option java_package = "com.arbiwallet.exchange.rates";

service ExchangeRatesService {
  rpc GetCurrencySettings(GetCurrencySettingsRequest) returns (GetCurrencySettingsResponse);
  rpc UpdateCurrencySetting(UpdateCurrencySettingRequest) returns (UpdateCurrencySettingResponse);
  rpc GetRatesPage(GetRatesPageRequest) returns (GetRatesPageResponse);
  rpc RefreshRatesPage(RefreshRatesPageRequest) returns (RefreshRatesPageResponse);

  rpc CreatePrimeRate(CreatePrimeRateRequest) returns (CreatePrimeRateResponse);
  rpc GetPrimeRate(GetPrimeRateRequest) returns (GetPrimeRateResponse);
  rpc GetPrimeRates(GetPrimeRatesRequest) returns (GetPrimeRatesResponse);
  rpc UpdatePrimeRate(UpdatePrimeRateRequest) returns (UpdatePrimeRateResponse);
  rpc DeletePrimeRate(DeletePrimeRateRequest) returns (DeletePrimeRateResponse);

  rpc GetExchangeRates(GetExchangeRatesRequest) returns (GetExchangeRatesResponse);
  rpc GetCompetitorRates(GetCompetitorRatesRequest) returns (GetCompetitorRatesResponse);

  rpc GetMarketRates(GetMarketRatesRequest) returns (GetMarketRatesResponse);
  rpc GetMarketRate(GetMarketRateRequest) returns (GetMarketRateResponse);

  rpc GetCustomRates(GetCustomRatesRequest) returns (GetCustomRatesResponse);
  rpc UpdateCustomRate(UpdateCustomRateRequest) returns (UpdateCustomRateResponse);

  rpc GetBybitRateProfit(GetBybitRateProfitRequest) returns (GetBybitRateProfitResponse);
  rpc UpdateBybitRateProfit(UpdateBybitRateProfitRequest) returns (UpdateBybitRateProfitResponse);

  rpc CalculateExchange(CalculateExchangeRequest) returns (CalculateExchangeResponse);
  rpc GetExchangeCalculation(GetExchangeCalculationRequest) returns (GetExchangeCalculationResponse);
  rpc GetExchangeCalculationHistory(GetExchangeCalculationHistoryRequest) returns (GetExchangeCalculationHistoryResponse);
  rpc GetExchangeCalculationStats(ExchangeCalculationFiltersRequest) returns (GetExchangeCalculationStatsResponse);

  rpc UpdateCalculationPaymentParams(UpdateCalculationPaymentParamsRequest) returns (UpdateCalculationPaymentParamsResponse);
  rpc UpdateCalculationDealParams(UpdateCalculationDealParamsRequest) returns (UpdateCalculationDealParamsResponse);
}

message CurrencySetting {
  string id = 1;
  string short_name = 2;
  optional string name = 3;
  optional string currency_type = 4;
  optional string currency_type_id = 5;
  optional string symbol = 6;
  optional double priority_buy = 7;
  optional double priority_sell = 8;
  optional string price_buy = 9;
  optional string price_sell = 10;
  bool one_price_for_variations_buy = 11;
  optional string one_price_buy_type_id = 12;
  optional string one_price_buy_type = 13;
  bool one_price_for_variations_sell = 14;
  optional string one_price_sell_type_id = 15;
  optional string one_price_sell_type = 16;
  optional int32 output_rounding = 17;
}

message GetCurrencySettingsRequest {
  optional string search = 1;
}

message GetCurrencySettingsResponse {
  bool exist = 1;
  repeated CurrencySetting settings = 2;
}

message UpdateCurrencySettingRequest {
  string id = 1;
  optional double priority_buy = 2;
  optional double priority_sell = 3;
  optional string price_buy = 4;
  optional string price_sell = 5;
  optional bool one_price_for_variations_buy = 6;
  optional string one_price_buy_type_id = 7;
  optional bool one_price_for_variations_sell = 8;
  optional string one_price_sell_type_id = 9;
  optional int32 output_rounding = 10;
}

message UpdateCurrencySettingResponse {
  bool ok = 1;
  optional CurrencySetting setting = 2;
  optional string error = 3;
}

message GetRatesPageRequest {
  optional string slug = 1;
}

message RatesPageRow {
  string key = 1;
  string label = 2;
  string currency = 3;
  int32 id = 4;
  optional string icon_url = 5;
  string buying_rate = 6;
  string selling_rate = 7;
  string buy_str = 8;
  string sell_str = 9;
}

message GetRatesPageResponse {
  bool ok = 1;
  string slug = 2;
  string title = 3;
  string updated_at = 4;
  string logo_url = 5;
  string qr_code_url = 6;
  string qr_text = 7;
  repeated RatesPageRow rows = 8;
  optional string error = 9;
}

message RefreshRatesPageRequest {
  optional string slug = 1;
}

message RefreshRatesPageResponse {
  bool ok = 1;
  string slug = 2;
  optional string updated_at = 3;
  optional int32 rows_count = 4;
  optional string error = 5;
}

message GetExchangeRatesRequest {}

message ExchangeRate {
  string id = 1;
  string pair = 2;
  string first_currency = 3;
  string second_currency = 4;
  optional double exchange_rate = 5;
  optional double real_exchange_rate = 6;
  bool is_new = 7;
  optional google.protobuf.Struct buy_prices = 8;
  optional google.protobuf.Struct sell_prices = 9;
  optional google.protobuf.Struct percents = 10;
  string created_at = 11;
  string updated_at = 12;
}

message GetExchangeRatesResponse {
  bool exist = 1;
  repeated ExchangeRate rates = 2;
}

message GetCompetitorRatesRequest {}

message CompetitorRate {
  string company = 1;
  map<string, string> rates = 2;
  string date = 3;
}

message GetCompetitorRatesResponse {
  bool exist = 1;
  repeated CompetitorRate rates = 2;
}

message MarketRate {
  string id = 1;
  string updated_at = 2;
  string input_currency = 3;
  string output_currency = 4;
  double rate = 5;
  optional string market = 6;
}

message GetMarketRatesRequest {}

message GetMarketRatesResponse {
  bool exist = 1;
  repeated MarketRate rates = 2;
}

message GetMarketRateRequest {
  string id = 1;
}

message GetMarketRateResponse {
  bool exist = 1;
  optional MarketRate rate = 2;
}

message PrimeRate {
  string id = 1;
  string input_currency_id = 2;
  string output_currency_id = 3;
  optional string input_currency = 4;
  optional string output_currency = 5;
  double rate = 6;
}

message CreatePrimeRateRequest {
  string input_currency_id = 1;
  string output_currency_id = 2;
  double rate = 3;
}

message CreatePrimeRateResponse {
  bool ok = 1;
  optional PrimeRate rate = 2;
  optional string error = 3;
}

message GetPrimeRateRequest {
  string id = 1;
}

message GetPrimeRateResponse {
  bool exist = 1;
  optional PrimeRate rate = 2;
}

message GetPrimeRatesRequest {}

message GetPrimeRatesResponse {
  bool exist = 1;
  repeated PrimeRate rates = 2;
}

message UpdatePrimeRateRequest {
  string id = 1;
  optional string input_currency_id = 2;
  optional string output_currency_id = 3;
  optional double rate = 4;
}

message UpdatePrimeRateResponse {
  bool ok = 1;
  optional PrimeRate rate = 2;
  optional string error = 3;
}

message DeletePrimeRateRequest {
  string id = 1;
}

message DeletePrimeRateResponse {
  bool ok = 1;
  optional string error = 2;
}

message GetCustomRatesRequest {}

message CustomRate {
  string id = 1;
  string input_currency_id = 2;
  string output_currency_id = 3;
  optional string input_currency = 4;
  optional string output_currency = 5;
  double rate = 6;
  string rate_profit = 7;
}

message GetCustomRatesResponse {
  bool exist = 1;
  repeated CustomRate rates = 2;
}

message UpdateCustomRateRequest {
  string id = 1;
  optional string input_currency_id = 2;
  optional string output_currency_id = 3;
  optional double rate = 4;
  optional string rate_profit = 5;
}

message UpdateCustomRateResponse {
  bool ok = 1;
  optional CustomRate rate = 2;
  optional string error = 3;
}

message GetBybitRateProfitRequest {}

message GetBybitRateProfitResponse {
  bool exist = 1;
  optional string rate_profit = 2;
}

message UpdateBybitRateProfitRequest {
  string rate_profit = 1;
}

message UpdateBybitRateProfitResponse {
  bool ok = 1;
  optional string rate_profit = 2;
  optional string error = 3;
}

message AuthManagerContext {
  string manager_id = 1;
  repeated string office_ids = 2;
  bool can_cross_office = 3;
  optional string request_id = 4;
  optional string client_type = 5;
  repeated string permissions = 6;
  bool is_super_admin = 7;
}

message CalculateExchangeRequest {
  optional AuthManagerContext auth_manager_context = 1;

  string input_currency = 2;
  string output_currency = 3;
  string currency_for_amount = 4;
  double amount = 5;
  optional double adjustment = 6;
  optional double promo_discount = 7;
  optional string customer_id = 8;
  optional bool disable_rounding = 9;
  optional double fixed_rate = 10;
}

message CalculateExchangeResult {
  bool result = 1;
  optional string calculation_id = 2;
  double input_currency_quantity = 3;
  double output_currency_quantity = 4;
  string exchange_rate_for_message = 5;
  double exchange_rate = 6;
  double real_exchange_rate = 7;
  double final_percent = 8;
  double adjustment = 9;
  bool has_customer = 10;
  optional string customer_id = 11;
  repeated int32 available_agents = 12;
  optional double exchange_rate_before_rounding = 13;
  string real_rate_for_message = 14;
}

message CalculateExchangeResponse {
  bool ok = 1;
  optional CalculateExchangeResult result = 2;
  optional string error = 3;
  optional google.protobuf.Struct validationErrors = 4;
}

message GetExchangeCalculationRequest {
  string calculation_id = 1;
}

message ExchangeCalculation {
  string id = 1;
  optional string customer_id = 2;
  optional string manager_id = 3;
  string input_currency_id = 4;
  string output_currency_id = 5;
  string currency_for_amount_id = 6;
  double amount = 7;
  double input_currency_quantity = 8;
  double output_currency_quantity = 9;
  double exchange_rate = 10;
  double real_exchange_rate = 11;
  double final_percent = 12;
  double adjustment = 13;
  double promo_discount = 14;
  string created_at = 15;
  string updated_at = 16;
}

message GetExchangeCalculationResponse {
  bool exist = 1;
  optional ExchangeCalculation result = 2;
}

message GetExchangeCalculationHistoryRequest {
  optional string date_from = 1;
  optional string date_to = 2;
  optional double amount_from = 3;
  optional double amount_to = 4;
  optional string input_currency = 5;
  optional string output_currency = 6;
  optional string status = 7; // no_payment | with_payment | with_exchange | completed
  optional string search = 8;
  optional int32 page = 9;
  optional int32 page_size = 10;
  optional AuthManagerContext auth_manager_context = 11;
}

message ExchangeCalculationFiltersRequest {
  optional string date_from = 1;
  optional string date_to = 2;
  optional double amount_from = 3;
  optional double amount_to = 4;
  optional string input_currency = 5;
  optional string output_currency = 6;
  optional string status = 7; // no_payment | with_payment | with_exchange | completed
  optional string search = 8;
  optional AuthManagerContext auth_manager_context = 9;
}

message ExchangeCalculationHistoryAppliedFilters {
  optional string date_from = 1;
  optional string date_to = 2;
  optional double amount_from = 3;
  optional double amount_to = 4;
  optional string input_currency = 5;
  optional string output_currency = 6;
  optional string status = 7;
  optional string search = 8;
}

message ExchangeCalculationHistoryPagination {
  int32 page = 1;
  int32 page_size = 2;
  int32 total_pages = 3;
  int32 total_count = 4;
  bool has_next = 5;
  bool has_previous = 6;
}

enum ExchangeCalculationDealStatus {
  EXCHANGE_CALCULATION_DEAL_STATUS_UNSPECIFIED = 0;
  EXCHANGE_CALCULATION_DEAL_STATUS_NO_DEAL = 1;
  EXCHANGE_CALCULATION_DEAL_STATUS_WITH_PAYMENT = 2;
  EXCHANGE_CALCULATION_DEAL_STATUS_IN_PROGRESS = 3;
  EXCHANGE_CALCULATION_DEAL_STATUS_COMPLETED = 4;
  EXCHANGE_CALCULATION_DEAL_STATUS_CANCELLED = 5;
}

enum ExchangeCalculationAllowedAction {
  EXCHANGE_CALCULATION_ALLOWED_ACTION_UNSPECIFIED = 0;
  EXCHANGE_CALCULATION_ALLOWED_ACTION_CREATE_DEAL = 1;
  EXCHANGE_CALCULATION_ALLOWED_ACTION_OPEN_DEAL = 2;
}

message ExchangeCalculationHistoryItem {
  string id = 1;
  string created_at = 2;

  optional string customer_name = 3;
  string input_currency = 4;
  string output_currency = 5;
  double input_currency_quantity = 6;
  double output_currency_quantity = 7;
  double exchange_rate = 8;
  double final_percent = 9;
  optional string manager_username = 10;

  bool has_payment = 11;
  optional string payment_uuid = 12;
  bool has_exchange = 13;
  optional string exchange_id = 14;
  optional bool exchange_completed = 15;

  string status = 16; // legacy: no_payment | with_payment | with_exchange | completed
  ExchangeCalculationDealStatus deal_status = 17;
  repeated ExchangeCalculationAllowedAction allowed_actions = 18;
  bool can_create_deal = 19;
  bool can_open_deal = 20;
}

message GetExchangeCalculationHistoryResponse {
  bool exist = 1;
  repeated ExchangeCalculationHistoryItem items = 2;
  repeated string currencies = 3;
  optional ExchangeCalculationHistoryAppliedFilters filters = 4;
  optional ExchangeCalculationHistoryPagination pagination = 5;
}

message CalculationsInputCurrencySummaryItem {
  string currency = 1;
  double total = 2;
  double completed = 3;
  double in_progress = 4;
}

message GetExchangeCalculationStatsResponse {
  int32 total_count = 1;
  int32 unique_customers = 2;
  int32 with_exchange_count = 3;
  int32 completed_count = 4;
  repeated CalculationsInputCurrencySummaryItem items = 5;
}

message UpdateCalculationPaymentParamsRequest {
  string calculation_id = 1;
  string payment_id = 2;
  optional int32 payment_id_status = 3;
}

message UpdateCalculationPaymentParamsResponse {
  bool ok = 1;
  optional string error = 2;
}

message UpdateCalculationDealParamsRequest {
  string calculation_id = 1;
  optional string exchange_deal_id = 2;
  optional int32 exchange_deal_status = 3;
}

message UpdateCalculationDealParamsResponse {
  bool ok = 1;
  optional string error = 2;
}
