/*
  This file is part of Anastasis
  Copyright (C) 2019-2022 Anastasis SARL

  Anastasis is free software; you can redistribute it and/or modify it under the
  terms of the GNU Affero General Public License as published by the Free Software
  Foundation; either version 3, or (at your option) any later version.

  Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License along with
  Anastasis; see the file COPYING.GPL.  If not, see <http://www.gnu.org/licenses/>
*/
/**
 * @file include/anastasis/anastasis-database/common.h
 * @brief common definitions for the Anastasis database
 * @author Christian Grothoff
 */
#ifndef ANASTASIS_DATABASE_COMMON_H
#define ANASTASIS_DATABASE_COMMON_H

#include "anastasis_service.h"
#include <gnunet/gnunet_db_lib.h>

/**
 * How long is an offer for a challenge payment valid for payment?
 */
#define ANASTASIS_CHALLENGE_OFFER_LIFETIME GNUNET_TIME_UNIT_HOURS

/**
 * Return values for checking code validity.
 */
enum ANASTASIS_DB_CodeStatus
{
  /**
   * The challenge code that is currently answerable has no attempts
   * left; the client must wait for the next code to be issued.
   */
  ANASTASIS_DB_CODE_STATUS_RATE_LIMITED = -4,

  /**
   * Provided authentication code does not match database content.
   */
  ANASTASIS_DB_CODE_STATUS_CHALLENGE_CODE_MISMATCH = -3,

  /**
   * Encountered hard error talking to DB.
   */
  ANASTASIS_DB_CODE_STATUS_HARD_ERROR = -2,

  /**
   * Encountered serialization error talking to DB.
   */
  ANASTASIS_DB_CODE_STATUS_SOFT_ERROR = -1,

  /**
   * We have no challenge in the database.
   */
  ANASTASIS_DB_CODE_STATUS_NO_RESULTS = 0,

  /**
   * The provided challenge matches what we have in the database.
   */
  ANASTASIS_DB_CODE_STATUS_VALID_CODE_STORED = 1,
};


/**
 * Return values for checking account validity.
 */
enum ANASTASIS_DB_AccountStatus
{
  /**
   * Account is unknown, user should pay to establish it.
   */
  ANASTASIS_DB_ACCOUNT_STATUS_PAYMENT_REQUIRED = -3,

  /**
   * Encountered hard error talking to DB.
   */
  ANASTASIS_DB_ACCOUNT_STATUS_HARD_ERROR = -2,

  /**
   * Account is valid, but we have no policy stored yet.
   */
  ANASTASIS_DB_ACCOUNT_STATUS_NO_RESULTS = 0,

  /**
   * Account is valid, and we have a policy stored.
   */
  ANASTASIS_DB_ACCOUNT_STATUS_VALID_HASH_RETURNED = 1,
};


/**
 * Return values for storing data in database with payment.
 */
enum ANASTASIS_DB_StoreStatus
{
  /**
   * The client has stored too many policies, should pay to store more.
   */
  ANASTASIS_DB_STORE_STATUS_STORE_LIMIT_EXCEEDED = -4,

  /**
   * The client needs to pay to store policies.
   */
  ANASTASIS_DB_STORE_STATUS_PAYMENT_REQUIRED = -3,

  /**
   * Encountered hard error talking to DB.
   */
  ANASTASIS_DB_STORE_STATUS_HARD_ERROR = -2,

  /**
   * Despite retrying, we encountered serialization errors.
   */
  ANASTASIS_DB_STORE_STATUS_SOFT_ERROR = -1,

  /**
   * Database did not need an update (document exists).
   */
  ANASTASIS_DB_STORE_STATUS_NO_RESULTS = 0,

  /**
   * We successfully stored the document.
   */
  ANASTASIS_DB_STORE_STATUS_SUCCESS = 1,
};


/**
 * Function called to test if a given wire transfer
 * satisfied the authentication requirement of the
 * IBAN plugin.
 *
 * @param cls closure
 * @param credit amount that was transferred
 * @param wire_subject subject provided in the wire transfer
 * @return true if this wire transfer satisfied the authentication check
 */
typedef bool
(*ANASTASIS_DB_AuthIbanTransferCallback)(
  void *cls,
  const struct TALER_Amount *credit,
  const char *wire_subject);


/**
 * Function called on matching meta data.  Note that if the client did
 * not provide meta data for @a version, the function will be called
 * with @a recovery_meta_data being NULL.
 *
 * @param cls closure
 * @param version the version of the recovery document
 * @param ts timestamp when the document was uploaded
 * @param recovery_meta_data contains meta data about the encrypted recovery document
 * @param recovery_meta_data_size size of @a recovery_meta_data blob
 * @return #GNUNET_OK to continue to iterate, #GNUNET_NO to abort iteration
 */
typedef enum GNUNET_GenericReturnValue
(*ANASTASIS_DB_RecoveryMetaCallback)(void *cls,
                                     uint32_t version,
                                     struct GNUNET_TIME_Timestamp ts,
                                     const void *recovery_meta_data,
                                     size_t recovery_meta_data_size);

#endif
