/*
  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-otp-devices-DEVICE_ID.h
 * @brief C interface for GET /private/otp-devices/$DEVICE_ID of the merchant backend
 * @author Christian Grothoff
 */
#ifndef _TALER_MERCHANT__GET_PRIVATE_OTP_DEVICES_DEVICE_ID_H
#define _TALER_MERCHANT__GET_PRIVATE_OTP_DEVICES_DEVICE_ID_H

#include <taler/merchant/common.h>


/**
 * Handle for a GET /private/otp-devices/$DEVICE_ID request.
 */
struct TALER_MERCHANT_GetPrivateOtpDeviceHandle;


/**
 * Response details for a GET /private/otp-devices/$DEVICE_ID request.
 */
struct TALER_MERCHANT_GetPrivateOtpDeviceResponse
{

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

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

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

      /**
       * Human-readable description of the OTP device.
       */
      const char *otp_device_description;

      /**
       * Current counter value of the OTP device (for HOTP).
       */
      uint64_t otp_ctr;

      /**
       * Current timestamp in seconds (for TOTP).
       */
      uint64_t otp_timestamp_s;

      /**
       * POS confirmation text with OTP codes that
       * would be returned for a purchase over the
       * amount given in the query for the respective
       * time and algorithm. NULL if the confirmation
       * could not be computed based on the query and
       * OTP algorithm.
       */
      const char *otp_code;

      /**
       * OTP algorithm used.
       */
      enum TALER_MerchantConfirmationAlgorithm otp_alg;

    } ok;

  } details;

};


/**
 * Possible options we can set for the GET /private/otp-devices/$DEVICE_ID request.
 */
enum TALER_MERCHANT_GetPrivateOtpDeviceOption
{
  /**
   * End of list of options.
   */
  TALER_MERCHANT_GET_PRIVATE_OTP_DEVICE_OPTION_END = 0,

  /**
   * Set a fake time for OTP code computation.
   */
  TALER_MERCHANT_GET_PRIVATE_OTP_DEVICE_OPTION_FAKETIME,

  /**
   * Set a price amount for OTP code computation.
   */
  TALER_MERCHANT_GET_PRIVATE_OTP_DEVICE_OPTION_PRICE

};


/**
 * Value for an option for the GET /private/otp-devices/$DEVICE_ID request.
 */
struct TALER_MERCHANT_GetPrivateOtpDeviceOptionValue
{

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

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

    /**
     * Value if @e option is
     * #TALER_MERCHANT_GET_PRIVATE_OTP_DEVICE_OPTION_FAKETIME.
     */
    struct GNUNET_TIME_Timestamp faketime;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_GET_PRIVATE_OTP_DEVICE_OPTION_PRICE.
     */
    struct TALER_Amount price;

  } details;

};


/**
 * Set up GET /private/otp-devices/$DEVICE_ID 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
 * @param otp_device_id identifier of the OTP device to retrieve
 * @return handle to operation
 */
struct TALER_MERCHANT_GetPrivateOtpDeviceHandle *
TALER_MERCHANT_get_private_otp_device_create (
  struct GNUNET_CURL_Context *ctx,
  const char *url,
  const char *otp_device_id);


/**
 * Terminate the list of the options.
 *
 * @return the terminating object of struct TALER_MERCHANT_GetPrivateOtpDeviceOptionValue
 */
#define TALER_MERCHANT_get_private_otp_device_option_end_()                  \
        (const struct TALER_MERCHANT_GetPrivateOtpDeviceOptionValue)           \
        {                                                                      \
          .option = TALER_MERCHANT_GET_PRIVATE_OTP_DEVICE_OPTION_END          \
        }

/**
 * Set a fake time for OTP code computation.
 *
 * @param t the timestamp to use
 * @return representation of the option as a struct TALER_MERCHANT_GetPrivateOtpDeviceOptionValue
 */
#define TALER_MERCHANT_get_private_otp_device_option_faketime(t)              \
        (const struct TALER_MERCHANT_GetPrivateOtpDeviceOptionValue)            \
        {                                                                       \
          .option =                                                             \
            TALER_MERCHANT_GET_PRIVATE_OTP_DEVICE_OPTION_FAKETIME,             \
          .details.faketime = (t)                                               \
        }

