/*
   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 exchangedb/insert_aggregation_deferral.c
 * @brief Implementation of the insert_aggregation_deferral function for Postgres
 * @author Christian Grothoff
 */
#include "taler/taler_error_codes.h"
#include "taler/taler_pq_lib.h"
#include "exchange-database/insert_aggregation_deferral.h"
#include "helper.h"


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)
{
  uint32_t reason32 = (uint32_t) reason;
  struct GNUNET_PQ_QueryParam params[] = {
    GNUNET_PQ_query_param_auto_from_type (wtid),
    GNUNET_PQ_query_param_auto_from_type (h_payto),
    TALER_PQ_query_param_amount (pg->conn,
                                 amount),
    GNUNET_PQ_query_param_uint32 (&reason32),
    GNUNET_PQ_query_param_uint64 (&kyc_requirement_row),
    GNUNET_PQ_query_param_timestamp (&deferral_time),
    GNUNET_PQ_query_param_end
  };

  /* The deposit we point at is picked here rather than passed in because it
     is not a fact about the deferral at all: it exists only so that
     exchange_do_main_gc() deleting the aggregate's deposits takes the
     deferral with it.  Hence the *last* deposit to reach its wire deadline --
     that is the one whose collection means the whole aggregate is being aged
     out.  If the aggregation_tracking rows are not there yet the SELECT is
     empty and nothing is inserted, which is why the caller must run after
     TALER_EXCHANGEDB_do_aggregate(). */
  PREPARE (pg,
           "insert_aggregation_deferral",
           "INSERT INTO aggregation_deferrals"
           " (batch_deposit_serial_id"
           " ,wtid_raw"
           " ,wire_target_h_payto"
           " ,amount"
           " ,deferral_reason"
           " ,legitimization_requirement_serial_id"
           " ,deferral_time)"
           " SELECT"
           "   atr.batch_deposit_serial_id"
           "  ,$1, $2, $3, $4, $5, $6"
           "  FROM aggregation_tracking atr"
           "  JOIN batch_deposits bdep"
           "    USING (batch_deposit_serial_id)"
           "  WHERE atr.wtid_raw=$1"
           "  ORDER BY bdep.wire_deadline DESC"
           "  LIMIT 1;");
  return GNUNET_PQ_eval_prepared_non_select (pg->conn,
                                             "insert_aggregation_deferral",
                                             params);
}
