/*
  This file is part of TALER
  Copyright (C) 2020-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 lib/exchange_api_post-management-wire-fee.c
 * @brief functions to set wire fees at an exchange
 * @author Christian Grothoff
 */
#include "taler/taler_json_lib.h"
#include <gnunet/gnunet_curl_lib.h>
#include <microhttpd.h>
#include "taler/exchange/post-management-wire-fee.h"
#include "exchange_api_curl_defaults.h"
#include "taler/taler_curl_lib.h"


struct TALER_EXCHANGE_PostManagementWireFeesHandle
{

  /**
   * The base URL for this request.
   */
  char *base_url;

  /**
   * The full URL for this request, set during _start.
   */
  char *url;

  /**
   * Minor context that holds body and headers.
   */
  struct TALER_CURL_PostContext post_ctx;

  /**
   * Handle for the request.
   */
  struct GNUNET_CURL_Job *job;

  /**
   * Function to call with the result.
   */
  TALER_EXCHANGE_PostManagementWireFeesCallback cb;

  /**
   * Closure for @a cb.
   */
  TALER_EXCHANGE_POST_MANAGEMENT_WIRE_FEES_RESULT_CLOSURE *cb_cls;

  /**
   * Reference to the execution context.
   */
  struct GNUNET_CURL_Context *ctx;

  /**
   * Wire method these fees are for.
   */
  char *wire_method;

  /**
   * Start of validity period.
   */
  struct GNUNET_TIME_Timestamp validity_start;

  /**
   * End of validity period.
   */
  struct GNUNET_TIME_Timestamp validity_end;

  /**
   * Wire fees for this time period.
   */
  struct TALER_WireFeeSet fees;

  /**
   * Signature affirming the wire fees.
   */
  struct TALER_MasterSignatureP master_sig;

};


/**
 * Function called when we're done processing the
 * HTTP POST /management/wire-fees request.
 *
 * @param cls the `struct TALER_EXCHANGE_PostManagementWireFeesHandle`
 * @param response_code HTTP response code, 0 on error
 * @param response response body, NULL if not in JSON
 */
static void
handle_wire_fees_finished (void *cls,
                           long response_code,
                           const void *response)
{
  struct TALER_EXCHANGE_PostManagementWireFeesHandle *pmwfh = cls;
  const json_t *json = response;
  struct TALER_EXCHANGE_PostManagementWireFeesResponse res = {
    .hr.http_status = (unsigned int) response_code,
    .hr.reply = json
  };

  pmwfh->job = NULL;
  switch (response_code)
  {
  case MHD_HTTP_NO_CONTENT:
    break;
  case MHD_HTTP_FORBIDDEN:
    res.hr.ec = TALER_JSON_get_error_code (json);
    res.hr.hint = TALER_JSON_get_error_hint (json);
    break;
  case MHD_HTTP_CONFLICT:
    res.hr.ec = TALER_JSON_get_error_code (json);
    res.hr.hint = TALER_JSON_get_error_hint (json);
    break;
  default:
    /* unexpected response code */
    GNUNET_break_op (0);
    res.hr.ec = TALER_JSON_get_error_code (json);
    res.hr.hint = TALER_JSON_get_error_hint (json);
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Unexpected response code %u/%d for exchange management set wire fee\n",
                (unsigned int) response_code,
                (int) res.hr.ec);
    break;
  }
  if (NULL != pmwfh->cb)
  {
    pmwfh->cb (pmwfh->cb_cls,
               &res);
    pmwfh->cb = NULL;
  }
  TALER_EXCHANGE_post_management_wire_fees_cancel (pmwfh);
}


