/*
   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-attributes-H_NORMALIZED_PAYTO.h
 * @brief C interface for the GET /aml/$OFFICER_PUB/attributes/$H_NORMALIZED_PAYTO endpoint
 * @author Christian Grothoff
 */
#ifndef _TALER_EXCHANGE__GET_AML_OFFICER_PUB_ATTRIBUTES_H_NORMALIZED_PAYTO_H
#define _TALER_EXCHANGE__GET_AML_OFFICER_PUB_ATTRIBUTES_H_NORMALIZED_PAYTO_H

#include <taler/exchange/common.h>

/**
 * Possible options we can set for the GET attributes request.
 */
enum TALER_EXCHANGE_GetAmlAttributesOption
{
  /**
   * End of list of options.
   */
  TALER_EXCHANGE_GET_AML_ATTRIBUTES_OPTION_END = 0,

  /**
   * 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_ATTRIBUTES_OPTION_LIMIT,

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

};


/**
 * Possible options we can set for the GET attributes request.
 */
struct TALER_EXCHANGE_GetAmlAttributesOptionValue
{

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

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

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

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

  } details;

};


/**
 * Handle for an operation to GET /aml/$OFFICER_PUB/attributes/$H_NORMALIZED_PAYTO.
 */
struct TALER_EXCHANGE_GetAmlAttributesHandle;


/**
 * Set up GET /aml/$OPUB/attributes/$H_NORMALIZED_PAYTO 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
 * @param h_payto normalized payto URI hash of the account to get attributes for
 * @return handle to operation
 */
struct TALER_EXCHANGE_GetAmlAttributesHandle *
TALER_EXCHANGE_get_aml_attributes_create (
  struct GNUNET_CURL_Context *ctx,
  const char *url,
  const struct TALER_AmlOfficerPrivateKeyP *officer_priv,
  const struct TALER_NormalizedPaytoHashP *h_payto);


/**
 * Terminate the list of the options.
 *
 * @return the terminating object of struct TALER_EXCHANGE_GetAmlAttributesOptionValue
 */
#define TALER_EXCHANGE_get_aml_attributes_option_end_()           \
        (const struct TALER_EXCHANGE_GetAmlAttributesOptionValue) \
        {                                                         \
          .option = TALER_EXCHANGE_GET_AML_ATTRIBUTES_OPTION_END  \
        }

/**
 * 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_GetAmlAttributesOptionValue
 */
