/*
   This file is part of TALER
   Copyright (C) 2025, 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-aml-OFFICER_PUB-transfers-credit.h
 * @brief C interface for the GET /aml/$OFFICER_PUB/transfers-{credit,debit,kycauth} endpoints
 * @author Christian Grothoff
 */
#ifndef _TALER_EXCHANGE__GET_AML_OFFICER_PUB_TRANSFERS_CREDIT_H
#define _TALER_EXCHANGE__GET_AML_OFFICER_PUB_TRANSFERS_CREDIT_H

#include <taler/exchange/common.h>

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

  /**
   * Minimum amount threshold to return. All amounts below
   * the given threshold will be filtered.
   */
  TALER_EXCHANGE_GET_AML_TRANSFERS_OPTION_THRESHOLD,

  /**
   * Return at most N values, default is -20 to return
   * the last 20 entries before start. Negative values
   * to return before limit, positive to return after limit.
   */
  TALER_EXCHANGE_GET_AML_TRANSFERS_OPTION_LIMIT,

  /**
   * Row number threshold, defaults to INT64_MAX, namely
   * the biggest row id possible in the database.
   */
  TALER_EXCHANGE_GET_AML_TRANSFERS_OPTION_OFFSET,

  /**
   * Filter by account using a normalized payto URI hash.
   * Since protocol v29.
   */
  TALER_EXCHANGE_GET_AML_TRANSFERS_OPTION_H_PAYTO

};


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

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

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

    /**
     * Value if @e option is TALER_EXCHANGE_GET_AML_TRANSFERS_OPTION_THRESHOLD.
     */
    const struct TALER_Amount *threshold;

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

    /**
     * Value if @e option is TALER_EXCHANGE_GET_AML_TRANSFERS_OPTION_OFFSET.
     * Note that in practice the maximum value is INT64_MAX, even though
     * this value is unsigned.
     */
    uint64_t offset;

    /**
     * Value if @e option is TALER_EXCHANGE_GET_AML_TRANSFERS_OPTION_H_PAYTO.
     */
    const struct TALER_NormalizedPaytoHashP *h_payto;

  } details;

};


/**
 * Handle for an operation to GET /aml/$OFFICER_PUB/transfers-{credit,debit,kycauth}.
 */
struct TALER_EXCHANGE_GetAmlTransfersHandle;


/**
 * Set up GET /aml/$OPUB/transfers-credit 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 officer_priv private key of the officer
 * @return handle to operation
 */
struct TALER_EXCHANGE_GetAmlTransfersHandle *
TALER_EXCHANGE_get_aml_transfers_credit_create (
  struct GNUNET_CURL_Context *ctx,
  const char *url,
  const struct TALER_AmlOfficerPrivateKeyP *officer_priv);


/**
 * Set up GET /aml/$OPUB/transfers-debit 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 officer_priv private key of the officer
 * @return handle to operation
 */
struct TALER_EXCHANGE_GetAmlTransfersHandle *
TALER_EXCHANGE_get_aml_transfers_debit_create (
  struct GNUNET_CURL_Context *ctx,
  const char *url,
  const struct TALER_AmlOfficerPrivateKeyP *officer_priv);


/**
 * Set up GET /aml/$OPUB/transfers-kycauth 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 officer_priv private key of the officer
 * @return handle to operation
 */
struct TALER_EXCHANGE_GetAmlTransfersHandle *
TALER_EXCHANGE_get_aml_transfers_kycauth_create (
  struct GNUNET_CURL_Context *ctx,
  const char *url,
  const struct TALER_AmlOfficerPrivateKeyP *officer_priv);


/**
 * Terminate the list of the options.
 *
 * @return the terminating object of struct TALER_EXCHANGE_GetAmlTransfersOptionValue
 */
#define TALER_EXCHANGE_get_aml_transfers_option_end_()               \
        (const struct TALER_EXCHANGE_GetAmlTransfersOptionValue)     \
        {                                                             \
          .option = TALER_EXCHANGE_GET_AML_TRANSFERS_OPTION_END      \
        }

/**
 * Set minimum amount threshold @a t for transfers to return.
 *
 * @param t minimum amount threshold
 * @return representation of the option as a struct TALER_EXCHANGE_GetAmlTransfersOptionValue
 */
#define TALER_EXCHANGE_get_aml_transfers_option_threshold(t)              \
        (const struct TALER_EXCHANGE_GetAmlTransfersOptionValue)          \
        {                                                                  \
          .option = TALER_EXCHANGE_GET_AML_TRANSFERS_OPTION_THRESHOLD,    \
          .details.threshold = (t)                                         \
        }

/**
 * Set limit @a l 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_EXCHANGE_GetAmlTransfersOptionValue
 */
