/*
  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 Lesser General Public License as published by the Free Software
  Foundation; either version 2.1, 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 Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License along with
  TALER; see the file COPYING.LGPL.  If not, see
  <http://www.gnu.org/licenses/>
*/
/**
 * @file src/include/taler/merchant/get-private-transfers.h
 * @brief C interface for the GET /private/transfers endpoint
 * @author Christian Grothoff
 */
#ifndef _TALER_MERCHANT__GET_PRIVATE_TRANSFERS_H
#define _TALER_MERCHANT__GET_PRIVATE_TRANSFERS_H

#include <taler/merchant/common.h>


/**
 * Possible options we can set for the GET /private/transfers request.
 */
enum TALER_MERCHANT_GetPrivateTransfersOption
{
  /**
   * End of list of options.
   */
  TALER_MERCHANT_GET_PRIVATE_TRANSFERS_OPTION_END = 0,

  /**
   * Filter by payto URI.
   */
  TALER_MERCHANT_GET_PRIVATE_TRANSFERS_OPTION_PAYTO_URI,

  /**
   * Filter for transfers before this time.
   */
  TALER_MERCHANT_GET_PRIVATE_TRANSFERS_OPTION_BEFORE,

  /**
   * Filter for transfers after this time.
   */
  TALER_MERCHANT_GET_PRIVATE_TRANSFERS_OPTION_AFTER,

  /**
   * Return at most N values. Negative values
   * to return in descending order, positive for ascending.
   * Default is -20.
   */
  TALER_MERCHANT_GET_PRIVATE_TRANSFERS_OPTION_LIMIT,

  /**
   * Starting transfer_serial_id for pagination.
   */
  TALER_MERCHANT_GET_PRIVATE_TRANSFERS_OPTION_OFFSET,

  /**
   * Filter by verification/expected status.
   */
  TALER_MERCHANT_GET_PRIVATE_TRANSFERS_OPTION_EXPECTED

};


/**
 * Value for an option for the GET /private/transfers request.
 */
struct TALER_MERCHANT_GetPrivateTransfersOptionValue
{

  /**
   * Type of the option being set.
   */
  enum TALER_MERCHANT_GetPrivateTransfersOption option;

  /**
   * Specific option value.
   */
  union
  {

    /**
     * Value if @e option is
     * #TALER_MERCHANT_GET_PRIVATE_TRANSFERS_OPTION_PAYTO_URI.
     */
    struct TALER_FullPayto payto_uri;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_GET_PRIVATE_TRANSFERS_OPTION_BEFORE.
     */
    struct GNUNET_TIME_Timestamp before;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_GET_PRIVATE_TRANSFERS_OPTION_AFTER.
     */
    struct GNUNET_TIME_Timestamp after;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_GET_PRIVATE_TRANSFERS_OPTION_LIMIT.
     */
    int64_t limit;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_GET_PRIVATE_TRANSFERS_OPTION_OFFSET.
     */
    uint64_t offset;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_GET_PRIVATE_TRANSFERS_OPTION_EXPECTED.
     */
    enum TALER_EXCHANGE_YesNoAll expected;

  } details;

};


/**
 * Handle for a GET /private/transfers request.
 */
struct TALER_MERCHANT_GetPrivateTransfersHandle;


/**
 * Set up GET /private/transfers operation.
 * Note that you must explicitly start the operation after
 * possibly setting options.
 *
 * @param ctx the context
 * @param url base URL of the merchant backend
 * @return handle to operation
 */
struct TALER_MERCHANT_GetPrivateTransfersHandle *
TALER_MERCHANT_get_private_transfers_create (
  struct GNUNET_CURL_Context *ctx,
  const char *url);


/**
 * Terminate the list of the options.
 *
 * @return the terminating object of struct TALER_MERCHANT_GetPrivateTransfersOptionValue
 */
#define TALER_MERCHANT_get_private_transfers_option_end_()                \
        (const struct TALER_MERCHANT_GetPrivateTransfersOptionValue)      \
        {                                                                  \
          .option = TALER_MERCHANT_GET_PRIVATE_TRANSFERS_OPTION_END       \
        }

/**
 * Set payto URI filter.
 *
 * @param p payto URI to filter by
 * @return representation of the option as a struct TALER_MERCHANT_GetPrivateTransfersOptionValue
 */
