/*
   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 src/backenddb/iterate_outdated_kyc_statuses.c
 * @brief Implementation of the iterate_outdated_kyc_statuses function for Postgres
 * @author Christian Grothoff
 */
#include "platform.h"
#include <taler/taler_error_codes.h>
#include <taler/taler_dbevents.h>
#include <taler/taler_pq_lib.h>
#include "merchant-database/iterate_outdated_kyc_statuses.h"
#include "helper.h"

/**
 * Closure for kyc_status_cb().
 */
struct KycStatusContext
{
  /**
   * Function to call with results.
   */
  TALER_MERCHANTDB_KycOutdatedCallback kyc_cb;

  /**
   * Closure for @e kyc_cb.
   */
  void *kyc_cb_cls;

  /**
   * Number of results found.
   */
  unsigned int count;

  /**
   * Set to true on failure(s).
   */
  bool failure;
};


/**
 * Function to be called with the results of a SELECT statement
 * that has returned @a num_results results about accounts.
 *
 * @param[in,out] cls of type `struct KycStatusContext *`
 * @param result the postgres result
 * @param num_results the number of results in @a result
 */
static void
kyc_status_cb (void *cls,
               PGresult *result,
               unsigned int num_results)
{
  struct KycStatusContext *ksc = cls;

  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "Got %u outdated KYC records\n",
              num_results);
  for (unsigned int i = 0; i < num_results; i++)
  {
    struct TALER_MerchantWireHashP h_wire;
    char *exchange_url;
    char *instance_id;
    struct GNUNET_PQ_ResultSpec rs[] = {
      GNUNET_PQ_result_spec_auto_from_type ("out_h_wire",
                                            &h_wire),
      GNUNET_PQ_result_spec_string ("out_exchange_url",
                                    &exchange_url),
      GNUNET_PQ_result_spec_string ("out_merchant_id",
                                    &instance_id),
      GNUNET_PQ_result_spec_end
    };

    if (GNUNET_OK !=
        GNUNET_PQ_extract_result (result,
                                  rs,
                                  i))
    {
      GNUNET_break (0);
      ksc->failure = true;
      return;
    }
    ksc->count++;
    ksc->kyc_cb (ksc->kyc_cb_cls,
                 instance_id,
                 exchange_url,
                 &h_wire);
    GNUNET_PQ_cleanup_result (rs);
  }
}


enum GNUNET_DB_QueryStatus
TALER_MERCHANTDB_iterate_outdated_kyc_statuses (
  struct TALER_MERCHANTDB_PostgresContext *pg,
  TALER_MERCHANTDB_KycOutdatedCallback kyc_cb,
  void *kyc_cb_cls)
{
  struct KycStatusContext ksc = {
    .kyc_cb = kyc_cb,
    .kyc_cb_cls = kyc_cb_cls
  };
  struct GNUNET_TIME_Absolute now
    = GNUNET_TIME_absolute_get ();
  struct GNUNET_PQ_QueryParam params[] = {
    GNUNET_PQ_query_param_absolute_time (&now),
    GNUNET_PQ_query_param_end
  };
  enum GNUNET_DB_QueryStatus qs;

  PREPARE (pg,
           "iterate_outdated_kyc_statuses",
           "SELECT"
           "  out_merchant_id"
           " ,out_h_wire"
           " ,out_exchange_url"
           " FROM merchant.account_kyc_get_outdated($1)");
  qs = GNUNET_PQ_eval_prepared_multi_select (
    pg->conn,
    "iterate_outdated_kyc_statuses",
    params,
    &kyc_status_cb,
    &ksc);
  if (ksc.failure)
  {
    GNUNET_break (0);
    return GNUNET_DB_STATUS_HARD_ERROR;
  }
  if (0 > qs)
    return qs;
  return ksc.count;
}
