/*
  This file is part of TALER
  Copyright (C) 2014-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/include/taler/merchant/post-private-webhooks.h
 * @brief C interface for the POST /private/webhooks endpoint
 * @author Christian Grothoff
 */
#ifndef _TALER_MERCHANT__POST_PRIVATE_WEBHOOKS_H
#define _TALER_MERCHANT__POST_PRIVATE_WEBHOOKS_H

#include <taler/merchant/common.h>


/**
 * Possible options we can set for the POST /private/webhooks request.
 */
enum TALER_MERCHANT_PostPrivateWebhooksOption
{
  /**
   * End of list of options.
   */
  TALER_MERCHANT_POST_PRIVATE_WEBHOOKS_OPTION_END = 0,

  /**
   * Set the header template.
   */
  TALER_MERCHANT_POST_PRIVATE_WEBHOOKS_OPTION_HEADER_TEMPLATE,

  /**
   * Set the body template.
   */
  TALER_MERCHANT_POST_PRIVATE_WEBHOOKS_OPTION_BODY_TEMPLATE

};


/**
 * Value for an option for the POST /private/webhooks request.
 */
struct TALER_MERCHANT_PostPrivateWebhooksOptionValue
{

  /**
   * Type of the option being set.
   */
  enum TALER_MERCHANT_PostPrivateWebhooksOption option;

  /**
   * Specific option value.
   */
  union
  {

    /**
     * Value if @e option is
     * #TALER_MERCHANT_POST_PRIVATE_WEBHOOKS_OPTION_HEADER_TEMPLATE.
     */
    const char *header_template;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_POST_PRIVATE_WEBHOOKS_OPTION_BODY_TEMPLATE.
     */
    const char *body_template;

  } details;

};


/**
 * Handle for a POST /private/webhooks request.
 */
struct TALER_MERCHANT_PostPrivateWebhooksHandle;


/**
 * Response details for a POST /private/webhooks request.
 */
struct TALER_MERCHANT_PostPrivateWebhooksResponse
{

  /**
   * HTTP response details.
   */
  struct TALER_MERCHANT_HttpResponse hr;

};


/**
 * Set up POST /private/webhooks operation.
 * Note that you must explicitly start the operation after
 * possibly setting options.
 *
 * @param ctx the context
 * @param url base URL of the merchant backend
 * @param webhook_id identifier for the new webhook
 * @param event_type event type that triggers this webhook
 * @param notification_url URL to send notifications to
 * @param http_method HTTP method to use for notifications
 * @return handle to operation
 */
struct TALER_MERCHANT_PostPrivateWebhooksHandle *
TALER_MERCHANT_post_private_webhooks_create (
  struct GNUNET_CURL_Context *ctx,
  const char *url,
  const char *webhook_id,
  const char *event_type,
  const char *notification_url,
  const char *http_method);


/**
 * Terminate the list of the options.
 *
 * @return the terminating object of struct TALER_MERCHANT_PostPrivateWebhooksOptionValue
 */
#define TALER_MERCHANT_post_private_webhooks_option_end_()                \
        (const struct TALER_MERCHANT_PostPrivateWebhooksOptionValue)       \
        {                                                                  \
          .option = TALER_MERCHANT_POST_PRIVATE_WEBHOOKS_OPTION_END       \
        }

/**
 * Set header template.
 *
 * @param ht header template string to set
 * @return representation of the option as a struct TALER_MERCHANT_PostPrivateWebhooksOptionValue
 */
#define TALER_MERCHANT_post_private_webhooks_option_header_template(ht)            \
        (const struct TALER_MERCHANT_PostPrivateWebhooksOptionValue)                \
        {                                                                           \
          .option = \
            TALER_MERCHANT_POST_PRIVATE_WEBHOOKS_OPTION_HEADER_TEMPLATE,    \
          .details.header_template = (ht)                                           \
        }

/**
 * Set body template.
 *
 * @param bt body template string to set
 * @return representation of the option as a struct TALER_MERCHANT_PostPrivateWebhooksOptionValue
 */
