/*
  This file is part of Anastasis
  Copyright (C) 2020-2026 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/do_update_account_lifetime.c
 * @brief Anastasis database: do update account lifetime
 * @author Christian Grothoff
 */
#include "platform.h"
#include "anastasis-db_pg.h"
#include "anastasis/anastasis-database/do_update_account_lifetime.h"
#include "anastasis/anastasis-database/preflight.h"
#include <taler/taler_pq_lib.h>


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)
{
  struct GNUNET_PQ_QueryParam params[] = {
    GNUNET_PQ_query_param_auto_from_type (account_pub),
    GNUNET_PQ_query_param_auto_from_type (payment_identifier),
    GNUNET_PQ_query_param_timestamp (&min_expiration),
    GNUNET_PQ_query_param_relative_time (&lifetime),
    GNUNET_PQ_query_param_end
  };
  bool payment_credited;
  struct GNUNET_PQ_ResultSpec rs[] = {
    GNUNET_PQ_result_spec_bool ("payment_credited",
                                &payment_credited),
    GNUNET_PQ_result_spec_timestamp ("paid_until",
                                     paid_until),
    GNUNET_PQ_result_spec_end
  };
  enum GNUNET_DB_QueryStatus qs;

  /* Expiration dates are whole seconds; make sure adding the increment
     cannot break that (GNUNET_PQ_result_spec_timestamp() insists on it). */
  if (! GNUNET_TIME_relative_is_forever (lifetime))
    lifetime.rel_value_us
      -= lifetime.rel_value_us % GNUNET_TIME_UNIT_SECONDS.rel_value_us;
  *paid_until = GNUNET_TIME_UNIT_ZERO_TS;
  GNUNET_break (GNUNET_OK ==
                ANASTASIS_DB_preflight ());
  PREPARE ("do_update_account_lifetime",
           "SELECT"
           " out_payment_credited AS payment_credited"
           ",out_paid_until AS paid_until"
           " FROM anastasis_do_update_account_lifetime"
           " ($1, $2, $3, $4);");
  qs = GNUNET_PQ_eval_prepared_singleton_select (
    pg->conn,
    "do_update_account_lifetime",
    params,
    rs);
  if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != qs)
  {
    /* The stored procedure always returns exactly one row, so anything
       else is a real database failure. */
    GNUNET_break (qs < 0);
    *paid_until = GNUNET_TIME_UNIT_ZERO_TS;
    return (qs < 0)
           ? qs
           : GNUNET_DB_STATUS_HARD_ERROR;
  }
  if (! payment_credited)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                "Payment existed, lifetime of account %s unchanged at %s\n",
                TALER_B2S (account_pub),
                GNUNET_TIME_timestamp2s (*paid_until));
    return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS;
  }
  GNUNET_break (! GNUNET_TIME_absolute_is_never (paid_until->abs_time));
  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "Updated lifetime of account %s to %s\n",
              TALER_B2S (account_pub),
              GNUNET_TIME_timestamp2s (*paid_until));
  return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
}


/* end of do_update_account_lifetime.c */
