/*
  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-kyc-check-H_NORMALIZED_PAYTO.h
 * @brief C interface for GET /kyc-check/$H_NORMALIZED_PAYTO
 * @author Christian Grothoff
 */
#ifndef _TALER_EXCHANGE__GET_KYC_CHECK_H_NORMALIZED_PAYTO_H
#define _TALER_EXCHANGE__GET_KYC_CHECK_H_NORMALIZED_PAYTO_H

#include <taler/exchange/common.h>

/**
 * KYC/AML status information about an account.
 */
struct TALER_EXCHANGE_AccountKycStatus
{
  /**
   * Current AML state for the target account.  True if operations are not
   * happening due to staff processing paperwork *or* due to legal
   * requirements (so the client cannot do anything but wait).
   */
  bool aml_review;

  /**
   * Length of the @e limits array.
   */
  unsigned int limits_length;

  /**
   * Generation of this rule, matches a monotonically increasing
   * table row in the exchange with KYC rules for this account.
   */
  uint64_t rule_gen;

  /**
   * Array of (exposed) limits that apply to the account.
   * Length in @e limits_length.
   */
  const struct TALER_EXCHANGE_AccountLimit *limits;

  /**
   * Access token the user needs to start a KYC process.
   */
  struct TALER_AccountAccessTokenP access_token;

  /**
   * Set if and only if the client must next accept the
   * terms of service and KYC_SWAP_TOS_ACCEPTANCE is enabled.
   * In this case, the value given is the ETag of the /terms
   * that the customer must accept to proceed. If the customer
   * already accepted these terms, the merchant backend will
   * use this to signal the terms acceptance without requiring
   * the user to again interact with it.
   */
  const char *tos_required;

};


/**
 * Possible options we can set for the GET kyc-check request.
 */
enum TALER_EXCHANGE_GetKycCheckOption
{
  /**
   * End of list of options.
   */
  TALER_EXCHANGE_GET_KYC_CHECK_OPTION_END = 0,

  /**
   * Latest known AML/KYC rule generation, for long polling.
   * The server will only respond when the rule generation exceeds this value.
   * Default is 0 (respond immediately).
   */
  TALER_EXCHANGE_GET_KYC_CHECK_OPTION_KNOWN_RULE_GEN,

  /**
   * Long polling target: what KYC state to wait for.
   * Default is to respond immediately.
   */
  TALER_EXCHANGE_GET_KYC_CHECK_OPTION_LPT,

  /**
   * How long to wait for an answer including long polling.
   * Default is zero (no long polling).
   */
  TALER_EXCHANGE_GET_KYC_CHECK_OPTION_TIMEOUT

};


/**
 * Value for an option for the GET kyc-check request.
 */
struct TALER_EXCHANGE_GetKycCheckOptionValue
{
  /**
   * Type of the option being set.
   */
  enum TALER_EXCHANGE_GetKycCheckOption option;

  /**
   * Specific option value.
   */
  union
  {
    /**
     * Value if @e option is TALER_EXCHANGE_GET_KYC_CHECK_OPTION_KNOWN_RULE_GEN.
     */
    uint64_t known_rule_gen;

    /**
     * Value if @e option is TALER_EXCHANGE_GET_KYC_CHECK_OPTION_LPT.
     */
    enum TALER_EXCHANGE_KycLongPollTarget lpt;

    /**
     * Value if @e option is TALER_EXCHANGE_GET_KYC_CHECK_OPTION_TIMEOUT.
     */
    struct GNUNET_TIME_Relative timeout;

  } details;

};


/**
 * Handle for an operation to GET /kyc-check/$H_NORMALIZED_PAYTO.
 */
struct TALER_EXCHANGE_GetKycCheckHandle;


/**
 * Set up GET /kyc-check/$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 h_payto hash of the account the KYC check is about
 * @param pk private key to authorize the request with
 * @return handle to operation
 */
struct TALER_EXCHANGE_GetKycCheckHandle *
TALER_EXCHANGE_get_kyc_check_create (
  struct GNUNET_CURL_Context *ctx,
  const char *url,
  const struct TALER_NormalizedPaytoHashP *h_payto,
  const union TALER_AccountPrivateKeyP *pk);


/**
 * Terminate the list of options.
 *
 * @return the terminating object of struct TALER_EXCHANGE_GetKycCheckOptionValue
 */
#define TALER_EXCHANGE_get_kyc_check_option_end_()                   \
        (const struct TALER_EXCHANGE_GetKycCheckOptionValue)         \
        {                                                             \
          .option = TALER_EXCHANGE_GET_KYC_CHECK_OPTION_END          \
        }

/**
 * Set the latest known rule generation for long polling.
 *
 * @param g latest known AML decision / rule generation
 * @return representation of the option as a struct TALER_EXCHANGE_GetKycCheckOptionValue
 */
#define TALER_EXCHANGE_get_kyc_check_option_known_rule_gen(g)              \
        (const struct TALER_EXCHANGE_GetKycCheckOptionValue)               \
        {                                                                   \
          .option = TALER_EXCHANGE_GET_KYC_CHECK_OPTION_KNOWN_RULE_GEN,    \
          .details.known_rule_gen = (g)                                     \
        }

/**
 * Set the long-polling target KYC state to wait for.
 *
 * @param t long poll target
 * @return representation of the option as a struct TALER_EXCHANGE_GetKycCheckOptionValue
 */
