/*
   This file is part of TALER
   Copyright (C) 2025 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/include/auditor-database/iterate_early_aggregations.h
 * @brief implementation of the iterate_early_aggregations function for Postgres
 * @author Christian Grothoff
 */
#ifndef AUDITOR_DATABASE_ITERATE_EARLY_AGGREGATIONS_H
#define AUDITOR_DATABASE_ITERATE_EARLY_AGGREGATIONS_H

#include "taler/taler_util.h"
#include "taler/taler_json_lib.h"
#include "auditordb_lib.h"


/**
 * Information about an early aggregation event.
 */
struct TALER_AUDITORDB_EarlyAggregation
{
  /**
   * Row of the event in the auditor database.
   */
  uint64_t row_id;

  /**
   * Row of the batch deposit in the exchange database.
   */
  uint64_t batch_deposit_serial_id;

  /**
   * FIXME
   */
  uint64_t tracking_serial_id;

  /**
   * Total amount involved.
   */
  struct TALER_Amount total;

  /**
   * True if this report was previously suppressed.
   */
  bool suppressed;
};


/**
 * Function to call with information about early aggregations.
 *
 * @param cls closure
 * @param ea event data
 * @return #GNUNET_OK to continue to iterate
 */
#ifndef TALER_AUDITORDB_EARLY_AGGREGATIONS_RESULT_CLOSURE
/**
 * Type of the closure for #TALER_AUDITORDB_EarlyAggregationsCallback.
 */
#define TALER_AUDITORDB_EARLY_AGGREGATIONS_RESULT_CLOSURE void
#endif
typedef enum GNUNET_GenericReturnValue
(*TALER_AUDITORDB_EarlyAggregationsCallback)(
  TALER_AUDITORDB_EARLY_AGGREGATIONS_RESULT_CLOSURE *cls,
  const struct TALER_AUDITORDB_EarlyAggregation *ea);


/**
 * Returns all aggregations that were found that were done
 * too early.
 *
 * @param pg the database context
 * @param limit number of rows to return, negative to iterate backwards
 * @param offset starting offset, exclusive
 * @param return_suppressed true to also return suppressed events
 * @param cb function to call with results
 * @param cb_cls closure for @a cb
 * @return transaction status
 */
enum GNUNET_DB_QueryStatus
TALER_AUDITORDB_iterate_early_aggregations (
  struct TALER_AUDITORDB_PostgresContext *pg,
  int64_t limit,
  uint64_t offset,
  bool return_suppressed,
  TALER_AUDITORDB_EarlyAggregationsCallback cb,
  TALER_AUDITORDB_EARLY_AGGREGATIONS_RESULT_CLOSURE *cb_cls);


#endif