#define TALER_MERCHANT_get_private_transfers_option_payto_uri(p)              \
        (const struct TALER_MERCHANT_GetPrivateTransfersOptionValue)          \
        {                                                                      \
          .option = TALER_MERCHANT_GET_PRIVATE_TRANSFERS_OPTION_PAYTO_URI,    \
          .details.payto_uri = (p)                                             \
        }

/**
 * Set filter for transfers before the given timestamp.
 *
 * @param t timestamp threshold
 * @return representation of the option as a struct TALER_MERCHANT_GetPrivateTransfersOptionValue
 */
#define TALER_MERCHANT_get_private_transfers_option_before(t)              \
        (const struct TALER_MERCHANT_GetPrivateTransfersOptionValue)       \
        {                                                                   \
          .option = TALER_MERCHANT_GET_PRIVATE_TRANSFERS_OPTION_BEFORE,    \
          .details.before = (t)                                             \
        }

/**
 * Set filter for transfers after the given timestamp.
 *
 * @param t timestamp threshold
 * @return representation of the option as a struct TALER_MERCHANT_GetPrivateTransfersOptionValue
 */
#define TALER_MERCHANT_get_private_transfers_option_after(t)              \
        (const struct TALER_MERCHANT_GetPrivateTransfersOptionValue)      \
        {                                                                  \
          .option = TALER_MERCHANT_GET_PRIVATE_TRANSFERS_OPTION_AFTER,    \
          .details.after = (t)                                             \
        }

/**
 * Set limit on the number of results to return.
 *
 * @param l limit on the number of results to return
 * @return representation of the option as a struct TALER_MERCHANT_GetPrivateTransfersOptionValue
 */
#define TALER_MERCHANT_get_private_transfers_option_limit(l)              \
        (const struct TALER_MERCHANT_GetPrivateTransfersOptionValue)      \
        {                                                                  \
          .option = TALER_MERCHANT_GET_PRIVATE_TRANSFERS_OPTION_LIMIT,    \
          .details.limit = (l)                                             \
        }

/**
 * Set row offset from which to return results.
 *
 * @param o offset to use
 * @return representation of the option as a struct TALER_MERCHANT_GetPrivateTransfersOptionValue
 */
#define TALER_MERCHANT_get_private_transfers_option_offset(o)              \
        (const struct TALER_MERCHANT_GetPrivateTransfersOptionValue)       \
        {                                                                   \
          .option = TALER_MERCHANT_GET_PRIVATE_TRANSFERS_OPTION_OFFSET,    \
          .details.offset = (o)                                             \
        }

/**
 * Set filter on expected/verified status.
 *
 * @param e expected filter to use
 * @return representation of the option as a struct TALER_MERCHANT_GetPrivateTransfersOptionValue
 */
#define TALER_MERCHANT_get_private_transfers_option_expected(e)              \
        (const struct TALER_MERCHANT_GetPrivateTransfersOptionValue)         \
        {                                                                     \
          .option = TALER_MERCHANT_GET_PRIVATE_TRANSFERS_OPTION_EXPECTED,    \
          .details.expected = (e)                                             \
        }


/**
 * Set the requested options for the operation.
 *
 * If any option fail other options may be or may be not applied.
 *
 * @param gth 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_MERCHANT_get_private_transfers_set_options_ (
  struct TALER_MERCHANT_GetPrivateTransfersHandle *gth,
  unsigned int num_options,
  const struct TALER_MERCHANT_GetPrivateTransfersOptionValue *options);


/**
 * Set the requested options for the operation.
 *
 * If any option fail other options may be or may be not applied.
 *
 * It should be used with helpers that create required options, for example:
 *
 * TALER_MERCHANT_get_private_transfers_set_options (
 *   gth,
 *   TALER_MERCHANT_get_private_transfers_option_limit (-20),
 *   TALER_MERCHANT_get_private_transfers_option_expected (
 *     TALER_EXCHANGE_YNA_YES));
 *
 * @param gth the request to set the options for
 * @param ... the list of the options, each option must be created
 *            by helpers TALER_MERCHANT_get_private_transfers_option_NAME(VALUE)
 * @return #GNUNET_OK on success,
 *         #GNUNET_NO on failure,
 *         #GNUNET_SYSERR on internal error
 */