struct TALER_EXCHANGE_PostManagementWireFeesHandle *
TALER_EXCHANGE_post_management_wire_fees_create (
  struct GNUNET_CURL_Context *ctx,
  const char *exchange_base_url,
  const char *wire_method,
  struct GNUNET_TIME_Timestamp validity_start,
  struct GNUNET_TIME_Timestamp validity_end,
  const struct TALER_WireFeeSet *fees,
  const struct TALER_MasterSignatureP *master_sig)
{
  struct TALER_EXCHANGE_PostManagementWireFeesHandle *pmwfh;

  pmwfh = GNUNET_new (struct TALER_EXCHANGE_PostManagementWireFeesHandle);
  pmwfh->ctx = ctx;
  pmwfh->base_url = GNUNET_strdup (exchange_base_url);
  pmwfh->wire_method = GNUNET_strdup (wire_method);
  pmwfh->validity_start = validity_start;
  pmwfh->validity_end = validity_end;
  pmwfh->fees = *fees;
  pmwfh->master_sig = *master_sig;
  return pmwfh;
}


enum TALER_ErrorCode
TALER_EXCHANGE_post_management_wire_fees_start (
  struct TALER_EXCHANGE_PostManagementWireFeesHandle *pmwfh,
  TALER_EXCHANGE_PostManagementWireFeesCallback cb,
  TALER_EXCHANGE_POST_MANAGEMENT_WIRE_FEES_RESULT_CLOSURE *cb_cls)
{
  CURL *eh;
  json_t *body;

  pmwfh->cb = cb;
  pmwfh->cb_cls = cb_cls;
  pmwfh->url = TALER_url_join (pmwfh->base_url,
                               "management/wire-fee",
                               NULL);
  if (NULL == pmwfh->url)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Could not construct request URL.\n");
    return TALER_EC_GENERIC_CONFIGURATION_INVALID;
  }
  body = GNUNET_JSON_PACK (
    GNUNET_JSON_pack_string ("wire_method",
                             pmwfh->wire_method),
    GNUNET_JSON_pack_data_auto ("master_sig",
                                &pmwfh->master_sig),
    GNUNET_JSON_pack_timestamp ("fee_start",
                                pmwfh->validity_start),
    GNUNET_JSON_pack_timestamp ("fee_end",
                                pmwfh->validity_end),
    TALER_JSON_pack_amount ("closing_fee",
                            &pmwfh->fees.closing),
    TALER_JSON_pack_amount ("wire_fee",
                            &pmwfh->fees.wire));
  eh = TALER_EXCHANGE_curl_easy_get_ (pmwfh->url);
  if ( (NULL == eh) ||
       (GNUNET_OK !=
        TALER_curl_easy_post (&pmwfh->post_ctx,
                              eh,
                              body)) )
  {
    GNUNET_break (0);
    if (NULL != eh)
      curl_easy_cleanup (eh);
    json_decref (body);
    GNUNET_free (pmwfh->url);
    pmwfh->url = NULL;
    return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
  }
  json_decref (body);
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Requesting URL '%s'\n",
              pmwfh->url);
  pmwfh->job = GNUNET_CURL_job_add2 (pmwfh->ctx,
                                     eh,
                                     pmwfh->post_ctx.headers,
                                     &handle_wire_fees_finished,
                                     pmwfh);
  if (NULL == pmwfh->job)
  {
    TALER_curl_easy_post_finished (&pmwfh->post_ctx);
    GNUNET_free (pmwfh->url);
    pmwfh->url = NULL;
    return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
  }
  return TALER_EC_NONE;
}


void
TALER_EXCHANGE_post_management_wire_fees_cancel (
  struct TALER_EXCHANGE_PostManagementWireFeesHandle *pmwfh)
{
  if (NULL != pmwfh->job)
  {
    GNUNET_CURL_job_cancel (pmwfh->job);
    pmwfh->job = NULL;
  }
  TALER_curl_easy_post_finished (&pmwfh->post_ctx);
  GNUNET_free (pmwfh->wire_method);
  GNUNET_free (pmwfh->url);
  GNUNET_free (pmwfh->base_url);
  GNUNET_free (pmwfh);
}


/* end of exchange_api_post-management-wire-fee.c */
