/*
  This file is part of TALER
  (C) 2026 Taler Systems SA

  TALER is free software; you can redistribute it and/or modify it under the
  terms of the GNU Affero 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 taler-merchant-httpd_get-exchanges.c
 * @brief implement API for querying exchange status
 * @author Christian Grothoff
 */
#include "taler/platform.h"
#include <jansson.h>
#include <taler/taler_util.h>
#include <taler/taler_json_lib.h>
#include "taler-merchant-httpd.h"
#include "taler-merchant-httpd_get-exchanges.h"
#include "taler-merchant-httpd_mhd.h"
#include "taler-merchant-httpd_get-exchanges.h"


/**
 * Callback on an exchange known to us.
 *
 * @param cls closure with `json_t *` array to expand
 * @param exchange_url base URL of the exchange
 * @param next_download when will be the next download
 * @param keys_expiration when does the current ``/keys`` response expire
 * @param http_status last HTTP status from ``/keys``
 * @param ec last error code from fetching ``/keys``
 */
static void
add_exchange (
  void *cls,
  const char *exchange_url,
  struct GNUNET_TIME_Absolute next_download,
  struct GNUNET_TIME_Absolute keys_expiration,
  unsigned int http_status,
  enum TALER_ErrorCode ec)
{
  json_t *xa = cls;
  json_t *xi;

  xi = GNUNET_JSON_PACK (
    GNUNET_JSON_pack_string ("exchange_url",
                             exchange_url),
    GNUNET_JSON_pack_timestamp ("next_download",
                                GNUNET_TIME_absolute_to_timestamp (next_download
                                                                   )),
    GNUNET_TIME_absolute_is_zero (keys_expiration)
    ? GNUNET_JSON_pack_allow_null (
      GNUNET_JSON_pack_string ("dummy",
                               NULL))
    : GNUNET_JSON_pack_timestamp ("keys_expiration",
                                  GNUNET_TIME_absolute_to_timestamp (
                                    keys_expiration)),
    GNUNET_JSON_pack_uint64 ("keys_http_status",
                             http_status),
    GNUNET_JSON_pack_uint64 ("keys_ec",
                             ec),
    GNUNET_JSON_pack_string ("keys_hint",
                             TALER_ErrorCode_get_hint (ec)));
  GNUNET_assert (NULL != xi);
  GNUNET_assert (0 ==
                 json_array_append_new (xa,
                                        xi));
}


MHD_RESULT
MH_handler_exchanges (const struct TMH_RequestHandler *rh,
                      struct MHD_Connection *connection,
                      struct TMH_HandlerContext *hc)
{
  json_t *exchanges;
  enum GNUNET_DB_QueryStatus qs;

  exchanges = json_array ();
  GNUNET_assert (NULL != exchanges);
  qs = TMH_db->select_exchanges (TMH_db->cls,
                                 &add_exchange,
                                 exchanges);
  if (0 > qs)
  {
    GNUNET_break (0);
    json_decref (exchanges);
    return TALER_MHD_reply_with_error (connection,
                                       MHD_HTTP_INTERNAL_SERVER_ERROR,
                                       TALER_EC_GENERIC_DB_FETCH_FAILED,
                                       "select_exchanges");
  }
  return TALER_MHD_REPLY_JSON_PACK (
    connection,
    MHD_HTTP_OK,
    GNUNET_JSON_pack_array_steal ("exchanges",
                                  exchanges));
}


/* end of taler-merchant-httpd_get-exchanges.c */