#define TALER_MERCHANT_get_private_transfers_set_options(gth,...)                \
        TALER_MERCHANT_get_private_transfers_set_options_ (                       \
          gth,                                                                    \
          TALER_MERCHANT_COMMON_OPTIONS_ARRAY_MAX_SIZE,                          \
          ((const struct TALER_MERCHANT_GetPrivateTransfersOptionValue[])        \
           {__VA_ARGS__, TALER_MERCHANT_get_private_transfers_option_end_ () }   \
          ))


/**
 * Transfer data entry.
 */
struct TALER_MERCHANT_GetPrivateTransfersTransferData
{

  /**
   * Total amount wired by the exchange (without wire fee).
   */
  struct TALER_Amount credit_amount;

  /**
   * Wire transfer identifier used.
   */
  struct TALER_WireTransferIdentifierRawP wtid;

  /**
   * URI of the target account.
   */
  struct TALER_FullPayto payto_uri;

  /**
   * URL of the exchange that made the transfer.
   */
  const char *exchange_url;

  /**
   * Serial number of the credit operation in the merchant backend.
   */
  uint64_t transfer_serial_id;

  /**
   * Time of the wire transfer, based on when we received
   * a confirmation for the wire transfer.
   */
  struct GNUNET_TIME_Timestamp execution_time;

  /**
   * Serial number of the expected transfer in the merchant backend,
   * or 0 if not known.
   */
  uint64_t expected_transfer_serial_id;

  /**
   * True if this wire transfer was expected.
   */
  bool expected;

};


/**
 * Response details for a GET /private/transfers request.
 */
struct TALER_MERCHANT_GetPrivateTransfersResponse
{

  /**
   * HTTP response details.
   */
  struct TALER_MERCHANT_HttpResponse hr;

  /**
   * Details depending on the HTTP status code.
   */
  union
  {

    /**
     * Details on #MHD_HTTP_OK.
     */
    struct
    {

      /**
       * Number of transfers in @a transfers.
       */
      unsigned int transfers_length;

      /**
       * Array of transfer data entries.
       */
      const struct TALER_MERCHANT_GetPrivateTransfersTransferData *transfers;

    } ok;

  } details;

};


#ifndef TALER_MERCHANT_GET_PRIVATE_TRANSFERS_RESULT_CLOSURE
/**
 * Type of the closure used by
 * the #TALER_MERCHANT_GetPrivateTransfersCallback.
 */
#define TALER_MERCHANT_GET_PRIVATE_TRANSFERS_RESULT_CLOSURE void
#endif /* TALER_MERCHANT_GET_PRIVATE_TRANSFERS_RESULT_CLOSURE */

/**
 * Callback for a GET /private/transfers request.
 *
 * @param cls closure
 * @param gtr response details
 */
typedef void
(*TALER_MERCHANT_GetPrivateTransfersCallback)(
  TALER_MERCHANT_GET_PRIVATE_TRANSFERS_RESULT_CLOSURE *cls,
  const struct TALER_MERCHANT_GetPrivateTransfersResponse *gtr);


/**
 * Start GET /private/transfers operation.
 *
 * @param[in,out] gth operation to start
 * @param cb function to call with the merchant's result
 * @param cb_cls closure for @a cb
 * @return status code, #TALER_EC_NONE on success
 */
enum TALER_ErrorCode
TALER_MERCHANT_get_private_transfers_start (
  struct TALER_MERCHANT_GetPrivateTransfersHandle *gth,
  TALER_MERCHANT_GetPrivateTransfersCallback cb,
  TALER_MERCHANT_GET_PRIVATE_TRANSFERS_RESULT_CLOSURE *cb_cls);


/**
 * Cancel GET /private/transfers operation.  This function must not be
 * called by clients after the TALER_MERCHANT_GetPrivateTransfersCallback
 * has been invoked (as in those cases it'll be called internally by
 * the implementation already).
 *
 * @param[in] gth operation to cancel
 */
void
TALER_MERCHANT_get_private_transfers_cancel (
  struct TALER_MERCHANT_GetPrivateTransfersHandle *gth);


#endif /* _TALER_MERCHANT__GET_PRIVATE_TRANSFERS_H */
