/*
  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 include/anastasis/anastasis-database/do_update_account_lifetime.h
 * @brief Anastasis database: do update account lifetime
 * @author Christian Grothoff
 */
#ifndef ANASTASIS_DATABASE_DO_UPDATE_ACCOUNT_LIFETIME_H
#define ANASTASIS_DATABASE_DO_UPDATE_ACCOUNT_LIFETIME_H

#include <gnunet/gnunet_util_lib.h>
#include <gnunet/gnunet_db_lib.h>
#include "anastasis_service.h"
#include "anastasis/anastasis-database/common.h"

/**
 * Credit a recovery document payment and extend the account's lifetime
 * accordingly.  Does nothing if the payment is not new.
 *
 * The account is set to expire at
 *
 *   MAX(current expiration, @a min_expiration) + @a lifetime
 *
 * (saturating at "forever"), and is created if it does not exist yet.
 * Callers that bought a duration pass the current time as @a min_expiration
 * and the duration as @a lifetime; callers that bought a fixed end date pass
 * that date as @a min_expiration and #GNUNET_TIME_UNIT_ZERO as @a lifetime.
 *
 * @param account_pub which account received a payment
 * @param payment_identifier proof of payment, must be unique and match pending payment
 * @param min_expiration lower bound for the resulting expiration
 * @param lifetime by how much to extend beyond @a min_expiration
 * @param[out] paid_until set to the lifetime of the account after the
 *        operation; on a payment that was not new, to the lifetime it
 *        already had (zero if there is no such account)
 * @return #GNUNET_DB_STATUS_SUCCESS_ONE_RESULT if the payment was credited
 *         and the lifetime extended,
 *         #GNUNET_DB_STATUS_SUCCESS_NO_RESULTS if the payment was unknown or
 *         had been credited before, in which case the lifetime is unchanged
 */
enum GNUNET_DB_QueryStatus
ANASTASIS_DB_do_update_account_lifetime (
  const struct ANASTASIS_CRYPTO_AccountPublicKeyP *account_pub,
  const struct ANASTASIS_PaymentSecretP *payment_identifier,
  struct GNUNET_TIME_Timestamp min_expiration,
  struct GNUNET_TIME_Relative lifetime,
  struct GNUNET_TIME_Timestamp *paid_until);

#endif
