/*
  This file is part of TALER
  Copyright (C) 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-wads-WAD_ID.h
 * @brief C interface for GET /wads/$WAD_ID
 * @author Christian Grothoff
 */
#ifndef _TALER_EXCHANGE__GET_WADS_WAD_ID_H
#define _TALER_EXCHANGE__GET_WADS_WAD_ID_H

#include <taler/exchange/common.h>

/**
 * Handle for an operation to GET /wads/$WAD_ID.
 */
struct TALER_EXCHANGE_GetWadsHandle;


/**
 * A single item in the wad (one purse that was merged into the wad).
 */
struct TALER_EXCHANGE_GetWadsWadItem
{
  /**
   * Amount in the purse.
   */
  struct TALER_Amount amount;

  /**
   * Normalized payto URI of the account the purse is to be merged into.
   * Must be of the form: 'payto://taler/EXCHANGE_URL/RESERVE_PUB'.
   */
  const char *payto_uri;

  /**
   * Purse public key.
   */
  struct TALER_PurseContractPublicKeyP purse_pub;

  /**
   * Hash of the contract.
   */
  struct TALER_PrivateContractHashP h_contract;

  /**
   * Indicative time by which the purse should expire if not paid.
   */
  struct GNUNET_TIME_Timestamp purse_expiration;

  /**
   * Client-side timestamp of when the merge request was made.
   */
  struct GNUNET_TIME_Timestamp merge_timestamp;

  /**
   * Signature created with the reserve's private key.
   * Must be of purpose TALER_SIGNATURE_ACCOUNT_MERGE
   * and over TALER_AccountMergeSignaturePS.
   */
  struct TALER_ReserveSignatureP reserve_sig;

  /**
   * Signature created with the purse's private key.
   * Must be of purpose TALER_SIGNATURE_PURSE_MERGE
   * and over TALER_PurseMergeSignaturePS.
   */
  struct TALER_PurseContractSignatureP purse_sig;

  /**
   * Deposit fees charged to the purse.
   */
  struct TALER_Amount deposit_fees;

  /**
   * Wad fees charged to the purse.
   */
  struct TALER_Amount wad_fees;

};


/**
 * Response from a GET /wads/$WAD_ID request.
 */
struct TALER_EXCHANGE_GetWadsResponse
{
  /**
   * HTTP response data.
   */
  struct TALER_EXCHANGE_HttpResponse hr;

  /**
   * Response details depending on HTTP status.
   */
  union
  {
    /**
     * Details if HTTP status is #MHD_HTTP_OK.
     */
    struct
    {
      /**
       * Total transfer amount claimed by the exchange.
       */
      struct TALER_Amount total;

      /**
       * Indicative time when the wad was handed to the bank.
       */
      struct GNUNET_TIME_Timestamp wad_execution_time;

      /**
       * Array of transfers (purses) aggregated in the wad.
       */
      const struct TALER_EXCHANGE_GetWadsWadItem *items;

      /**
       * Length of the @e items array.
       */
      size_t items_length;

      /**
       * Exchange signature over the wad data.
       * Of purpose TALER_SIGNATURE_WAD_DATA, over TALER_WadDataSignaturePS.
       */
      struct TALER_ExchangeSignatureP exchange_sig;

      /**
       * Exchange public key used to create @e exchange_sig.
       */
      struct TALER_ExchangePublicKeyP exchange_pub;

    } ok;

  } details;

};


#ifndef TALER_EXCHANGE_GET_WADS_RESULT_CLOSURE
/**
 * Type of the closure used by
 * the #TALER_EXCHANGE_GetWadsCallback.
 */
#define TALER_EXCHANGE_GET_WADS_RESULT_CLOSURE void
#endif /* TALER_EXCHANGE_GET_WADS_RESULT_CLOSURE */

/**
 * Type of the function that receives the result of a
 * GET /wads/$WAD_ID request.
 *
 * @param cls closure
 * @param result result returned by the HTTP server
 */
typedef void
(*TALER_EXCHANGE_GetWadsCallback)(
  TALER_EXCHANGE_GET_WADS_RESULT_CLOSURE *cls,
  const struct TALER_EXCHANGE_GetWadsResponse *result);


/**
 * Set up GET /wads/$WAD_ID operation.
 * Note that you must explicitly start the operation after setup.
 *
 * @param ctx the context
 * @param url base URL of the exchange
 * @param wad_id identifier of the wad to look up
 * @return handle to operation
 */
struct TALER_EXCHANGE_GetWadsHandle *
TALER_EXCHANGE_get_wads_create (
  struct GNUNET_CURL_Context *ctx,
  const char *url,
  const struct TALER_WireTransferIdentifierRawP *wad_id);


/**
 * Start GET /wads/$WAD_ID operation.
 *
 * @param[in,out] gwh 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_wads_start (
  struct TALER_EXCHANGE_GetWadsHandle *gwh,
  TALER_EXCHANGE_GetWadsCallback cb,
  TALER_EXCHANGE_GET_WADS_RESULT_CLOSURE *cb_cls);


/**
 * Cancel GET /wads/$WAD_ID operation.  This function must not be called
 * by clients after the TALER_EXCHANGE_GetWadsCallback has been invoked
 * (as in those cases it'll be called internally by the
 * implementation already).
 *
 * @param[in] gwh operation to cancel
 */
void
TALER_EXCHANGE_get_wads_cancel (
  struct TALER_EXCHANGE_GetWadsHandle *gwh);


#endif /* _TALER_EXCHANGE__GET_WADS_WAD_ID_H */
