/*
  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/post-reserves-RESERVE_PUB-close.h
 * @brief C interface for POST /reserves/$RESERVE_PUB/close
 * @author Christian Grothoff
 */
#ifndef _TALER_EXCHANGE__POST_RESERVES_RESERVE_PUB_CLOSE_H
#define _TALER_EXCHANGE__POST_RESERVES_RESERVE_PUB_CLOSE_H

#include <taler/exchange/common.h>


/**
 * Possible options we can set for the POST /reserves/$RESERVE_PUB/close request.
 */
enum TALER_EXCHANGE_PostReservesCloseOption
{
  /**
   * End of list of options.
   */
  TALER_EXCHANGE_POST_RESERVES_CLOSE_OPTION_END = 0,

  /**
   * Target payto URI to send the reserve balance to.
   * If not set, the exchange will use the reserve's origin account.
   */
  TALER_EXCHANGE_POST_RESERVES_CLOSE_OPTION_PAYTO_URI

};


/**
 * Value for an option we can set for the POST /reserves/$RESERVE_PUB/close request.
 */
struct TALER_EXCHANGE_PostReservesCloseOptionValue
{
  /**
   * Type of the option being set.
   */
  enum TALER_EXCHANGE_PostReservesCloseOption option;

  /**
   * Specific option value.
   */
  union
  {
    /**
     * Value if @e option is
     * #TALER_EXCHANGE_POST_RESERVES_CLOSE_OPTION_PAYTO_URI.
     */
    struct TALER_FullPayto payto_uri;

  } details;

};


/**
 * Handle for an operation to POST /reserves/$RESERVE_PUB/close.
 */
struct TALER_EXCHANGE_PostReservesCloseHandle;


/**
 * Set up POST /reserves/$RESERVE_PUB/close operation.
 * Note that you must explicitly start the operation after setup.
 *
 * @param ctx curl context
 * @param url exchange base URL
 * @param reserve_priv private key of the reserve to close
 * @return handle to operation, NULL on error
 */
struct TALER_EXCHANGE_PostReservesCloseHandle *
TALER_EXCHANGE_post_reserves_close_create (
  struct GNUNET_CURL_Context *ctx,
  const char *url,
  const struct TALER_ReservePrivateKeyP *reserve_priv);


/**
 * Terminate the list of options.
 *
 * @return the terminating object of struct TALER_EXCHANGE_PostReservesCloseOptionValue
 */
#define TALER_EXCHANGE_post_reserves_close_option_end_()              \
        (const struct TALER_EXCHANGE_PostReservesCloseOptionValue)    \
        {                                                             \
          .option = TALER_EXCHANGE_POST_RESERVES_CLOSE_OPTION_END    \
        }

/**
 * Set the target payto URI to send the reserve balance to.
 *
 * @param p the target payto URI
 * @return representation of the option as a struct TALER_EXCHANGE_PostReservesCloseOptionValue
 */
#define TALER_EXCHANGE_post_reserves_close_option_payto_uri(p)           \
        (const struct TALER_EXCHANGE_PostReservesCloseOptionValue)        \
        {                                                                 \
          .option = TALER_EXCHANGE_POST_RESERVES_CLOSE_OPTION_PAYTO_URI, \
          .details.payto_uri = (p)                                        \
        }


/**
 * Set the requested options for the operation.
 *
 * If any option fails, other options may or may not be applied.
 *
 * @param prch 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_post_reserves_close_set_options_ (
  struct TALER_EXCHANGE_PostReservesCloseHandle *prch,
  unsigned int num_options,
  const struct TALER_EXCHANGE_PostReservesCloseOptionValue options[]);


/**
 * Set the requested options for the operation.
 *
 * If any option fails, other options may or may not be applied.
 *
 * @param prch the request to set the options for
 * @param ... the list of the options, each option must be created
 *            by helpers TALER_EXCHANGE_post_reserves_close_option_NAME(VALUE)
 * @return #GNUNET_OK on success,
 *         #GNUNET_NO on failure,
 *         #GNUNET_SYSERR on internal error
 */
#define TALER_EXCHANGE_post_reserves_close_set_options(prch,...)          \
        TALER_EXCHANGE_post_reserves_close_set_options_ (                 \
          prch,                                                           \
          TALER_EXCHANGE_COMMON_OPTIONS_ARRAY_MAX_SIZE,                   \
          ((const struct TALER_EXCHANGE_PostReservesCloseOptionValue[])   \
           {__VA_ARGS__,                                                  \
            TALER_EXCHANGE_post_reserves_close_option_end_ ()}            \
          ))


/**
 * Response from a POST /reserves/$RESERVE_PUB/close request.
 */
struct TALER_EXCHANGE_PostReservesCloseResponse
{
  /**
   * High-level HTTP response details.
   */
  struct TALER_EXCHANGE_HttpResponse hr;

  /**
   * Details depending on @e hr.http_status.
   */
  union
  {
    /**
     * Information returned on success, if
     * @e hr.http_status is #MHD_HTTP_OK
     */
    struct
    {
      /**
       * Amount wired to the target account.
       */
      struct TALER_Amount wire_amount;
    } ok;

    /**
     * Details if the status is #MHD_HTTP_UNAVAILABLE_FOR_LEGAL_REASONS.
     */
    struct TALER_EXCHANGE_KycNeededRedirect unavailable_for_legal_reasons;

  } details;

};


#ifndef TALER_EXCHANGE_POST_RESERVES_CLOSE_RESULT_CLOSURE
/**
 * Type of the closure used by
 * the #TALER_EXCHANGE_PostReservesCloseCallback.
 */
#define TALER_EXCHANGE_POST_RESERVES_CLOSE_RESULT_CLOSURE void
#endif /* TALER_EXCHANGE_POST_RESERVES_CLOSE_RESULT_CLOSURE */

/**
 * Type of the function that receives the result of a
 * POST /reserves/$RESERVE_PUB/close request.
 *
 * @param cls closure
 * @param result result returned by the HTTP server
 */
typedef void
(*TALER_EXCHANGE_PostReservesCloseCallback)(
  TALER_EXCHANGE_POST_RESERVES_CLOSE_RESULT_CLOSURE *cls,
  const struct TALER_EXCHANGE_PostReservesCloseResponse *result);


/**
 * Start POST /reserves/$RESERVE_PUB/close operation.
 *
 * @param[in,out] prch 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_post_reserves_close_start (
  struct TALER_EXCHANGE_PostReservesCloseHandle *prch,
  TALER_EXCHANGE_PostReservesCloseCallback cb,
  TALER_EXCHANGE_POST_RESERVES_CLOSE_RESULT_CLOSURE *cb_cls);


/**
 * Cancel POST /reserves/$RESERVE_PUB/close operation.  This function must not
 * be called by clients after the TALER_EXCHANGE_PostReservesCloseCallback has
 * been invoked (as in those cases it'll be called internally by the
 * implementation already).
 *
 * @param[in] prch operation to cancel
 */
void
TALER_EXCHANGE_post_reserves_close_cancel (
  struct TALER_EXCHANGE_PostReservesCloseHandle *prch);


#endif /* _TALER_EXCHANGE__POST_RESERVES_RESERVE_PUB_CLOSE_H */
