/*
  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/get-purses-PURSE_PUB-merge.h
 * @brief C interface for GET /purses/$PURSE_PUB/merge
 * @author Christian Grothoff
 */
#ifndef _TALER_EXCHANGE__GET_PURSES_PURSE_PUB_MERGE_H
#define _TALER_EXCHANGE__GET_PURSES_PURSE_PUB_MERGE_H

#include <taler/exchange/common.h>

/**
 * Possible options we can set for the GET purses/merge request.
 */
enum TALER_EXCHANGE_GetPursesOption
{
  /**
   * End of list of options.
   */
  TALER_EXCHANGE_GET_PURSES_OPTION_END = 0,

  /**
   * How long to wait for a change to happen, enabling long polling.
   * Default is zero (no long polling).
   */
  TALER_EXCHANGE_GET_PURSES_OPTION_TIMEOUT,

  /**
   * If set to true, wait for a merge event (the purse becoming merged).
   * If set to false (the default), wait for a deposit event
   * (sufficient funds being deposited into the purse).
   */
  TALER_EXCHANGE_GET_PURSES_OPTION_WAIT_FOR_MERGE

};


/**
 * Value for an option for the GET purses/merge request.
 */
struct TALER_EXCHANGE_GetPursesOptionValue
{
  /**
   * Type of the option being set.
   */
  enum TALER_EXCHANGE_GetPursesOption option;

  /**
   * Specific option value.
   */
  union
  {
    /**
     * Value if @e option is TALER_EXCHANGE_GET_PURSES_OPTION_TIMEOUT.
     */
    struct GNUNET_TIME_Relative timeout;

    /**
     * Value if @e option is TALER_EXCHANGE_GET_PURSES_OPTION_WAIT_FOR_MERGE.
     * True to wait for merge, false to wait for deposit (default).
     */
    bool wait_for_merge;

  } details;

};


/**
 * Handle for an operation to GET /purses/$PURSE_PUB/merge.
 */
struct TALER_EXCHANGE_GetPursesHandle;


/**
 * Set up GET /purses/$PURSE_PUB/merge operation.
 * Note that you must explicitly start the operation after
 * possibly setting options.
 *
 * @param ctx the context
 * @param url base URL of the exchange
 * @param keys exchange keys for signature verification
 * @param purse_pub public key of the purse to query
 * @return handle to operation
 */
struct TALER_EXCHANGE_GetPursesHandle *
TALER_EXCHANGE_get_purses_create (
  struct GNUNET_CURL_Context *ctx,
  const char *url,
  struct TALER_EXCHANGE_Keys *keys,
  const struct TALER_PurseContractPublicKeyP *purse_pub);


/**
 * Terminate the list of options.
 *
 * @return the terminating object of struct TALER_EXCHANGE_GetPursesOptionValue
 */
#define TALER_EXCHANGE_get_purses_option_end_()                   \
        (const struct TALER_EXCHANGE_GetPursesOptionValue)        \
        {                                                          \
          .option = TALER_EXCHANGE_GET_PURSES_OPTION_END          \
        }

/**
 * Set long-polling timeout.
 *
 * @param t how long to wait for a change to happen
 * @return representation of the option as a struct TALER_EXCHANGE_GetPursesOptionValue
 */
#define TALER_EXCHANGE_get_purses_option_timeout(t)                   \
        (const struct TALER_EXCHANGE_GetPursesOptionValue)            \
        {                                                              \
          .option = TALER_EXCHANGE_GET_PURSES_OPTION_TIMEOUT,         \
          .details.timeout = (t)                                       \
        }

/**
 * Set whether to wait for a merge event (true) or a deposit event (false).
 *
 * @param w true to wait for merge, false to wait for deposit
 * @return representation of the option as a struct TALER_EXCHANGE_GetPursesOptionValue
 */
#define TALER_EXCHANGE_get_purses_option_wait_for_merge(w)               \
        (const struct TALER_EXCHANGE_GetPursesOptionValue)               \
        {                                                                 \
          .option = TALER_EXCHANGE_GET_PURSES_OPTION_WAIT_FOR_MERGE,     \
          .details.wait_for_merge = (w)                                   \
        }


