/*
  This file is part of TALER
  Copyright (C) 2014-2026 Taler Systems SA

  TALER 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.

  TALER 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
  TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
/**
 * @file include/taler/exchange/post-reserves-RESERVE_PUB-purse.h
 * @brief C interface for POST /reserves/$RESERVE_PUB/purse
 * @author Christian Grothoff
 */
#ifndef _TALER_EXCHANGE__POST_RESERVES_RESERVE_PUB_PURSE_H
#define _TALER_EXCHANGE__POST_RESERVES_RESERVE_PUB_PURSE_H

#include <taler/exchange/common.h>


/**
 * Possible options we can set for the POST /reserves/$RESERVE_PUB/purse request.
 */
enum TALER_EXCHANGE_PostReservesPurseOption
{
  /**
   * End of list of options.
   */
  TALER_EXCHANGE_POST_RESERVES_PURSE_OPTION_END = 0,

  /**
   * Upload the contract to the exchange.
   * If not set, defaults to not uploading the contract.
   */
  TALER_EXCHANGE_POST_RESERVES_PURSE_OPTION_UPLOAD_CONTRACT

};


/**
 * Value for an option we can set for the POST /reserves/$RESERVE_PUB/purse request.
 */
struct TALER_EXCHANGE_PostReservesPurseOptionValue
{
  /**
   * Type of the option being set.
   */
  enum TALER_EXCHANGE_PostReservesPurseOption option;

  /**
   * Specific option value.
   */
  union
  {
  } details;

};


/**
 * Handle for an operation to POST /reserves/$RESERVE_PUB/purse.
 */
struct TALER_EXCHANGE_PostReservesPurseHandle;


/**
 * Set up POST /reserves/$RESERVE_PUB/purse operation.
 * Note that you must explicitly start the operation after setup.
 *
 * @param ctx curl context
 * @param url exchange base URL
 * @param keys exchange keys
 * @param reserve_priv private key of the reserve
 * @param purse_priv private key of the purse
 * @param merge_priv private key of the merge capability
 * @param contract_priv private key to get the contract
 * @param contract_terms contract the purse is about
 * @param pay_for_purse true to pay for purse creation
 * @param merge_timestamp when should the merge happen (use current time)
 * @return handle to operation, NULL on error
 */
struct TALER_EXCHANGE_PostReservesPurseHandle *
TALER_EXCHANGE_post_reserves_purse_create (
  struct GNUNET_CURL_Context *ctx,
  const char *url,
  struct TALER_EXCHANGE_Keys *keys,
  const struct TALER_ReservePrivateKeyP *reserve_priv,
  const struct TALER_PurseContractPrivateKeyP *purse_priv,
  const struct TALER_PurseMergePrivateKeyP *merge_priv,
  const struct TALER_ContractDiffiePrivateP *contract_priv,
  const json_t *contract_terms,
  bool pay_for_purse, // FIXME: turn into option (default: false)
  struct GNUNET_TIME_Timestamp merge_timestamp);


/**
 * Terminate the list of options.
 *
 * @return the terminating object of struct TALER_EXCHANGE_PostReservesPurseOptionValue
 */
#define TALER_EXCHANGE_post_reserves_purse_option_end_()              \
        (const struct TALER_EXCHANGE_PostReservesPurseOptionValue)    \
        {                                                             \
          .option = TALER_EXCHANGE_POST_RESERVES_PURSE_OPTION_END    \
        }

/**
 * Set flag to upload the contract to the exchange to true.
 *
 * @return representation of the option as a struct TALER_EXCHANGE_PostReservesPurseOptionValue
 */
#define TALER_EXCHANGE_post_reserves_purse_option_upload_contract()           \
        (const struct TALER_EXCHANGE_PostReservesPurseOptionValue)              \
        {                                                                       \
          .option = TALER_EXCHANGE_POST_RESERVES_PURSE_OPTION_UPLOAD_CONTRACT, \
        }


/**
 * Set the requested options for the operation.
 *
 * If any option fails, other options may or may not be applied.
 *
 * @param prph the request to set the options for
 * @param num_options length of the @a options array
 * @param options an array of options
 * @return #GNUNET_OK on success,
 *         #GNUNET_NO on failure,
 *         #GNUNET_SYSERR on internal error
 */
enum GNUNET_GenericReturnValue
TALER_EXCHANGE_post_reserves_purse_set_options_ (
  struct TALER_EXCHANGE_PostReservesPurseHandle *prph,
  unsigned int num_options,
  const struct TALER_EXCHANGE_PostReservesPurseOptionValue options[]);


