/*
   This file is part of TALER
   Copyright (C) 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 backenddb/pg_lookup_expected_transfer.c
 * @brief Implementation of the lookup_expected_transfer function for Postgres
 * @author Christian Grothoff
 */
#include "taler/platform.h"
#include <taler/taler_error_codes.h>
#include <taler/taler_dbevents.h>
#include <taler/taler_pq_lib.h>
#include "pg_lookup_expected_transfer.h"
#include "pg_helper.h"


enum GNUNET_DB_QueryStatus
TMH_PG_lookup_expected_transfer (
  void *cls,
  const char *instance_id,
  uint64_t expected_incoming_serial,
  struct GNUNET_TIME_Timestamp *expected_time,
  struct TALER_Amount *expected_credit_amount,
  struct TALER_WireTransferIdentifierRawP *wtid,
  struct TALER_FullPayto *payto_uri,
  char **exchange_url,
  struct GNUNET_TIME_Timestamp *execution_time,
  bool *confirmed,
  struct TALER_MasterPublicKeyP *master_pub)
{
  struct PostgresClosure *pg = cls;
  struct GNUNET_PQ_QueryParam params[] = {
    GNUNET_PQ_query_param_string (instance_id),
    GNUNET_PQ_query_param_uint64 (&expected_incoming_serial),
    GNUNET_PQ_query_param_end
  };
  struct GNUNET_PQ_ResultSpec rs[] = {
    GNUNET_PQ_result_spec_timestamp ("expected_time",
                                     expected_time),
    GNUNET_PQ_result_spec_bool ("confirmed",
                                confirmed),
    GNUNET_PQ_result_spec_allow_null (
      TALER_PQ_result_spec_amount_with_currency ("expected_credit_amount",
                                                 expected_credit_amount),
      NULL),
    GNUNET_PQ_result_spec_string ("exchange_url",
                                  exchange_url),
    GNUNET_PQ_result_spec_auto_from_type ("wtid",
                                          wtid),
    GNUNET_PQ_result_spec_string ("payto_uri",
                                  &payto_uri->full_payto),
    GNUNET_PQ_result_spec_allow_null (
      GNUNET_PQ_result_spec_timestamp ("execution_time",
                                       execution_time),
      NULL),
    GNUNET_PQ_result_spec_auto_from_type ("master_pub",
                                          master_pub),
    GNUNET_PQ_result_spec_end
  };

  *execution_time = GNUNET_TIME_UNIT_ZERO_TS;
  memset (expected_credit_amount,
          0,
          sizeof (*expected_credit_amount));
  check_connection (pg);
  PREPARE (pg,
           "lookup_expected_transfer",
           "SELECT"
           "  met.expected_time"
           " ,met.confirmed"
           " ,met.expected_credit_amount"
           " ,met.exchange_url"
           " ,met.wtid"
           " ,ma.payto_uri"
           " ,mts.execution_time"
           " ,esk.master_pub"
           " FROM merchant_expected_transfers met"
           " JOIN merchant_exchange_signing_keys esk"
           "   USING (signkey_serial)"
           " JOIN merchant_accounts ma"
           "   USING (account_serial)"
           " JOIN merchant_instances inst"
           "   USING (merchant_serial)"
           " LEFT JOIN merchant_transfer_signatures mts"
           "   USING (expected_credit_serial)"
           " WHERE inst.merchant_id=$1"
           "   AND met.expected_credit_serial=$2");
  return GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
                                                   "lookup_expected_transfer",
                                                   params,
                                                   rs);
}