#define TALER_MERCHANT_post_private_webhooks_option_body_template(bt)              \
        (const struct TALER_MERCHANT_PostPrivateWebhooksOptionValue)                \
        {                                                                           \
          .option = \
            TALER_MERCHANT_POST_PRIVATE_WEBHOOKS_OPTION_BODY_TEMPLATE,      \
          .details.body_template = (bt)                                             \
        }


/**
 * Set the requested options for the operation.
 *
 * If any option fail other options may be or may be not applied.
 *
 * @param ppwh the request to set the options for
 * @param num_options length of the @a options array
 * @param options an array of options
 * @return #GNUNET_OK on success,
 *         #GNUNET_NO on failure,
 *         #GNUNET_SYSERR on internal error
 */
enum GNUNET_GenericReturnValue
TALER_MERCHANT_post_private_webhooks_set_options_ (
  struct TALER_MERCHANT_PostPrivateWebhooksHandle *ppwh,
  unsigned int num_options,
  const struct TALER_MERCHANT_PostPrivateWebhooksOptionValue *options);


/**
 * Set the requested options for the operation.
 *
 * If any option fail other options may be or may be not applied.
 *
 * It should be used with helpers that create required options, for example:
 *
 * TALER_MERCHANT_post_private_webhooks_set_options (
 *   ppwh,
 *   TALER_MERCHANT_post_private_webhooks_option_header_template (
 *     "Content-Type: application/json"));
 *
 * @param ppwh the request to set the options for
 * @param ... the list of the options, each option must be created
 *            by helpers TALER_MERCHANT_post_private_webhooks_option_NAME(VALUE)
 * @return #GNUNET_OK on success,
 *         #GNUNET_NO on failure,
 *         #GNUNET_SYSERR on internal error
 */
#define TALER_MERCHANT_post_private_webhooks_set_options(ppwh,...)                \
        TALER_MERCHANT_post_private_webhooks_set_options_ (                        \
          ppwh,                                                                    \
          TALER_MERCHANT_COMMON_OPTIONS_ARRAY_MAX_SIZE,                           \
          ((const struct TALER_MERCHANT_PostPrivateWebhooksOptionValue[])          \
           {__VA_ARGS__, TALER_MERCHANT_post_private_webhooks_option_end_ () }    \
          ))


#ifndef TALER_MERCHANT_POST_PRIVATE_WEBHOOKS_RESULT_CLOSURE
/**
 * Type of the closure used by
 * the #TALER_MERCHANT_PostPrivateWebhooksCallback.
 */
#define TALER_MERCHANT_POST_PRIVATE_WEBHOOKS_RESULT_CLOSURE void
#endif /* TALER_MERCHANT_POST_PRIVATE_WEBHOOKS_RESULT_CLOSURE */

/**
 * Callback for a POST /private/webhooks request.
 *
 * @param cls closure
 * @param wpr response details
 */
typedef void
(*TALER_MERCHANT_PostPrivateWebhooksCallback)(
  TALER_MERCHANT_POST_PRIVATE_WEBHOOKS_RESULT_CLOSURE *cls,
  const struct TALER_MERCHANT_PostPrivateWebhooksResponse *wpr);


/**
 * Start POST /private/webhooks operation.
 *
 * @param[in,out] ppwh operation to start
 * @param cb function to call with the merchant's result
 * @param cb_cls closure for @a cb
 * @return status code, #TALER_EC_NONE on success
 */
enum TALER_ErrorCode
TALER_MERCHANT_post_private_webhooks_start (
  struct TALER_MERCHANT_PostPrivateWebhooksHandle *ppwh,
  TALER_MERCHANT_PostPrivateWebhooksCallback cb,
  TALER_MERCHANT_POST_PRIVATE_WEBHOOKS_RESULT_CLOSURE *cb_cls);


/**
 * Cancel POST /private/webhooks operation.  This function must not be
 * called by clients after the TALER_MERCHANT_PostPrivateWebhooksCallback
 * has been invoked (as in those cases it'll be called internally by the
 * implementation already).
 *
 * @param[in] ppwh operation to cancel
 */
void
TALER_MERCHANT_post_private_webhooks_cancel (
  struct TALER_MERCHANT_PostPrivateWebhooksHandle *ppwh);


#endif /* _TALER_MERCHANT__POST_PRIVATE_WEBHOOKS_H */