#define TALER_EXCHANGE_get_aml_attributes_option_limit(l)           \
        (const struct TALER_EXCHANGE_GetAmlAttributesOptionValue)   \
        {                                                           \
          .option = TALER_EXCHANGE_GET_AML_ATTRIBUTES_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_GetAmlAttributesOptionValue
 */
#define TALER_EXCHANGE_get_aml_attributes_option_offset(o)           \
        (const struct TALER_EXCHANGE_GetAmlAttributesOptionValue)    \
        {                                                            \
          .option = TALER_EXCHANGE_GET_AML_ATTRIBUTES_OPTION_OFFSET, \
          .details.offset = (o)                                      \
        }


/**
 * Set the requested options for the operation.
 *
 * If any option fail other options may be or may be not applied.
 *
 * @param aagh the request to set the options for
 * @param num_options length of the @a options array
 * @param options an array of options
 * @return ::TALER_EC_NONE on success,
 *         error code otherwise
 * @return #GNUNET_OK on success,
 *         #GNUNET_NO on failure,
 *         #GNUNET_SYSERR on internal error
 */
enum GNUNET_GenericReturnValue
TALER_EXCHANGE_get_aml_attributes_set_options_ (
  struct TALER_EXCHANGE_GetAmlAttributesHandle *aagh,
  unsigned int num_options,
  const struct TALER_EXCHANGE_GetAmlAttributesOptionValue *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 creates required options, for example:
 *
 * TALER_EXCHANGE_get_aml_attributes_set_options (
 *   aagh,
 *   TALER_EXCHANGE_get_aml_attributes_option_limit_(10));
 *
 * @param aagh the request to set the options for
 * @param ... the list of the options, each option must be created
 *            by helpers TALER_EXCHANGE_get_aml_attributes_option_NAME(VALUE)
 * @return ::TALER_EC_NONE on success,
 *         error code otherwise
 */
#define TALER_EXCHANGE_get_aml_attributes_set_options(aagh,...)              \
        TALER_EXCHANGE_get_aml_attributes_set_options_ (                     \
          aagh,                                                              \
          TALER_EXCHANGE_COMMON_OPTIONS_ARRAY_MAX_SIZE,                      \
          ((const struct TALER_EXCHANGE_GetAmlAttributesOptionValue[])       \
           {__VA_ARGS__, TALER_EXCHANGE_get_aml_attributes_option_end_ () }  \
          ))


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

  /**
   * True if the attributes were filed by an AML officer,
   * false if provided directly by the customer.
   */
  bool by_aml_officer;

  /**
   * The collected KYC data. NULL if the attribute data could not
   * be decrypted (internal error of the exchange, likely the
   * attribute key was changed).
   */
  const json_t *attributes;

  /**
   * Time when the KYC data was collected.
   */
  struct GNUNET_TIME_Timestamp collection_time;
};

/**
 * Information returned from the exchange for a
 * GET /aml/$OFFICER_PUB/attributes/$H_NORMALIZED_PAYTO request.
 */
struct TALER_EXCHANGE_GetAmlAttributesResponse
{
  /**
   * 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 details array.
       */
      size_t details_length;

      /**
       * Matching KYC attribute history of the account.
       */
      const struct TALER_EXCHANGE_GetAmlAttributesCollectionEvent *details;

    } ok;

    /**
     * Details on #MHD_HTTP_NOT_IMPLEMENTED (501).
     * Returned when the requested output format (e.g. application/pdf)
     * is not supported by this exchange backend.
     */
    struct
    {
      /* no additional details */
    } not_implemented;

  } details;
};


#ifndef TALER_EXCHANGE_GET_AML_ATTRIBUTES_RESULT_CLOSURE
/**
 * Type of the closure used by
 * the #TALER_EXCHANGE_GetAmlAttributesCallback.
 */
#define TALER_EXCHANGE_GET_AML_ATTRIBUTES_RESULT_CLOSURE void
#endif \
  /* _TALER_EXCHANGE_GET_AML_OFFICER_PUB_ATTRIBUTES_H_NORMALIZED_PAYTO_H */

/**
 * Type of the function that receives the result of a
 * GET /aml/$OFFICER_PUB/attributes/$H_NORMALIZED_PAYTO request.
 *
 * @param cls closure
 * @param result result returned by the HTTP server
 */
typedef void
(*TALER_EXCHANGE_GetAmlAttributesCallback)(
  TALER_EXCHANGE_GET_AML_ATTRIBUTES_RESULT_CLOSURE *cls,
  const struct TALER_EXCHANGE_GetAmlAttributesResponse *result);


/**
 * Start GET /aml/$OPUB/attributes/$H_NORMALIZED_PAYTO operation.
 *
 * @param[in,out] aagh operation to start
 * @param cb function to call with the exchange's result
 * @param cb_cls closure for @a cb
 * @return status code
 */
enum TALER_ErrorCode
TALER_EXCHANGE_get_aml_attributes_start (
  struct TALER_EXCHANGE_GetAmlAttributesHandle *aagh,
  TALER_EXCHANGE_GetAmlAttributesCallback cb,
  TALER_EXCHANGE_GET_AML_ATTRIBUTES_RESULT_CLOSURE *cb_cls);


/**
 * Cancel GET /aml/$OPUB/attributes/$H_NORMALIZED_PAYTO operation.
 *
 * @param[in] aagh operation to cancel
 */
void
TALER_EXCHANGE_get_aml_attributes_cancel (
  struct TALER_EXCHANGE_GetAmlAttributesHandle *aagh);


#endif
/* _TALER_EXCHANGE__GET_AML_OFFICER_PUB_ATTRIBUTES_H_NORMALIZED_PAYTO_H */
