/*
   This file is part of TALER
   Copyright (C) 2024 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/donaudb/iterate_history_entries.c
 * @brief Implementation of the iterate_history_entries function for Postgres
 * @author Johannes Casaburi
 */
#include "donau_config.h"
#include "taler/taler_error_codes.h"
#include "taler/taler_dbevents.h"
#include "taler/taler_pq_lib.h"
#include "iterate_history_entries.h"
#include "helper.h"


/**
 * Closure for #iterate_history_entries_cb().
 */
struct IterateHistoryEntriesContext
{
  /**
   * Function to call per result.
   */
  DONAUDB_IterateHistoryEntriesCallback cb;

  /**
   * Closure for @e cb.
   */
  void *cb_cls;

  /**
   * Plugin context.
   */
  struct DONAUDB_PostgresContext *ctx;

  /**
   * Number of results processed.
   */
  enum GNUNET_DB_QueryStatus qs;

};


/**
 * Invoke the callback for each result.
 *
 * @param cls a `struct IterateHistoryEntriesContext *`
 * @param result SQL result
 * @param num_results number of rows in @a result
 */
static void
iterate_history_entries_cb (void *cls,
                            PGresult *result,
                            unsigned int num_results)
{
  struct IterateHistoryEntriesContext *gctx = cls;
  struct DONAUDB_PostgresContext *ctx = gctx->ctx;

  for (unsigned int i = 0; i < num_results; i++)
  {
    uint64_t charity_id;
    struct TALER_Amount final_amount;
    uint64_t donation_year;

    struct GNUNET_PQ_ResultSpec rs[] = {
      GNUNET_PQ_result_spec_uint64 ("charity_id",
                                    &charity_id),
      TALER_PQ_RESULT_SPEC_AMOUNT ("final_amount",
                                   &final_amount),
      GNUNET_PQ_result_spec_uint64 ("donation_year",
                                    &donation_year),
      GNUNET_PQ_result_spec_end
    };

    if (GNUNET_OK !=
        GNUNET_PQ_extract_result (result,
                                  rs,
                                  i))
    {
      GNUNET_break (0);
      gctx->qs = GNUNET_DB_STATUS_HARD_ERROR;
      return;
    }

    gctx->qs = i + 1;
    if (GNUNET_OK !=
        gctx->cb (gctx->cb_cls,
                  charity_id,
                  final_amount,
                  donation_year))
      break;
  }
}


enum GNUNET_DB_QueryStatus
DONAUDB_iterate_history_entries (struct DONAUDB_PostgresContext *ctx,
                                 DONAUDB_IterateHistoryEntriesCallback cb,
                                 void *cb_cls)
{
  struct GNUNET_PQ_QueryParam params[] = {
    GNUNET_PQ_query_param_end
  };
  struct IterateHistoryEntriesContext gctx = {
    .cb = cb,
    .cb_cls = cb_cls,
    .ctx = ctx
  };
  enum GNUNET_DB_QueryStatus qs;

  PREPARE (ctx,
           "iterate_history_entries",
           "SELECT"
           " charity_id"
           ",final_amount"
           ",donation_year"
           " FROM history");
  qs = GNUNET_PQ_eval_prepared_multi_select (ctx->conn,
                                             "iterate_history_entries",
                                             params,
                                             &iterate_history_entries_cb,
                                             &gctx);
  if (qs <= 0)
    return qs;
  return gctx.qs;
}
