/*
   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/include/exchange-database/iterate_aml_decisions.h
 * @brief implementation of the iterate_aml_decisions function for Postgres
 * @author Christian Grothoff
 */
#ifndef EXCHANGE_DATABASE_ITERATE_AML_DECISIONS_H
#define EXCHANGE_DATABASE_ITERATE_AML_DECISIONS_H

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


/**
 * Return AML decision information.
 *
 * @param cls closure
 * @param row_id current row in legitimization outcomes table
 * @param justification human-readable reason for the decision, NULL if none is available
 * @param h_payto account for which the attribute data is stored
 * @param decision_time when was the decision taken
 * @param expiration_time when will the rules expire
 * @param jproperties properties set for the account,
 *    NULL if no properties were set
 * @param to_investigate true if AML staff should look at the account
 * @param is_active true if this is the currently active decision about the account
 * @param is_wallet true if the @a h_payto is for a Taler wallet
 * @param payto payto URI of the account the decision is about
 * @param account_rules current active rules for the account
 */
#ifndef TALER_EXCHANGEDB_AML_DECISION_RESULT_CLOSURE
/**
 * Type of the closure for #TALER_EXCHANGEDB_AmlDecisionCallback.
 */
#define TALER_EXCHANGEDB_AML_DECISION_RESULT_CLOSURE void
#endif
typedef void
(*TALER_EXCHANGEDB_AmlDecisionCallback)(
  TALER_EXCHANGEDB_AML_DECISION_RESULT_CLOSURE *cls,
  uint64_t row_id,
  const char *justification,
  const struct TALER_NormalizedPaytoHashP *h_payto,
  struct GNUNET_TIME_Timestamp decision_time,
  struct GNUNET_TIME_Absolute expiration_time,
  const json_t *jproperties,
  bool to_investigate,
  bool is_active,
  bool is_wallet,
  struct TALER_FullPayto payto,
  const json_t *account_rules);


/* Callback typedefs */
/**
 * Return AML decision information.
 *
 * @param cls closure
 * @param row_id current row in legitimization outcomes table
 * @param justification human-readable reason for the decision, NULL if none is available
 * @param h_payto account for which the attribute data is stored
 * @param decision_time when was the decision taken
 * @param expiration_time when will the rules expire
 * @param jproperties properties set for the account,
 *    NULL if no properties were set
 * @param to_investigate true if AML staff should look at the account
 * @param is_active true if this is the currently active decision about the account
 * @param is_wallet true if the @a h_payto is for a Taler wallet
 * @param payto payto URI of the account the decision is about
 * @param account_rules current active rules for the account
 */
typedef void
(*TALER_EXCHANGEDB_AmlDecisionCallback)(
  TALER_EXCHANGEDB_AML_DECISION_RESULT_CLOSURE *cls,
  uint64_t row_id,
  const char *justification,
  const struct TALER_NormalizedPaytoHashP *h_payto,
  struct GNUNET_TIME_Timestamp decision_time,
  struct GNUNET_TIME_Absolute expiration_time,
  const json_t *jproperties,
  bool to_investigate,
  bool is_active,
  bool is_wallet,
  struct TALER_FullPayto payto,
  const json_t *account_rules);

/**
 * Lookup AML decisions that have a particular state.
 *
 * @param pg the database context
 * @param h_payto which account should we return the AML decision history for, NULL to return all accounts
 * @param investigation_only filter by investigation state
 * @param active_only filter for only active states
 * @param offset row to start from
 * @param limit how many records to return (negative
 *        to go back in time, positive to go forward)
 * @param cb callback to invoke on each match
 * @param cb_cls closure for @a cb
 * @return database transaction status
 */
enum GNUNET_DB_QueryStatus
TALER_EXCHANGEDB_iterate_aml_decisions (struct
                                        TALER_EXCHANGEDB_PostgresContext *
                                        pg,
                                        const struct
                                        TALER_NormalizedPaytoHashP *
                                        h_payto,
                                        enum TALER_EXCHANGE_YesNoAll
                                        investigation_only
                                        ,
                                        enum TALER_EXCHANGE_YesNoAll active_only
                                        ,
                                        uint64_t offset,
                                        int64_t limit,
                                        TALER_EXCHANGEDB_AmlDecisionCallback cb,
                                        TALER_EXCHANGEDB_AML_DECISION_RESULT_CLOSURE
                                        *cb_cls);

#endif