/**
 * Set a price amount for OTP code computation.
 *
 * @param a the amount to use
 * @return representation of the option as a struct TALER_MERCHANT_GetPrivateOtpDeviceOptionValue
 */
#define TALER_MERCHANT_get_private_otp_device_option_price(a)                 \
        (const struct TALER_MERCHANT_GetPrivateOtpDeviceOptionValue)            \
        {                                                                       \
          .option =                                                             \
            TALER_MERCHANT_GET_PRIVATE_OTP_DEVICE_OPTION_PRICE,                \
          .details.price = (a)                                                  \
        }


/**
 * Set the requested options for the operation.
 *
 * If any option fail other options may be or may be not applied.
 *
 * @param god 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_otp_device_set_options_ (
  struct TALER_MERCHANT_GetPrivateOtpDeviceHandle *god,
  unsigned int num_options,
  const struct TALER_MERCHANT_GetPrivateOtpDeviceOptionValue *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_otp_device_set_options (
 *   god,
 *   TALER_MERCHANT_get_private_otp_device_option_faketime (ts),
 *   TALER_MERCHANT_get_private_otp_device_option_price (amount));
 *
 * @param god the request to set the options for
 * @param ... the list of the options, each option must be created
 *            by helpers TALER_MERCHANT_get_private_otp_device_option_NAME(VALUE)
 * @return #GNUNET_OK on success,
 *         #GNUNET_NO on failure,
 *         #GNUNET_SYSERR on internal error
 */
#define TALER_MERCHANT_get_private_otp_device_set_options(god,...)                \
        TALER_MERCHANT_get_private_otp_device_set_options_ (                       \
          god,                                                                     \
          TALER_MERCHANT_COMMON_OPTIONS_ARRAY_MAX_SIZE,                           \
          ((const struct TALER_MERCHANT_GetPrivateOtpDeviceOptionValue[])          \
           {__VA_ARGS__, TALER_MERCHANT_get_private_otp_device_option_end_ () }   \
          ))


#ifndef TALER_MERCHANT_GET_PRIVATE_OTP_DEVICE_RESULT_CLOSURE
/**
 * Type of the closure used by
 * the #TALER_MERCHANT_GetPrivateOtpDeviceCallback.
 */
#define TALER_MERCHANT_GET_PRIVATE_OTP_DEVICE_RESULT_CLOSURE void
#endif /* TALER_MERCHANT_GET_PRIVATE_OTP_DEVICE_RESULT_CLOSURE */

/**
 * Callback for a GET /private/otp-devices/$DEVICE_ID request.
 *
 * @param cls closure
 * @param ogr response details
 */
typedef void
(*TALER_MERCHANT_GetPrivateOtpDeviceCallback)(
  TALER_MERCHANT_GET_PRIVATE_OTP_DEVICE_RESULT_CLOSURE *cls,
  const struct TALER_MERCHANT_GetPrivateOtpDeviceResponse *ogr);


/**
 * Start GET /private/otp-devices/$DEVICE_ID operation.
 *
 * @param[in,out] god 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_otp_device_start (
  struct TALER_MERCHANT_GetPrivateOtpDeviceHandle *god,
  TALER_MERCHANT_GetPrivateOtpDeviceCallback cb,
  TALER_MERCHANT_GET_PRIVATE_OTP_DEVICE_RESULT_CLOSURE *cb_cls);


/**
 * Cancel GET /private/otp-devices/$DEVICE_ID operation.  This function
 * must not be called by clients after the
 * TALER_MERCHANT_GetPrivateOtpDeviceCallback has been invoked
 * (as in those cases it'll be called internally by the
 * implementation already).
 *
 * @param[in] god operation to cancel
 */
void
TALER_MERCHANT_get_private_otp_device_cancel (
  struct TALER_MERCHANT_GetPrivateOtpDeviceHandle *god);


#endif /* _TALER_MERCHANT__GET_PRIVATE_OTP_DEVICES_DEVICE_ID_H */