/**
 * Set the requested options for the operation.
 *
 * If any option fails, other options may or may not be applied.
 *
 * @param prph the request to set the options for
 * @param ... the list of the options, each option must be created
 *            by helpers TALER_EXCHANGE_post_reserves_purse_option_NAME(VALUE)
 * @return #GNUNET_OK on success,
 *         #GNUNET_NO on failure,
 *         #GNUNET_SYSERR on internal error
 */
#define TALER_EXCHANGE_post_reserves_purse_set_options(prph,...)          \
        TALER_EXCHANGE_post_reserves_purse_set_options_ (                 \
          prph,                                                           \
          TALER_EXCHANGE_COMMON_OPTIONS_ARRAY_MAX_SIZE,                   \
          ((const struct TALER_EXCHANGE_PostReservesPurseOptionValue[])   \
           {__VA_ARGS__,                                                  \
            TALER_EXCHANGE_post_reserves_purse_option_end_ ()}            \
          ))


/**
 * Response generated for a purse creation request via reserve.
 */
struct TALER_EXCHANGE_PostReservesPurseResponse
{
  /**
   * Full HTTP response.
   */
  struct TALER_EXCHANGE_HttpResponse hr;

  /**
   * Reserve signature generated for the request
   * (client-side).
   */
  const struct TALER_ReserveSignatureP *reserve_sig;

  /**
   * Details depending on the HTTP status.
   */
  union
  {
    /**
     * Details returned on #MHD_HTTP_OK.
     */
    struct
    {

      /**
       * Signature by the exchange affirming the purse creation.
       */
      struct TALER_ExchangeSignatureP exchange_sig;

      /**
       * Online signing key used by the exchange.
       */
      struct TALER_ExchangePublicKeyP exchange_pub;

      /**
       * Timestamp of the exchange for @e exchange_sig.
       */
      struct GNUNET_TIME_Timestamp exchange_timestamp;

      /**
       * Amount deposited (excluding deposit fees).
       */
      struct TALER_Amount total_deposited;

    } ok;

    /**
     * Details if the status is #MHD_HTTP_UNAVAILABLE_FOR_LEGAL_REASONS.
     */
    struct TALER_EXCHANGE_KycNeededRedirect unavailable_for_legal_reasons;

  } details;

};


#ifndef TALER_EXCHANGE_POST_RESERVES_PURSE_RESULT_CLOSURE
/**
 * Type of the closure used by
 * the #TALER_EXCHANGE_PostReservesPurseCallback.
 */
#define TALER_EXCHANGE_POST_RESERVES_PURSE_RESULT_CLOSURE void
#endif /* TALER_EXCHANGE_POST_RESERVES_PURSE_RESULT_CLOSURE */

/**
 * Type of the function that receives the result of a
 * POST /reserves/$RESERVE_PUB/purse request.
 *
 * @param cls closure
 * @param result result returned by the HTTP server
 */
typedef void
(*TALER_EXCHANGE_PostReservesPurseCallback)(
  TALER_EXCHANGE_POST_RESERVES_PURSE_RESULT_CLOSURE *cls,
  const struct TALER_EXCHANGE_PostReservesPurseResponse *result);


/**
 * Start POST /reserves/$RESERVE_PUB/purse operation.
 *
 * @param[in,out] prph operation to start
 * @param cb function to call with the exchange's result
 * @param cb_cls closure for @a cb
 * @return status code, #TALER_EC_NONE on success
 */
enum TALER_ErrorCode
TALER_EXCHANGE_post_reserves_purse_start (
  struct TALER_EXCHANGE_PostReservesPurseHandle *prph,
  TALER_EXCHANGE_PostReservesPurseCallback cb,
  TALER_EXCHANGE_POST_RESERVES_PURSE_RESULT_CLOSURE *cb_cls);


/**
 * Cancel POST /reserves/$RESERVE_PUB/purse operation.  This function must not
 * be called by clients after the TALER_EXCHANGE_PostReservesPurseCallback has
 * been invoked (as in those cases it'll be called internally by the
 * implementation already).
 *
 * @param[in] prph operation to cancel
 */
void
TALER_EXCHANGE_post_reserves_purse_cancel (
  struct TALER_EXCHANGE_PostReservesPurseHandle *prph);


#endif /* _TALER_EXCHANGE__POST_RESERVES_RESERVE_PUB_PURSE_H */
