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

#include <taler/exchange/common.h>

/**
 * Handle for an operation to POST /reserves/$RESERVE_PUB/open.
 */
struct TALER_EXCHANGE_PostReservesOpenHandle;


/**
 * Set up POST /reserves/$RESERVE_PUB/open operation.
 * Note that you must explicitly start the operation after setup.
 *
 * @param ctx curl context
 * @param url exchange base URL
 * @param keys exchange keys
 * @param reserve_priv private key of the reserve to open
 * @param reserve_contribution amount to pay from the reserve's balance for the operation
 * @param coin_payments_length length of the @a coin_payments array
 * @param coin_payments array of coin payments to use for opening the reserve
 * @param expiration_time desired new expiration time for the reserve
 * @param min_purses minimum number of purses to allow being concurrently opened per reserve
 * @return handle to operation, NULL on error
 */
struct TALER_EXCHANGE_PostReservesOpenHandle *
TALER_EXCHANGE_post_reserves_open_create (
  struct GNUNET_CURL_Context *ctx,
  const char *url,
  struct TALER_EXCHANGE_Keys *keys,
  const struct TALER_ReservePrivateKeyP *reserve_priv,
  const struct TALER_Amount *reserve_contribution,
  unsigned int coin_payments_length,
  const struct TALER_EXCHANGE_PurseDeposit coin_payments[
    static coin_payments_length],
  struct GNUNET_TIME_Timestamp expiration_time,
  uint32_t min_purses);


/**
 * Response from a POST /reserves/$RESERVE_PUB/open request.
 */
struct TALER_EXCHANGE_PostReservesOpenResponse
{
  /**
   * 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
    {
      /**
       * New expiration time
       */
      struct GNUNET_TIME_Timestamp expiration_time;

      /**
       * Actual cost of the open operation.
       */
      struct TALER_Amount open_cost;

    } ok;

    /**
     * Information returned if the payment provided is insufficient, if
     * @e hr.http_status is #MHD_HTTP_PAYMENT_REQUIRED
     */
    struct
    {
      /**
       * Current expiration time of the reserve.
       */
      struct GNUNET_TIME_Timestamp expiration_time;

      /**
       * Actual cost of the open operation that should have been paid.
       */
      struct TALER_Amount open_cost;

    } payment_required;

    /**
     * Information returned if status is #MHD_HTTP_CONFLICT.
     */
    struct
    {
      /**
       * Public key of the coin that caused the conflict.
       */
      struct TALER_CoinSpendPublicKeyP coin_pub;

    } conflict;

    /**
     * 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_OPEN_RESULT_CLOSURE
/**
 * Type of the closure used by
 * the #TALER_EXCHANGE_PostReservesOpenCallback.
 */
#define TALER_EXCHANGE_POST_RESERVES_OPEN_RESULT_CLOSURE void
#endif /* TALER_EXCHANGE_POST_RESERVES_OPEN_RESULT_CLOSURE */

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


/**
 * Start POST /reserves/$RESERVE_PUB/open operation.
 *
 * @param[in,out] proh 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_open_start (
  struct TALER_EXCHANGE_PostReservesOpenHandle *proh,
  TALER_EXCHANGE_PostReservesOpenCallback cb,
  TALER_EXCHANGE_POST_RESERVES_OPEN_RESULT_CLOSURE *cb_cls);


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


#endif /* _TALER_EXCHANGE__POST_RESERVES_RESERVE_PUB_OPEN_H */
