/*
  This file is part of Anastasis
  Copyright (C) 2020-2022 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/update_to_challenge_code_sent.c
 * @brief Anastasis database: update to challenge code sent
 * @author Christian Grothoff
 */
#include "platform.h"
#include "anastasis-db_pg.h"
#include "anastasis/anastasis-database/update_to_challenge_code_sent.h"
#include "anastasis/anastasis-database/transaction.h"
#include "anastasis/anastasis-database/preflight.h"
#include <taler/taler_pq_lib.h>


/**
 * Remember in the database that we successfully sent a challenge.
 *
 * @param payment_secret payment secret which the user must provide with every upload
 * @param truth_uuid the identifier for the challenge
 * @param code the challenge that was sent
 */
enum GNUNET_DB_QueryStatus
ANASTASIS_DB_update_to_challenge_code_sent (
  const struct ANASTASIS_PaymentSecretP *payment_secret,
  const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid,
  uint64_t code)
{
  enum GNUNET_DB_QueryStatus qs;

  PREPARE ("update_to_challenge_code_sent_update_code",
           "UPDATE anastasis_challengecode"
           " SET retransmission_date=$3"
           " WHERE truth_uuid=$1"
           "   AND code=$2"
           "   AND creation_date IN"
           " (SELECT creation_date"
           "    FROM anastasis_challengecode"
           "   WHERE truth_uuid=$1"
           "     AND code=$2"
           "    ORDER BY creation_date DESC"
           "     LIMIT 1);");
  PREPARE ("update_to_challenge_code_sent_dec_payment_counter",
           "UPDATE anastasis_challenge_payment"
           " SET counter=counter - 1"
           " WHERE truth_uuid=$1"
           "   AND payment_identifier=$2"
           "   AND counter > 0;");
  {
    struct GNUNET_TIME_Timestamp now;
    struct GNUNET_PQ_QueryParam params[] = {
      GNUNET_PQ_query_param_auto_from_type (truth_uuid),
      GNUNET_PQ_query_param_uint64 (&code),
      GNUNET_PQ_query_param_timestamp (&now),
      GNUNET_PQ_query_param_end
    };

    now = GNUNET_TIME_timestamp_get ();
    qs = GNUNET_PQ_eval_prepared_non_select (pg->conn,
                                             "update_to_challenge_code_sent_update_code",
                                             params);
    if (qs <= 0)
      return qs;
  }
  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "Marking challenge %llu as issued\n",
              (unsigned long long) code);
  {
    struct GNUNET_PQ_QueryParam params[] = {
      GNUNET_PQ_query_param_auto_from_type (truth_uuid),
      GNUNET_PQ_query_param_auto_from_type (payment_secret),
      GNUNET_PQ_query_param_end
    };

    qs = GNUNET_PQ_eval_prepared_non_select (pg->conn,
                                             "update_to_challenge_code_sent_dec_payment_counter",
                                             params);
    if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
      return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT; /* probably was free */
    return qs;
  }
}


/* end of update_to_challenge_code_sent.c */
