/*
   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 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 src/include/exchange-database/insert_aggregation_deferral.h
 * @brief implementation of the insert_aggregation_deferral function for Postgres
 * @author Christian Grothoff
 */
#ifndef EXCHANGE_DATABASE_INSERT_AGGREGATION_DEFERRAL_H
#define EXCHANGE_DATABASE_INSERT_AGGREGATION_DEFERRAL_H

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

/**
 * Record why the aggregator did not execute the wire transfer @a wtid that it
 * had already decided on.  Appends to `aggregation_deferrals`, which unlike
 * `aggregation_transient` is replicated to the auditor, so that an external
 * auditor sees the exchange's own account of what it is withholding and why
 * instead of having to infer it.
 *
 * The row is tied to the deposit of the aggregate that is last to reach its
 * wire deadline, so that garbage collecting the deposits also removes this
 * claim about them.  It is therefore an error to call this before the
 * `aggregation_tracking` rows for @a wtid have been written; the statement
 * inserts nothing in that case.
 *
 * @param pg the database context
 * @param wtid the wire transfer that was not executed
 * @param h_payto destination that was not paid
 * @param amount total that was withheld
 * @param reason why the transfer was not made
 * @param kyc_requirement_row legitimization measure that has to be satisfied
 *        first, or 0 if @a reason is not #TALER_EXCHANGEDB_DR_KYC
 * @param deferral_time when the decision was made
 * @return transaction status
 */
enum GNUNET_DB_QueryStatus
TALER_EXCHANGEDB_insert_aggregation_deferral (
  struct TALER_EXCHANGEDB_PostgresContext *pg,
  const struct TALER_WireTransferIdentifierRawP *wtid,
  const struct TALER_FullPaytoHashP *h_payto,
  const struct TALER_Amount *amount,
  enum TALER_EXCHANGEDB_DeferralReason reason,
  uint64_t kyc_requirement_row,
  struct GNUNET_TIME_Timestamp deferral_time);

#endif
