/*
  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/patch-private-templates-TEMPLATE_ID.h
 * @brief C interface for PATCH /private/templates/$TEMPLATE_ID
 * @author Christian Grothoff
 */
#ifndef _TALER_MERCHANT__PATCH_PRIVATE_TEMPLATES_TEMPLATE_ID_H
#define _TALER_MERCHANT__PATCH_PRIVATE_TEMPLATES_TEMPLATE_ID_H

#include <taler/merchant/common.h>


/**
 * Possible options for the PATCH /private/templates/$TEMPLATE_ID request.
 */
enum TALER_MERCHANT_PatchPrivateTemplateOption
{
  /**
   * End of list of options.
   */
  TALER_MERCHANT_PATCH_PRIVATE_TEMPLATE_OPTION_END = 0,

  /**
   * Editable defaults (JSON object) for the template.
   */
  TALER_MERCHANT_PATCH_PRIVATE_TEMPLATE_OPTION_EDITABLE_DEFAULTS

};


/**
 * Value for an option for the PATCH /private/templates/$TEMPLATE_ID request.
 */
struct TALER_MERCHANT_PatchPrivateTemplateOptionValue
{

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

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

    /**
     * Value if @e option is
     * #TALER_MERCHANT_PATCH_PRIVATE_TEMPLATE_OPTION_EDITABLE_DEFAULTS.
     */
    const json_t *editable_defaults;

  } details;

};


/**
 * Handle for a PATCH /private/templates/$TEMPLATE_ID operation.
 */
struct TALER_MERCHANT_PatchPrivateTemplateHandle;


/**
 * Set up PATCH /private/templates/$TEMPLATE_ID 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 template_id identifier of the template to update
 * @param template_description new human-readable description
 * @param otp_id new OTP device ID, or NULL to remove association
 * @param template_contract new template contract (JSON)
 * @return handle to operation, NULL on error
 */
struct TALER_MERCHANT_PatchPrivateTemplateHandle *
TALER_MERCHANT_patch_private_template_create (
  struct GNUNET_CURL_Context *ctx,
  const char *url,
  const char *template_id,
  const char *template_description,
  const char *otp_id,
  json_t *template_contract);


/**
 * Terminate the list of the options.
 *
 * @return the terminating object
 */
#define TALER_MERCHANT_patch_private_template_option_end_()                \
        (const struct TALER_MERCHANT_PatchPrivateTemplateOptionValue)       \
        {                                                                   \
          .option = TALER_MERCHANT_PATCH_PRIVATE_TEMPLATE_OPTION_END       \
        }

/**
 * Set editable defaults.
 *
 * @param ed editable defaults (JSON object)
 * @return representation of the option
 */
#define TALER_MERCHANT_patch_private_template_option_editable_defaults(ed)  \
        (const struct TALER_MERCHANT_PatchPrivateTemplateOptionValue)        \
        {                                                                    \
          .option =                                                          \
            TALER_MERCHANT_PATCH_PRIVATE_TEMPLATE_OPTION_EDITABLE_DEFAULTS, \
          .details.editable_defaults = (ed)                                  \
        }


/**
 * Set the requested options for the operation.
 *
 * @param tph 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_patch_private_template_set_options_ (
  struct TALER_MERCHANT_PatchPrivateTemplateHandle *tph,
  unsigned int num_options,
  const struct TALER_MERCHANT_PatchPrivateTemplateOptionValue *options);


/**
 * Set the requested options for the operation.
 *
 * @param tph the request to set the options for
 * @param ... the list of the options
 * @return #GNUNET_OK on success,
 *         #GNUNET_NO on failure,
 *         #GNUNET_SYSERR on internal error
 */
#define TALER_MERCHANT_patch_private_template_set_options(tph,...)                \
        TALER_MERCHANT_patch_private_template_set_options_ (                       \
          tph,                                                                     \
          TALER_MERCHANT_COMMON_OPTIONS_ARRAY_MAX_SIZE,                           \
          ((const struct TALER_MERCHANT_PatchPrivateTemplateOptionValue[])         \
           {__VA_ARGS__, TALER_MERCHANT_patch_private_template_option_end_ () }   \
          ))


/**
 * Response details for a PATCH /private/templates/$TEMPLATE_ID request.
 */
struct TALER_MERCHANT_PatchPrivateTemplateResponse
{
  /**
   * HTTP response details.
   */
  struct TALER_MERCHANT_HttpResponse hr;
};


#ifndef TALER_MERCHANT_PATCH_PRIVATE_TEMPLATE_RESULT_CLOSURE
/**
 * Type of the closure used by
 * the #TALER_MERCHANT_PatchPrivateTemplateCallback.
 */
#define TALER_MERCHANT_PATCH_PRIVATE_TEMPLATE_RESULT_CLOSURE void
#endif /* TALER_MERCHANT_PATCH_PRIVATE_TEMPLATE_RESULT_CLOSURE */

/**
 * Callback for a PATCH /private/templates/$TEMPLATE_ID request.
 *
 * @param cls closure
 * @param result response details
 */
typedef void
(*TALER_MERCHANT_PatchPrivateTemplateCallback)(
  TALER_MERCHANT_PATCH_PRIVATE_TEMPLATE_RESULT_CLOSURE *cls,
  const struct TALER_MERCHANT_PatchPrivateTemplateResponse *result);


/**
 * Start PATCH /private/templates/$TEMPLATE_ID operation.
 *
 * @param[in,out] handle operation to start
 * @param cb function to call with the result
 * @param cb_cls closure for @a cb
 * @return status code, #TALER_EC_NONE on success
 */
enum TALER_ErrorCode
TALER_MERCHANT_patch_private_template_start (
  struct TALER_MERCHANT_PatchPrivateTemplateHandle *handle,
  TALER_MERCHANT_PatchPrivateTemplateCallback cb,
  TALER_MERCHANT_PATCH_PRIVATE_TEMPLATE_RESULT_CLOSURE *cb_cls);


/**
 * Cancel PATCH /private/templates/$TEMPLATE_ID operation.
 * This function must not be called by clients after the
 * callback has been invoked.
 *
 * @param[in] handle operation to cancel
 */
void
TALER_MERCHANT_patch_private_template_cancel (
  struct TALER_MERCHANT_PatchPrivateTemplateHandle *handle);


#endif /* _TALER_MERCHANT__PATCH_PRIVATE_TEMPLATES_TEMPLATE_ID_H */
