/*
   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/auditordb/iterate_aml_staff.c
 * @brief Implementation of the iterate_aml_staff function for Postgres
 * @author Christian Grothoff
 */
#include "taler/taler_error_codes.h"
#include "taler/taler_pq_lib.h"
#include "pg_helper.h"
#include "auditor-database/iterate_aml_staff.h"


/**
 * Closure for #aml_staff_cb().
 */
struct AmlStaffContext
{
  /**
   * Function to call for each status change.
   */
  TALER_AUDITORDB_AmlStaffCallback cb;

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

  /**
   * Query status to return.
   */
  enum GNUNET_DB_QueryStatus qs;
};


/**
 * Helper function for #TALER_AUDITORDB_iterate_aml_staff().
 * To be called with the results of a SELECT statement
 * that has returned @a num_results results.
 *
 * @param cls closure of type `struct AmlStaffContext *`
 * @param result the postgres result
 * @param num_results the number of results in @a result
 */
static void
aml_staff_cb (void *cls,
              PGresult *result,
              unsigned int num_results)
{
  struct AmlStaffContext *asc = cls;

  for (unsigned int i = 0; i < num_results; i++)
  {
    struct TALER_AmlOfficerPublicKeyP decider_pub;
    char *decider_name = NULL;
    bool is_active;
    bool read_only;
    bool master_sig_valid;
    struct GNUNET_TIME_Timestamp last_change;
    struct GNUNET_PQ_ResultSpec rs[] = {
      GNUNET_PQ_result_spec_auto_from_type ("decider_pub",
                                            &decider_pub),
      GNUNET_PQ_result_spec_string ("decider_name",
                                    &decider_name),
      GNUNET_PQ_result_spec_bool ("is_active",
                                  &is_active),
      GNUNET_PQ_result_spec_bool ("read_only",
                                  &read_only),
      GNUNET_PQ_result_spec_bool ("master_sig_valid",
                                  &master_sig_valid),
      GNUNET_PQ_result_spec_timestamp ("last_change",
                                       &last_change),
      GNUNET_PQ_result_spec_end
    };
    enum GNUNET_GenericReturnValue rval;

    if (GNUNET_OK !=
        GNUNET_PQ_extract_result (result,
                                  rs,
                                  i))
    {
      GNUNET_break (0);
      asc->qs = GNUNET_DB_STATUS_HARD_ERROR;
      return;
    }
    asc->qs = i + 1;
    rval = asc->cb (asc->cb_cls,
                    &decider_pub,
                    decider_name,
                    is_active,
                    read_only,
                    master_sig_valid,
                    last_change);
    GNUNET_PQ_cleanup_result (rs);
    if (GNUNET_OK != rval)
      break;
  }
}


enum GNUNET_DB_QueryStatus
TALER_AUDITORDB_iterate_aml_staff (
  struct TALER_AUDITORDB_PostgresContext *pg,
  TALER_AUDITORDB_AmlStaffCallback cb,
  TALER_AUDITORDB_AML_STAFF_RESULT_CLOSURE *cb_cls)
{
  struct GNUNET_PQ_QueryParam params[] = {
    GNUNET_PQ_query_param_end
  };
  struct AmlStaffContext asc = {
    .cb = cb,
    .cb_cls = cb_cls
  };
  enum GNUNET_DB_QueryStatus qs;

  PREPARE (pg,
           "iterate_aml_staff",
           "SELECT"
           " decider_pub"
           ",decider_name"
           ",is_active"
           ",read_only"
           ",master_sig_valid"
           ",last_change"
           " FROM auditor_aml_staff"
           " ORDER BY last_change ASC, row_id ASC;");
  qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
                                             "iterate_aml_staff",
                                             params,
                                             &aml_staff_cb,
                                             &asc);
  if (qs > 0)
    return asc.qs;
  GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != qs);
  return qs;
}
