/*
  This file is part of Anastasis
  Copyright (C) 2020-2026 Anastasis SARL

  Anastasis 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.

  Anastasis 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
  Anastasis; see the file COPYING.GPL.  If not, see <http://www.gnu.org/licenses/>
*/
/**
 * @file stasis/do_insert_recdoc_payment.c
 * @brief Anastasis database: record the start of a recovery document payment
 * @author Christian Grothoff
 */
#include "platform.h"
#include "anastasis-db_pg.h"
#include "anastasis/anastasis-database/do_insert_recdoc_payment.h"
#include "anastasis/anastasis-database/preflight.h"
#include <taler/taler_pq_lib.h>


enum GNUNET_DB_QueryStatus
ANASTASIS_DB_do_insert_recdoc_payment (
  const struct ANASTASIS_CRYPTO_AccountPublicKeyP *account_pub,
  uint32_t post_counter,
  const struct ANASTASIS_PaymentSecretP *payment_secret,
  const struct TALER_Amount *amount)
{
  struct GNUNET_TIME_Timestamp now = GNUNET_TIME_timestamp_get ();
  struct GNUNET_TIME_Timestamp transient_expiration
    = GNUNET_TIME_relative_to_timestamp (TRANSIENT_LIFETIME);
  struct GNUNET_PQ_QueryParam params[] = {
    GNUNET_PQ_query_param_auto_from_type (account_pub),
    GNUNET_PQ_query_param_uint32 (&post_counter),
    TALER_PQ_query_param_amount (pg->conn,
                                 amount),
    GNUNET_PQ_query_param_auto_from_type (payment_secret),
    GNUNET_PQ_query_param_timestamp (&now),
    GNUNET_PQ_query_param_timestamp (&transient_expiration),
    GNUNET_PQ_query_param_end
  };

  GNUNET_break (GNUNET_OK ==
                ANASTASIS_DB_preflight ());
  /* Creating the account and recording the payment happen in one statement:
     they must not be separated, since the payment row references the account
     and two concurrent first uploads would otherwise collide. */
  PREPARE ("do_insert_recdoc_payment",
           "SELECT anastasis_do_insert_recdoc_payment"
           " ($1, $2, $3, $4, $5, $6);");
  return GNUNET_PQ_eval_prepared_non_select (pg->conn,
                                             "do_insert_recdoc_payment",
                                             params);
}


/* end of do_insert_recdoc_payment.c */