#define TALER_EXCHANGE_get_kyc_check_option_lpt(t)                   \
        (const struct TALER_EXCHANGE_GetKycCheckOptionValue)         \
        {                                                             \
          .option = TALER_EXCHANGE_GET_KYC_CHECK_OPTION_LPT,         \
          .details.lpt = (t)                                          \
        }

/**
 * Set long-polling timeout.
 *
 * @param t how long to wait for an answer
 * @return representation of the option as a struct TALER_EXCHANGE_GetKycCheckOptionValue
 */
#define TALER_EXCHANGE_get_kyc_check_option_timeout(t)                   \
        (const struct TALER_EXCHANGE_GetKycCheckOptionValue)             \
        {                                                                 \
          .option = TALER_EXCHANGE_GET_KYC_CHECK_OPTION_TIMEOUT,         \
          .details.timeout = (t)                                          \
        }


/**
 * Set the requested options for the operation.
 *
 * If any option fails, other options may or may not be applied.
 *
 * @param gkch 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_kyc_check_set_options_ (
  struct TALER_EXCHANGE_GetKycCheckHandle *gkch,
  unsigned int num_options,
  const struct TALER_EXCHANGE_GetKycCheckOptionValue *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_kyc_check_set_options (
 *   gkch,
 *   TALER_EXCHANGE_get_kyc_check_option_known_rule_gen (last_gen),
 *   TALER_EXCHANGE_get_kyc_check_option_lpt (TALER_EXCHANGE_YNMS_NONE),
 *   TALER_EXCHANGE_get_kyc_check_option_timeout (timeout));
 *
 * @param gkch the request to set the options for
 * @param ... the list of options, each created by a
 *            TALER_EXCHANGE_get_kyc_check_option_NAME(VALUE) helper
 * @return #GNUNET_OK on success,
 *         #GNUNET_NO on failure,
 *         #GNUNET_SYSERR on internal error
 */
#define TALER_EXCHANGE_get_kyc_check_set_options(gkch,...)              \
        TALER_EXCHANGE_get_kyc_check_set_options_ (                     \
          gkch,                                                          \
          TALER_EXCHANGE_COMMON_OPTIONS_ARRAY_MAX_SIZE,                  \
          ((const struct TALER_EXCHANGE_GetKycCheckOptionValue[])        \
           {__VA_ARGS__, TALER_EXCHANGE_get_kyc_check_option_end_ () }   \
          ))


/**
 * KYC status response.
 */
struct TALER_EXCHANGE_GetKycCheckResponse
{
  /**
   * HTTP response data.
   */
  struct TALER_EXCHANGE_HttpResponse hr;

  /**
   * Details depending on @e hr.http_status.
   */
  union
  {
    /**
     * KYC is satisfied; affirmation returned by the exchange.
     * @e hr.http_status is #MHD_HTTP_OK.
     */
    struct TALER_EXCHANGE_AccountKycStatus ok;

    /**
     * KYC is required before the operation can proceed.
     * @e hr.http_status is #MHD_HTTP_ACCEPTED.
     */
    struct TALER_EXCHANGE_AccountKycStatus accepted;

    /**
     * Request was forbidden (wrong authorization key).
     * @e hr.http_status is #MHD_HTTP_FORBIDDEN.
     */
    struct
    {
      /**
       * Account public key that would have been authorized.
       */
      union TALER_AccountPublicKeyP expected_account_pub;

    } forbidden;

  } details;

};


#ifndef TALER_EXCHANGE_GET_KYC_CHECK_RESULT_CLOSURE
/**
 * Type of the closure used by
 * the #TALER_EXCHANGE_GetKycCheckCallback.
 */
#define TALER_EXCHANGE_GET_KYC_CHECK_RESULT_CLOSURE void
#endif /* TALER_EXCHANGE_GET_KYC_CHECK_RESULT_CLOSURE */

/**
 * Type of the function that receives the result of a
 * GET /kyc-check/$H_NORMALIZED_PAYTO request.
 *
 * @param cls closure
 * @param result result returned by the HTTP server
 */
typedef void
(*TALER_EXCHANGE_GetKycCheckCallback)(
  TALER_EXCHANGE_GET_KYC_CHECK_RESULT_CLOSURE *cls,
  const struct TALER_EXCHANGE_GetKycCheckResponse *result);


/**
 * Start GET /kyc-check/$H_NORMALIZED_PAYTO operation.
 *
 * @param[in,out] gkch 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_kyc_check_start (
  struct TALER_EXCHANGE_GetKycCheckHandle *gkch,
  TALER_EXCHANGE_GetKycCheckCallback cb,
  TALER_EXCHANGE_GET_KYC_CHECK_RESULT_CLOSURE *cb_cls);


/**
 * Cancel GET /kyc-check/$H_NORMALIZED_PAYTO operation.  This function must
 * not be called by clients after the TALER_EXCHANGE_GetKycCheckCallback has
 * been invoked (as in those cases it'll be called internally by the
 * implementation already).
 *
 * @param[in] gkch operation to cancel
 */
void
TALER_EXCHANGE_get_kyc_check_cancel (
  struct TALER_EXCHANGE_GetKycCheckHandle *gkch);


#endif /* _TALER_EXCHANGE__GET_KYC_CHECK_H_NORMALIZED_PAYTO_H */
