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


/**
 * Check if an account exists, and if so, return the
 * current @a recovery_document_hash.
 *
 * @param account_pub account identifier
 * @param[out] paid_until until when is the account paid up?
 * @param[out] recovery_data_hash set to hash of @a recovery document
 * @param[out] version set to the recovery policy version
 * @return transaction status
 */
enum ANASTASIS_DB_AccountStatus
ANASTASIS_DB_get_account (
  const struct ANASTASIS_CRYPTO_AccountPublicKeyP *account_pub,
  struct GNUNET_TIME_Timestamp *paid_until,
  struct GNUNET_HashCode *recovery_data_hash,
  uint32_t *version)
{
  struct GNUNET_PQ_QueryParam params[] = {
    GNUNET_PQ_query_param_auto_from_type (account_pub),
    GNUNET_PQ_query_param_end
  };
  enum GNUNET_DB_QueryStatus qs;

  PREPARE ("get_account_select_account",
           "SELECT"
           " expiration_date "
           "FROM anastasis_user"
           " WHERE user_id=$1"
           " FOR UPDATE;");
  PREPARE ("get_account_select_latest_version",
           "SELECT"
           " version"
           ",recovery_data_hash"
           ",expiration_date"
           " FROM anastasis_recoverydocument"
           " JOIN anastasis_user USING (user_id)"
           " WHERE user_id=$1"
           " ORDER BY version DESC"
           " LIMIT 1;");
  GNUNET_break (GNUNET_OK ==
                ANASTASIS_DB_preflight ());
  {
    struct GNUNET_PQ_ResultSpec rs[] = {
      GNUNET_PQ_result_spec_timestamp ("expiration_date",
                                       paid_until),
      GNUNET_PQ_result_spec_auto_from_type ("recovery_data_hash",
                                            recovery_data_hash),
      GNUNET_PQ_result_spec_uint32 ("version",
                                    version),
      GNUNET_PQ_result_spec_end
    };

    qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
                                                   "get_account_select_latest_version",
                                                   params,
                                                   rs);
  }
  switch (qs)
  {
  case GNUNET_DB_STATUS_HARD_ERROR:
    return ANASTASIS_DB_ACCOUNT_STATUS_HARD_ERROR;
  case GNUNET_DB_STATUS_SOFT_ERROR:
    GNUNET_break (0);
    return ANASTASIS_DB_ACCOUNT_STATUS_HARD_ERROR;
  case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
    break; /* handle interesting case below */
  case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
    return ANASTASIS_DB_ACCOUNT_STATUS_VALID_HASH_RETURNED;
  }

  /* check if account exists */
  {
    struct GNUNET_PQ_ResultSpec rs[] = {
      GNUNET_PQ_result_spec_timestamp ("expiration_date",
                                       paid_until),
      GNUNET_PQ_result_spec_end
    };

    qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
                                                   "get_account_select_account",
                                                   params,
                                                   rs);
  }
  switch (qs)
  {
  case GNUNET_DB_STATUS_HARD_ERROR:
    return ANASTASIS_DB_ACCOUNT_STATUS_HARD_ERROR;
  case GNUNET_DB_STATUS_SOFT_ERROR:
    GNUNET_break (0);
    return ANASTASIS_DB_ACCOUNT_STATUS_HARD_ERROR;
  case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
    /* indicates: no account */
    return ANASTASIS_DB_ACCOUNT_STATUS_PAYMENT_REQUIRED;
  case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
    /* indicates: no backup */
    *version = UINT32_MAX;
    memset (recovery_data_hash,
            0,
            sizeof (*recovery_data_hash));
    return ANASTASIS_DB_ACCOUNT_STATUS_NO_RESULTS;
  default:
    GNUNET_break (0);
    return ANASTASIS_DB_ACCOUNT_STATUS_HARD_ERROR;
  }
}


/* end of get_account.c */
