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

  TALER is free software; you can redistribute it and/or modify it under the
  terms of the GNU Lesser General Public License as published by the Free Software
  Foundation; either version 2.1, 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 Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License along with
  TALER; see the file COPYING.LGPL.  If not, see
  <http://www.gnu.org/licenses/>
*/
/**
 * @file src/lib/merchant_api_delete-private-webhooks-WEBHOOK_ID.c
 * @brief Implementation of the DELETE /private/webhooks/$WEBHOOK_ID request
 * @author Christian Grothoff
 */
#include "platform.h"
#include <curl/curl.h>
#include <jansson.h>
#include <microhttpd.h> /* just for HTTP status codes */
#include <gnunet/gnunet_util_lib.h>
#include <gnunet/gnunet_curl_lib.h>
#include <taler/merchant/delete-private-webhooks-WEBHOOK_ID.h>
#include "merchant_api_curl_defaults.h"
#include <taler/taler_json_lib.h>


/**
 * Handle for a DELETE /private/webhooks/$WEBHOOK_ID operation.
 */
struct TALER_MERCHANT_DeletePrivateWebhookHandle
{
  /**
   * Base URL of the merchant backend.
   */
  char *base_url;

  /**
   * The full URL for this request.
   */
  char *url;

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

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

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

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

  /**
   * Identifier of the webhook to delete.
   */
  char *webhook_id;
};


/**
 * Function called when we're done processing the
 * HTTP DELETE /private/webhooks/$WEBHOOK_ID request.
 *
 * @param cls the `struct TALER_MERCHANT_DeletePrivateWebhookHandle`
 * @param response_code HTTP response code, 0 on error
 * @param response response body, NULL if not in JSON
 */
static void
handle_delete_webhook_finished (void *cls,
                                long response_code,
                                const void *response)
{
  struct TALER_MERCHANT_DeletePrivateWebhookHandle *dwh = cls;
  const json_t *json = response;
  struct TALER_MERCHANT_DeletePrivateWebhookResponse dwr = {
    .hr.http_status = (unsigned int) response_code,
    .hr.reply = json
  };

  dwh->job = NULL;
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Got /private/webhooks/$WEBHOOK_ID DELETE response with status code %u\n",
              (unsigned int) response_code);
  switch (response_code)
  {
  case MHD_HTTP_NO_CONTENT:
    break;
  case MHD_HTTP_UNAUTHORIZED:
    dwr.hr.ec = TALER_JSON_get_error_code (json);
    dwr.hr.hint = TALER_JSON_get_error_hint (json);
    /* Nothing really to verify, merchant says we need to authenticate. */
    break;
  case MHD_HTTP_NOT_FOUND:
    dwr.hr.ec = TALER_JSON_get_error_code (json);
    dwr.hr.hint = TALER_JSON_get_error_hint (json);
    break;
  default:
    dwr.hr.ec = TALER_JSON_get_error_code (json);
    dwr.hr.hint = TALER_JSON_get_error_hint (json);
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Unexpected response code %u/%d for DELETE /private/webhooks/$WEBHOOK_ID\n",
                (unsigned int) response_code,
                (int) dwr.hr.ec);
    break;
  }
  dwh->cb (dwh->cb_cls,
           &dwr);
  TALER_MERCHANT_delete_private_webhook_cancel (dwh);
}


struct TALER_MERCHANT_DeletePrivateWebhookHandle *
TALER_MERCHANT_delete_private_webhook_create (
  struct GNUNET_CURL_Context *ctx,
  const char *url,
  const char *webhook_id)
{
  struct TALER_MERCHANT_DeletePrivateWebhookHandle *dwh;

  dwh = GNUNET_new (struct TALER_MERCHANT_DeletePrivateWebhookHandle);
  dwh->ctx = ctx;
  dwh->base_url = GNUNET_strdup (url);
  dwh->webhook_id = GNUNET_strdup (webhook_id);
  return dwh;
}


enum TALER_ErrorCode
TALER_MERCHANT_delete_private_webhook_start (
  struct TALER_MERCHANT_DeletePrivateWebhookHandle *dwh,
  TALER_MERCHANT_DeletePrivateWebhookCallback cb,
  TALER_MERCHANT_DELETE_PRIVATE_WEBHOOK_RESULT_CLOSURE *cb_cls)
{
  CURL *eh;

  dwh->cb = cb;
  dwh->cb_cls = cb_cls;
  {
    char *path;

    GNUNET_asprintf (&path,
                     "private/webhooks/%s",
                     dwh->webhook_id);
    dwh->url = TALER_url_join (dwh->base_url,
                               path,
                               NULL);
    GNUNET_free (path);
  }
  if (NULL == dwh->url)
    return TALER_EC_GENERIC_CONFIGURATION_INVALID;
  eh = TALER_MERCHANT_curl_easy_get_ (dwh->url);
  if (NULL == eh)
    return TALER_EC_GENERIC_CONFIGURATION_INVALID;
  GNUNET_assert (CURLE_OK ==
                 curl_easy_setopt (eh,
                                   CURLOPT_CUSTOMREQUEST,
                                   MHD_HTTP_METHOD_DELETE));
  dwh->job = GNUNET_CURL_job_add (dwh->ctx,
                                  eh,
                                  &handle_delete_webhook_finished,
                                  dwh);
  if (NULL == dwh->job)
    return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
  return TALER_EC_NONE;
}


void
TALER_MERCHANT_delete_private_webhook_cancel (
  struct TALER_MERCHANT_DeletePrivateWebhookHandle *dwh)
{
  if (NULL != dwh->job)
  {
    GNUNET_CURL_job_cancel (dwh->job);
    dwh->job = NULL;
  }
  GNUNET_free (dwh->url);
  GNUNET_free (dwh->base_url);
  GNUNET_free (dwh->webhook_id);
  GNUNET_free (dwh);
}


/* end of merchant_api_delete-private-webhooks-WEBHOOK_ID-new.c */
