/*
  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/get_latest_recovery_document.c
 * @brief Anastasis database: get latest recovery document
 * @author Christian Grothoff
 */
#include "platform.h"
#include "anastasis-db_pg.h"
#include "anastasis/anastasis-database/get_latest_recovery_document.h"
#include "anastasis/anastasis-database/transaction.h"
#include "anastasis/anastasis-database/preflight.h"
#include <taler/taler_pq_lib.h>


/**
 * Fetch latest recovery document for user.
 *
 * @param account_pub public key of the user's account
 * @param account_sig signature
 * @param recovery_data_hash hash of the current recovery data
 * @param data_size size of data blob
 * @param data blob which contains the recovery document
 * @param[out] version set to the version number of the policy being returned
 * @return transaction status
 */
enum GNUNET_DB_QueryStatus
ANASTASIS_DB_get_latest_recovery_document (
  const struct ANASTASIS_CRYPTO_AccountPublicKeyP *account_pub,
  struct ANASTASIS_AccountSignatureP *account_sig,
  struct GNUNET_HashCode *recovery_data_hash,
  size_t *data_size,
  void **data,
  uint32_t *version)
{
  struct GNUNET_PQ_QueryParam params[] = {
    GNUNET_PQ_query_param_auto_from_type (account_pub),
    GNUNET_PQ_query_param_end
  };
  struct GNUNET_PQ_ResultSpec rs[] = {
    GNUNET_PQ_result_spec_uint32 ("version",
                                  version),
    GNUNET_PQ_result_spec_auto_from_type ("account_sig",
                                          account_sig),
    GNUNET_PQ_result_spec_auto_from_type ("recovery_data_hash",
                                          recovery_data_hash),
    GNUNET_PQ_result_spec_variable_size ("recovery_data",
                                         data,
                                         data_size),
    GNUNET_PQ_result_spec_end
  };

  GNUNET_break (GNUNET_OK ==
                ANASTASIS_DB_preflight ());
  PREPARE ("get_latest_recovery_document",
           "SELECT "
           " version"
           ",account_sig"
           ",recovery_data_hash"
           ",recovery_data"
           " FROM anastasis_recoverydocument"
           " WHERE user_id=$1"
           " ORDER BY version DESC"
           " LIMIT 1;");
  return GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
                                                   "get_latest_recovery_document",
                                                   params,
                                                   rs);
}


/* end of get_latest_recovery_document.c */