#define TALER_EXCHANGE_get_aml_transfers_option_limit(l)              \
        (const struct TALER_EXCHANGE_GetAmlTransfersOptionValue)      \
        {                                                              \
          .option = TALER_EXCHANGE_GET_AML_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_EXCHANGE_GetAmlTransfersOptionValue
 */
#define TALER_EXCHANGE_get_aml_transfers_option_offset(o)              \
        (const struct TALER_EXCHANGE_GetAmlTransfersOptionValue)       \
        {                                                               \
          .option = TALER_EXCHANGE_GET_AML_TRANSFERS_OPTION_OFFSET,    \
          .details.offset = (o)                                         \
        }


/**
 * Set filter on which account to filter transfers by.
 *
 * @param p normalized payto URI hash of the account to filter by
 * @return representation of the option as a struct TALER_EXCHANGE_GetAmlTransfersOptionValue
 */
#define TALER_EXCHANGE_get_aml_transfers_option_filter_h_payto(p)      \
        (const struct TALER_EXCHANGE_GetAmlTransfersOptionValue)        \
        {                                                               \
          .option = TALER_EXCHANGE_GET_AML_TRANSFERS_OPTION_H_PAYTO,   \
          .details.h_payto = (p)                                        \
        }


/**
 * Set the requested options for the operation.
 *
 * If any option fail other options may be or may be not applied.
 *
 * @param atgh 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_aml_transfers_set_options_ (
  struct TALER_EXCHANGE_GetAmlTransfersHandle *atgh,
  unsigned int num_options,
  const struct TALER_EXCHANGE_GetAmlTransfersOptionValue *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_EXCHANGE_get_aml_transfers_set_options (
 *   atgh,
 *   TALER_EXCHANGE_get_aml_transfers_option_filter_h_payto (&h_payto));
 *
 * @param atgh the request to set the options for
 * @param ... the list of the options, each option must be created
 *            by helpers TALER_EXCHANGE_get_aml_transfers_option_NAME(VALUE)
 * @return #GNUNET_OK on success,
 *         #GNUNET_NO on failure,
 *         #GNUNET_SYSERR on internal error
 */
#define TALER_EXCHANGE_get_aml_transfers_set_options(atgh,...)              \
        TALER_EXCHANGE_get_aml_transfers_set_options_ (                     \
          atgh,                                                             \
          TALER_EXCHANGE_COMMON_OPTIONS_ARRAY_MAX_SIZE,                     \
          ((const struct TALER_EXCHANGE_GetAmlTransfersOptionValue[])       \
           {__VA_ARGS__, TALER_EXCHANGE_get_aml_transfers_option_end_ () }  \
          ))


/**
 * Entry in the set of transfers that are returned
 * by the server in a single request.
 */
struct TALER_EXCHANGE_GetAmlTransfersEntry
{
  /**
   * Row ID of the record. Used to filter by offset.
   */
  uint64_t rowid;

  /**
   * payto://-URI of the other account.
   */
  const char *payto_uri;

  /**
   * The amount involved.
   */
  struct TALER_Amount amount;

  /**
   * Time when the transfer was made.
   */
  struct GNUNET_TIME_Timestamp execution_time;
};

/**
 * Information returned from the exchange for a
 * GET /aml/$OFFICER_PUB/transfers-{credit,debit,kycauth} request.
 */
struct TALER_EXCHANGE_GetAmlTransfersResponse
{
  /**
   * HTTP response data
   */
  struct TALER_EXCHANGE_HttpResponse hr;

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

    /**
     * Details on #MHD_HTTP_OK.
     */
    struct
    {
      /**
       * Length of the @e transfers array.
       */
      size_t transfers_length;

      /**
       * Matching transactions of the exchange.
       */
      const struct TALER_EXCHANGE_GetAmlTransfersEntry *transfers;

    } ok;

  } details;
};


#ifndef TALER_EXCHANGE_GET_AML_TRANSFERS_RESULT_CLOSURE
/**
 * Type of the closure used by
 * the #TALER_EXCHANGE_GetAmlTransfersCallback.
 */
#define TALER_EXCHANGE_GET_AML_TRANSFERS_RESULT_CLOSURE void
#endif /* TALER_EXCHANGE_GET_AML_TRANSFERS_RESULT_CLOSURE */

/**
 * Type of the function that receives the result of a
 * GET /aml/$OFFICER_PUB/transfers-{credit,debit,kycauth} request.
 *
 * @param cls closure
 * @param result result returned by the HTTP server
 */
typedef void
(*TALER_EXCHANGE_GetAmlTransfersCallback)(
  TALER_EXCHANGE_GET_AML_TRANSFERS_RESULT_CLOSURE *cls,
  const struct TALER_EXCHANGE_GetAmlTransfersResponse *result);


/**
 * Start GET /aml/$OPUB/transfers-{credit,debit,kycauth} operation.
 *
 * @param[in,out] atgh 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_aml_transfers_start (
  struct TALER_EXCHANGE_GetAmlTransfersHandle *atgh,
  TALER_EXCHANGE_GetAmlTransfersCallback cb,
  TALER_EXCHANGE_GET_AML_TRANSFERS_RESULT_CLOSURE *cb_cls);


/**
 * Cancel GET /aml/$OPUB/transfers-{credit,debit,kycauth} operation.
 * This function must not be called by clients after the
 * TALER_EXCHANGE_GetAmlTransfersCallback has been invoked
 * (as in those cases it'll be called internally by the
 * implementation already).
 *
 * @param[in] atgh operation to cancel
 */
void
TALER_EXCHANGE_get_aml_transfers_cancel (
  struct TALER_EXCHANGE_GetAmlTransfersHandle *atgh);


#endif /* _TALER_EXCHANGE__GET_AML_OFFICER_PUB_TRANSFERS_CREDIT_H */
