/*
   This file is part of TALER
   Copyright (C) 2022 Taler Systems SA

   TALER is free software; you can redistribute it and/or modify it under the
   terms of the GNU 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 General Public License for more details.

   You should have received a copy of the GNU General Public License along with
   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
/**
 * @file iterate_reserve_closed_above_serial_id.h
 * @brief implementation of the select_reserve_closed_above_serial_id function
 * @author Christian Grothoff
 */
#ifndef EXCHANGE_DATABASE_ITERATE_RESERVE_CLOSED_ABOVE_SERIAL_ID_H
#define EXCHANGE_DATABASE_ITERATE_RESERVE_CLOSED_ABOVE_SERIAL_ID_H

#include "taler/taler_util.h"
#include "taler/taler_json_lib.h"
#include "exchangedb_lib.h"


/**
 * Function called about reserve closing operations
 * the aggregator triggered.
 *
 * @param cls closure
 * @param rowid row identifier used to uniquely identify the reserve closing operation
 * @param execution_date when did we execute the close operation
 * @param amount_with_fee how much did we debit the reserve
 * @param closing_fee how much did we charge for closing the reserve
 * @param reserve_pub public key of the reserve
 * @param receiver_account where did we send the funds, in payto://-format
 * @param wtid identifier used for the wire transfer
 * @param close_request_row row with the responsible close
 *            request, 0 if regular expiration triggered close
 * @return #GNUNET_OK to continue to iterate, #GNUNET_SYSERR to stop
 */
#ifndef TALER_EXCHANGEDB_RESERVE_CLOSED_RESULT_CLOSURE
/**
 * Type of the closure for #TALER_EXCHANGEDB_ReserveClosedCallback.
 */
#define TALER_EXCHANGEDB_RESERVE_CLOSED_RESULT_CLOSURE void
#endif
typedef enum GNUNET_GenericReturnValue
(*TALER_EXCHANGEDB_ReserveClosedCallback)(
  TALER_EXCHANGEDB_RESERVE_CLOSED_RESULT_CLOSURE *cls,
  uint64_t rowid,
  struct GNUNET_TIME_Timestamp execution_date,
  const struct TALER_Amount *amount_with_fee,
  const struct TALER_Amount *closing_fee,
  const struct TALER_ReservePublicKeyP *reserve_pub,
  const struct TALER_FullPayto receiver_account,
  const struct TALER_WireTransferIdentifierRawP *wtid,
  uint64_t close_request_row);


/* Callback typedefs */
/**
 * Function called about reserve closing operations
 * the aggregator triggered.
 *
 * @param cls closure
 * @param rowid row identifier used to uniquely identify the reserve closing operation
 * @param execution_date when did we execute the close operation
 * @param amount_with_fee how much did we debit the reserve
 * @param closing_fee how much did we charge for closing the reserve
 * @param reserve_pub public key of the reserve
 * @param receiver_account where did we send the funds, in payto://-format
 * @param wtid identifier used for the wire transfer
 * @param close_request_row row with the responsible close
 *            request, 0 if regular expiration triggered close
 * @return #GNUNET_OK to continue to iterate, #GNUNET_SYSERR to stop
 */
typedef enum GNUNET_GenericReturnValue
(*TALER_EXCHANGEDB_ReserveClosedCallback)(
  TALER_EXCHANGEDB_RESERVE_CLOSED_RESULT_CLOSURE *cls,
  uint64_t rowid,
  struct GNUNET_TIME_Timestamp execution_date,
  const struct TALER_Amount *amount_with_fee,
  const struct TALER_Amount *closing_fee,
  const struct TALER_ReservePublicKeyP *reserve_pub,
  const struct TALER_FullPayto receiver_account,
  const struct TALER_WireTransferIdentifierRawP *wtid,
  uint64_t close_request_row);

/**
 * Function called to select reserve close operations the aggregator
 * triggered, ordered by serial ID (monotonically increasing).
 *
 * @param pg the database context
 * @param serial_id lowest serial ID to include (select larger or equal)
 * @param cb function to call for ONE unfinished item
 * @param cb_cls closure for @a cb
 * @return transaction status code
 */
enum GNUNET_DB_QueryStatus
TALER_EXCHANGEDB_iterate_reserve_closed_above_serial_id (struct
                                                         TALER_EXCHANGEDB_PostgresContext
                                                         *
                                                         pg,
                                                         uint64_t serial_id,
                                                         TALER_EXCHANGEDB_ReserveClosedCallback
                                                         cb,
                                                         TALER_EXCHANGEDB_RESERVE_CLOSED_RESULT_CLOSURE
                                                         *cb_cls);


#endif