/**
 * Set the requested options for the operation.
 *
 * If any option fails, other options may or may not be applied.
 *
 * @param gph 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_get_purses_set_options_ (
  struct TALER_EXCHANGE_GetPursesHandle *gph,
  unsigned int num_options,
  const struct TALER_EXCHANGE_GetPursesOptionValue *options);


/**
 * Set the requested options for the operation.
 *
 * If any option fails, other options may or may not be applied.
 *
 * It should be used with helpers that create required options, for example:
 *
 * TALER_EXCHANGE_get_purses_set_options (
 *   gph,
 *   TALER_EXCHANGE_get_purses_option_timeout (timeout),
 *   TALER_EXCHANGE_get_purses_option_wait_for_merge (true));
 *
 * @param gph the request to set the options for
 * @param ... the list of options, each created by a
 *            TALER_EXCHANGE_get_purses_option_NAME(VALUE) helper
 * @return #GNUNET_OK on success,
 *         #GNUNET_NO on failure,
 *         #GNUNET_SYSERR on internal error
 */
#define TALER_EXCHANGE_get_purses_set_options(gph,...)              \
        TALER_EXCHANGE_get_purses_set_options_ (                    \
          gph,                                                       \
          TALER_EXCHANGE_COMMON_OPTIONS_ARRAY_MAX_SIZE,             \
          ((const struct TALER_EXCHANGE_GetPursesOptionValue[])     \
           {__VA_ARGS__, TALER_EXCHANGE_get_purses_option_end_ () } \
          ))


/**
 * Response from a GET /purses/$PURSE_PUB/merge request.
 */
struct TALER_EXCHANGE_GetPursesResponse
{
  /**
   * HTTP response data.
   */
  struct TALER_EXCHANGE_HttpResponse hr;

  /**
   * Details depending on the HTTP status.
   */
  union
  {
    /**
     * Response on #MHD_HTTP_OK.
     */
    struct
    {
      /**
       * Time when the purse was merged (or zero if not merged yet).
       */
      struct GNUNET_TIME_Timestamp merge_timestamp;

      /**
       * Time when the full amount was deposited into the purse
       * (or zero if a sufficient amount was not yet deposited).
       */
      struct GNUNET_TIME_Timestamp deposit_timestamp;

      /**
       * Reserve balance (total deposited minus deposit fees).
       */
      struct TALER_Amount balance;

      /**
       * Time when the purse will expire.
       */
      struct GNUNET_TIME_Timestamp purse_expiration;

    } ok;

  } details;

};


#ifndef TALER_EXCHANGE_GET_PURSES_RESULT_CLOSURE
/**
 * Type of the closure used by
 * the #TALER_EXCHANGE_GetPursesCallback.
 */
#define TALER_EXCHANGE_GET_PURSES_RESULT_CLOSURE void
#endif /* TALER_EXCHANGE_GET_PURSES_RESULT_CLOSURE */

/**
 * Type of the function that receives the result of a
 * GET /purses/$PURSE_PUB/merge request.
 *
 * @param cls closure
 * @param result result returned by the HTTP server
 */
typedef void
(*TALER_EXCHANGE_GetPursesCallback)(
  TALER_EXCHANGE_GET_PURSES_RESULT_CLOSURE *cls,
  const struct TALER_EXCHANGE_GetPursesResponse *result);


/**
 * Start GET /purses/$PURSE_PUB/merge operation.
 *
 * @param[in,out] gph 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_get_purses_start (
  struct TALER_EXCHANGE_GetPursesHandle *gph,
  TALER_EXCHANGE_GetPursesCallback cb,
  TALER_EXCHANGE_GET_PURSES_RESULT_CLOSURE *cb_cls);


/**
 * Cancel GET /purses/$PURSE_PUB/merge operation.  This function must not be
 * called by clients after the TALER_EXCHANGE_GetPursesCallback has been
 * invoked (as in those cases it'll be called internally by the
 * implementation already).
 *
 * @param[in] gph operation to cancel
 */
void
TALER_EXCHANGE_get_purses_cancel (
  struct TALER_EXCHANGE_GetPursesHandle *gph);


#endif /* _TALER_EXCHANGE__GET_PURSES_PURSE_PUB_MERGE_H */
