/*
   This file is part of TALER
   Copyright (C) 2022, 2026 Taler Systems SA

   TALER is free software; you can redistribute it and/or modify it under the
   terms of the GNU General Public License as published by the Free Software
   Foundation; either version 3, or (at your option) any later version.

   TALER 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 General Public License for more details.

   You should have received a copy of the GNU General Public License along with
   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
/**
 * @file get_reserve_close_request_info.c
 * @brief Low-level (statement-level) Postgres database access for the exchange
 * @author Christian Grothoff
 */
#include "taler/taler_pq_lib.h"
#include "exchange-database/get_reserve_close_request_info.h"
#include "helper.h"

enum GNUNET_DB_QueryStatus
TALER_EXCHANGEDB_get_reserve_close_request_info (
  struct TALER_EXCHANGEDB_PostgresContext *pg,
  const struct TALER_ReservePublicKeyP *reserve_pub,
  uint64_t rowid,
  struct TALER_ReserveSignatureP *reserve_sig,
  struct GNUNET_TIME_Timestamp *request_timestamp,
  struct TALER_Amount *close_balance,
  struct TALER_Amount *close_fee,
  struct TALER_FullPayto *payto_uri)
{
  struct GNUNET_PQ_QueryParam params[] = {
    GNUNET_PQ_query_param_auto_from_type (reserve_pub),
    GNUNET_PQ_query_param_uint64 (&rowid),
    GNUNET_PQ_query_param_end
  };
  struct GNUNET_PQ_ResultSpec rs[] = {
    GNUNET_PQ_result_spec_auto_from_type ("reserve_sig",
                                          reserve_sig),
    GNUNET_PQ_result_spec_timestamp ("close_timestamp",
                                     request_timestamp),
    TALER_PQ_result_spec_amount ("close",
                                 pg->currency,
                                 close_balance),
    TALER_PQ_result_spec_amount ("close_fee",
                                 pg->currency,
                                 close_fee),
    GNUNET_PQ_result_spec_string ("payto_uri",
                                  &payto_uri->full_payto),
    GNUNET_PQ_result_spec_end
  };

  PREPARE (pg,
           "get_reserve_close_request_info",
           "SELECT "
           "  cr.reserve_sig"
           " ,cr.close_timestamp"
           " ,cr.close"
           " ,cr.close_fee"
           " ,cr.payto_uri"
           " FROM close_requests cr"
           " WHERE cr.reserve_pub=$1"
           "   AND cr.row_id=$2;");
  return GNUNET_PQ_eval_prepared_singleton_select (
    pg->conn,
    "get_reserve_close_request_info",
    params,
    rs);
}
