/*
  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/insert_truth.c
 * @brief Anastasis database: insert truth
 * @author Christian Grothoff
 */
#include "platform.h"
#include "anastasis-db_pg.h"
#include "anastasis/anastasis-database/insert_truth.h"
#include "anastasis/anastasis-database/transaction.h"
#include "anastasis/anastasis-database/preflight.h"
#include <taler/taler_pq_lib.h>


/**
 * Upload Truth, which contains the Truth and the KeyShare.
 *
 * @param truth_uuid the identifier for the Truth
 * @param key_share_data contains information of an EncryptedKeyShare
 * @param mime_type presumed mime type of data in @a encrypted_truth
 * @param encrypted_truth contains the encrypted Truth which includes the ground truth i.e. H(challenge answer), phonenumber, SMS
 * @param encrypted_truth_size the size of the Truth
 * @param method name of method
 * @param truth_expiration time till the according data will be stored
 * @return transaction status
 */
enum GNUNET_DB_QueryStatus
ANASTASIS_DB_insert_truth (
  const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid,
  const struct ANASTASIS_CRYPTO_EncryptedKeyShareP *key_share_data,
  const char *mime_type,
  const void *encrypted_truth,
  size_t encrypted_truth_size,
  const char *method,
  struct GNUNET_TIME_Relative truth_expiration)
{
  struct GNUNET_TIME_Timestamp expiration;
  struct GNUNET_PQ_QueryParam params[] = {
    GNUNET_PQ_query_param_auto_from_type (truth_uuid),
    GNUNET_PQ_query_param_auto_from_type (key_share_data),
    GNUNET_PQ_query_param_string (method),
    GNUNET_PQ_query_param_fixed_size (encrypted_truth,
                                      encrypted_truth_size),
    GNUNET_PQ_query_param_string (mime_type),
    GNUNET_PQ_query_param_timestamp (&expiration),
    GNUNET_PQ_query_param_end
  };

  expiration = GNUNET_TIME_relative_to_timestamp (truth_expiration);
  PREPARE ("insert_truth",
           "INSERT INTO anastasis_truth "
           "(truth_uuid"
           ",key_share_data"
           ",method_name"
           ",encrypted_truth"
           ",truth_mime"
           ",expiration"
           ") VALUES "
           "($1, $2, $3, $4, $5, $6);");
  return GNUNET_PQ_eval_prepared_non_select (pg->conn,
                                             "insert_truth",
                                             params);
}


/* end of insert_truth.c */
