/*
  This file is part of TALER
  Copyright (C) 2025-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-units-UNIT.c
 * @brief Implementation of the DELETE /private/units/$UNIT 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-units-UNIT.h>
#include "merchant_api_curl_defaults.h"
#include <taler/taler_json_lib.h>


/**
 * Handle for a DELETE /private/units/$UNIT operation.
 */
struct TALER_MERCHANT_DeletePrivateUnitHandle
{
  /**
   * 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_DeletePrivateUnitCallback cb;

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

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

  /**
   * Identifier of the unit to delete.
   */
  char *unit_id;
};


/**
 * Function called when we're done processing the
 * HTTP DELETE /private/units/$UNIT request.
 *
 * @param cls the `struct TALER_MERCHANT_DeletePrivateUnitHandle`
 * @param response_code HTTP response code, 0 on error
 * @param response response body, NULL if not in JSON
 */
static void
handle_delete_unit_finished (void *cls,
                             long response_code,
                             const void *response)
{
  struct TALER_MERCHANT_DeletePrivateUnitHandle *duh = cls;
  const json_t *json = response;
  struct TALER_MERCHANT_DeletePrivateUnitResponse dur = {
    .hr.http_status = (unsigned int) response_code,
    .hr.reply = json
  };

  duh->job = NULL;
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Got /private/units/$UNIT DELETE response with status code %u\n",
              (unsigned int) response_code);
  switch (response_code)
  {
  case MHD_HTTP_NO_CONTENT:
    break;
  case MHD_HTTP_UNAUTHORIZED:
    dur.hr.ec = TALER_JSON_get_error_code (json);
    dur.hr.hint = TALER_JSON_get_error_hint (json);
    break;
  case MHD_HTTP_NOT_FOUND:
    dur.hr.ec = TALER_JSON_get_error_code (json);
    dur.hr.hint = TALER_JSON_get_error_hint (json);
    break;
  case MHD_HTTP_CONFLICT:
    dur.hr.ec = TALER_JSON_get_error_code (json);
    dur.hr.hint = TALER_JSON_get_error_hint (json);
    break;
  default:
    dur.hr.ec = TALER_JSON_get_error_code (json);
    dur.hr.hint = TALER_JSON_get_error_hint (json);
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Unexpected response code %u/%d for DELETE /private/units/$UNIT\n",
                (unsigned int) response_code,
                (int) dur.hr.ec);
    break;
  }
  duh->cb (duh->cb_cls,
           &dur);
  TALER_MERCHANT_delete_private_unit_cancel (duh);
}


struct TALER_MERCHANT_DeletePrivateUnitHandle *
TALER_MERCHANT_delete_private_unit_create (
  struct GNUNET_CURL_Context *ctx,
  const char *url,
  const char *unit_id)
{
  struct TALER_MERCHANT_DeletePrivateUnitHandle *duh;

  duh = GNUNET_new (struct TALER_MERCHANT_DeletePrivateUnitHandle);
  duh->ctx = ctx;
  duh->base_url = GNUNET_strdup (url);
  duh->unit_id = GNUNET_strdup (unit_id);
  return duh;
}


enum TALER_ErrorCode
TALER_MERCHANT_delete_private_unit_start (
  struct TALER_MERCHANT_DeletePrivateUnitHandle *duh,
  TALER_MERCHANT_DeletePrivateUnitCallback cb,
  TALER_MERCHANT_DELETE_PRIVATE_UNIT_RESULT_CLOSURE *cb_cls)
{
  CURL *eh;

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

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


void
TALER_MERCHANT_delete_private_unit_cancel (
  struct TALER_MERCHANT_DeletePrivateUnitHandle *duh)
{
  if (NULL != duh->job)
  {
    GNUNET_CURL_job_cancel (duh->job);
    duh->job = NULL;
  }
  GNUNET_free (duh->url);
  GNUNET_free (duh->unit_id);
  GNUNET_free (duh->base_url);
  GNUNET_free (duh);
}


/* end of merchant_api_delete-private-units-UNIT-new.c */
