syntax = "proto3";
package loyalty;
import "google/protobuf/empty.proto";

message e {}

service Loyalty {
  rpc CreateAccount(NewAccount) returns (Account);
  rpc GetAccountByCustomerId(CustomerRequest) returns (Account);
  rpc GetAccountByCardId(CardRequest) returns (Account);
  rpc GetAccountByQuery(QueryRequest) returns (Account);
  rpc GetTransactionsAll(TransactionAllRequest) returns (stream Transaction);
  rpc SetCard(Card) returns (Account);
  rpc SetLoyaltyLevel(LoyaltyLevelRequest) returns (Account);
  rpc SetBirthdate(SetBirthdateRequest) returns (Account);
  rpc BurnPoints(BurnRequest) returns (Account);
  rpc ClosePurchase(ClosePurchaseRequest) returns (PurchaseSummary);
}

message Transaction {
  enum TransactionKind {
    Burn = 0;
    Earn = 1;
  }
  string transaction_id = 1;
  string account_id = 2;
  string purchase_id = 3;
  TransactionKind transaction_kind = 4;
  int32 amount = 5;
  uint32 created_by = 6;
  string created_at = 7;
}

message TransactionAllRequest { string account_id = 1; }

message TransactionIds { repeated string transaction_ids = 1; }

message NewAccount {
  uint32 customer_id = 1;
  string birthdate = 2;
  uint32 created_by = 3;
}

message Account {
  string account_id = 1;           // Account ID
  uint32 customer_id = 2;          // Owner
  string customer_birthdate = 3;   // Customer birthdate for validation purpose
  string card_id = 4;              // Related active card id
  string loyalty_level = 5;        // Loyalty level
  int32 balance_points = 6;        // Balance in points
  int32 yearly_gross_turnover = 7; //
  string created_at = 8;           // RFC3339
  uint32 created_by = 9;           // User ID
}

message CustomerRequest { uint32 customer_id = 1; }

message CardRequest { string card_id = 1; }

message QueryRequest {
  uint32 customer_id = 1;
  string birthdate = 2;
}

message GetPurchaseRequest {
  string account_id = 1;
  string purchase_id = 2;
}

message GetPurchaseAllRequest { string account_id = 1; }

message PurchaseSummary {
  string account_id = 1;
  string purchase_id = 2;
  int32 balance_opening = 3;
  int32 burned_points = 5;
  int32 earned_points = 6;
  int32 balance_closing = 7;
}

message LoyaltyLevelRequest {
  string account_id = 1;
  string loyalty_level = 2;
  uint32 created_by = 3;
}

message SetBirthdateRequest {
  string account_id = 1;
  string birthdate = 2; // Naivedate YYYY-mm-dd, eg.: 1988-10-15
  uint32 created_by = 3;
}

message Card {
  string set_to_account_id = 1;
  string card_id = 2;
  uint32 created_by = 3;
}

message ClosePurchaseRequest {
  string account_id = 1;
  string purchase_id = 2;
  uint32 total_gross = 3;
  uint32 created_by = 4;
}

message BurnRequest {
  string account_id = 1;
  string purchase_id = 2;
  int32 points_to_burn = 3;
  uint32 created_by = 4;
}