/*
  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-management-instances-INSTANCE.h
 * @brief C interface for PATCH /management/instances/$INSTANCE
 * @author Christian Grothoff
 */
#ifndef _TALER_MERCHANT__PATCH_MANAGEMENT_INSTANCES_INSTANCE_H
#define _TALER_MERCHANT__PATCH_MANAGEMENT_INSTANCES_INSTANCE_H

#include <taler/merchant/common.h>


/**
 * Possible options we can set for the PATCH /management/instances/$INSTANCE request.
 */
enum TALER_MERCHANT_PatchManagementInstancesOption
{
  /**
   * End of list of options.
   */
  TALER_MERCHANT_PATCH_MANAGEMENT_INSTANCES_OPTION_END = 0,

  /**
   * Set the default pay delay.
   */
  TALER_MERCHANT_PATCH_MANAGEMENT_INSTANCES_OPTION_DEFAULT_PAY_DELAY,

  /**
   * Set the default refund delay.
   */
  TALER_MERCHANT_PATCH_MANAGEMENT_INSTANCES_OPTION_DEFAULT_REFUND_DELAY,

  /**
   * Set the default wire transfer delay.
   */
  TALER_MERCHANT_PATCH_MANAGEMENT_INSTANCES_OPTION_DEFAULT_WIRE_TRANSFER_DELAY,

  /**
   * Set the default wire transfer rounding interval.
   */
  TALER_MERCHANT_PATCH_MANAGEMENT_INSTANCES_OPTION_DEFAULT_WIRE_TRANSFER_ROUNDING_INTERVAL,

  /**
   * Set the email address.
   */
  TALER_MERCHANT_PATCH_MANAGEMENT_INSTANCES_OPTION_EMAIL,

  /**
   * Set the phone number.
   */
  TALER_MERCHANT_PATCH_MANAGEMENT_INSTANCES_OPTION_PHONE_NUMBER,

  /**
   * Set the website URL.
   */
  TALER_MERCHANT_PATCH_MANAGEMENT_INSTANCES_OPTION_WEBSITE,

  /**
   * Set the logo.
   */
  TALER_MERCHANT_PATCH_MANAGEMENT_INSTANCES_OPTION_LOGO

};


/**
 * Value for an option for the PATCH /management/instances/$INSTANCE request.
 */
struct TALER_MERCHANT_PatchManagementInstancesOptionValue
{

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

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

    /**
     * Value if @e option is
     * #TALER_MERCHANT_PATCH_MANAGEMENT_INSTANCES_OPTION_DEFAULT_PAY_DELAY.
     */
    struct GNUNET_TIME_Relative default_pay_delay;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_PATCH_MANAGEMENT_INSTANCES_OPTION_DEFAULT_REFUND_DELAY.
     */
    struct GNUNET_TIME_Relative default_refund_delay;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_PATCH_MANAGEMENT_INSTANCES_OPTION_DEFAULT_WIRE_TRANSFER_DELAY.
     */
    struct GNUNET_TIME_Relative default_wire_transfer_delay;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_PATCH_MANAGEMENT_INSTANCES_OPTION_DEFAULT_WIRE_TRANSFER_ROUNDING_INTERVAL.
     */
    enum GNUNET_TIME_RounderInterval default_wire_transfer_rounding_interval;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_PATCH_MANAGEMENT_INSTANCES_OPTION_EMAIL.
     */
    const char *email;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_PATCH_MANAGEMENT_INSTANCES_OPTION_PHONE_NUMBER.
     */
    const char *phone_number;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_PATCH_MANAGEMENT_INSTANCES_OPTION_WEBSITE.
     */
    const char *website;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_PATCH_MANAGEMENT_INSTANCES_OPTION_LOGO.
     */
    const char *logo;

  } details;

};


/**
 * Handle for a PATCH /management/instances/$INSTANCE operation.
 */
struct TALER_MERCHANT_PatchManagementInstancesHandle;


/**
 * Set up PATCH /management/instances/$INSTANCE 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 instance_id identifier of the instance to patch
 * @param name new human-readable name
 * @param address new address (JSON)
 * @param jurisdiction new jurisdiction (JSON)
 * @param use_stefan whether to use the STEFAN curve for fee calculations
 * @return handle to operation, NULL on error
 */
struct TALER_MERCHANT_PatchManagementInstancesHandle *
TALER_MERCHANT_patch_management_instances_create (
  struct GNUNET_CURL_Context *ctx,
  const char *url,
  const char *instance_id,
  const char *name,
  const json_t *address,
  const json_t *jurisdiction,
  bool use_stefan);


/**
 * Terminate the list of the options.
 *
 * @return the terminating object of struct TALER_MERCHANT_PatchManagementInstancesOptionValue
 */
#define TALER_MERCHANT_patch_management_instances_option_end_()                  \
        (const struct TALER_MERCHANT_PatchManagementInstancesOptionValue)         \
        {                                                                         \
          .option = TALER_MERCHANT_PATCH_MANAGEMENT_INSTANCES_OPTION_END         \
        }

/**
 * Set default pay delay.
 *
 * @param d delay to set
 * @return representation of the option
 */
#define TALER_MERCHANT_patch_management_instances_option_default_pay_delay(d)    \
        (const struct TALER_MERCHANT_PatchManagementInstancesOptionValue)         \
        {                                                                         \
          .option =                                                               \
            TALER_MERCHANT_PATCH_MANAGEMENT_INSTANCES_OPTION_DEFAULT_PAY_DELAY,  \
          .details.default_pay_delay = (d)                                        \
        }

/**
 * Set default refund delay.
 *
 * @param d delay to set
 * @return representation of the option
 */
#define TALER_MERCHANT_patch_management_instances_option_default_refund_delay(d)    \
        (const struct TALER_MERCHANT_PatchManagementInstancesOptionValue)            \
        {                                                                            \
          .option =                                                                  \
            TALER_MERCHANT_PATCH_MANAGEMENT_INSTANCES_OPTION_DEFAULT_REFUND_DELAY,  \
          .details.default_refund_delay = (d)                                        \
        }

/**
 * Set default wire transfer delay.
 *
 * @param d delay to set
 * @return representation of the option
 */
#define \
        TALER_MERCHANT_patch_management_instances_option_default_wire_transfer_delay(d)    \
        (const struct TALER_MERCHANT_PatchManagementInstancesOptionValue)                   \
        {                                                                                   \
          .option =                                                                         \
            TALER_MERCHANT_PATCH_MANAGEMENT_INSTANCES_OPTION_DEFAULT_WIRE_TRANSFER_DELAY,  \
          .details.default_wire_transfer_delay = (d)                                        \
        }

/**
 * Set default wire transfer rounding interval.
 *
 * @param ri rounding interval to set
 * @return representation of the option
 */
#define \
        TALER_MERCHANT_patch_management_instances_option_default_wire_transfer_rounding_interval(ri) \
        (const struct TALER_MERCHANT_PatchManagementInstancesOptionValue)                             \
        {                                                                                             \
          .option =                                                                                   \
            TALER_MERCHANT_PATCH_MANAGEMENT_INSTANCES_OPTION_DEFAULT_WIRE_TRANSFER_ROUNDING_INTERVAL, \
          .details.default_wire_transfer_rounding_interval = (ri)                                     \
        }

/**
 * Set email address.
 *
 * @param e email to set
 * @return representation of the option
 */
#define TALER_MERCHANT_patch_management_instances_option_email(e)                 \
        (const struct TALER_MERCHANT_PatchManagementInstancesOptionValue)          \
        {                                                                          \
          .option =                                                                \
            TALER_MERCHANT_PATCH_MANAGEMENT_INSTANCES_OPTION_EMAIL,               \
          .details.email = (e)                                                     \
        }

/**
 * Set phone number.
 *
 * @param p phone number to set
 * @return representation of the option
 */
#define TALER_MERCHANT_patch_management_instances_option_phone_number(p)          \
        (const struct TALER_MERCHANT_PatchManagementInstancesOptionValue)          \
        {                                                                          \
          .option =                                                                \
            TALER_MERCHANT_PATCH_MANAGEMENT_INSTANCES_OPTION_PHONE_NUMBER,        \
          .details.phone_number = (p)                                              \
        }

/**
 * Set website URL.
 *
 * @param w website to set
 * @return representation of the option
 */
#define TALER_MERCHANT_patch_management_instances_option_website(w)               \
        (const struct TALER_MERCHANT_PatchManagementInstancesOptionValue)          \
        {                                                                          \
          .option =                                                                \
            TALER_MERCHANT_PATCH_MANAGEMENT_INSTANCES_OPTION_WEBSITE,             \
          .details.website = (w)                                                   \
        }

/**
 * Set logo.
 *
 * @param l logo to set
 * @return representation of the option
 */
#define TALER_MERCHANT_patch_management_instances_option_logo(l)                  \
        (const struct TALER_MERCHANT_PatchManagementInstancesOptionValue)          \
        {                                                                          \
          .option =                                                                \
            TALER_MERCHANT_PATCH_MANAGEMENT_INSTANCES_OPTION_LOGO,               \
          .details.logo = (l)                                                      \
        }


/**
 * Set the requested options for the operation.
 *
 * If any option fail other options may be or may be not applied.
 *
 * @param pmih 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_management_instances_set_options_ (
  struct TALER_MERCHANT_PatchManagementInstancesHandle *pmih,
  unsigned int num_options,
  const struct TALER_MERCHANT_PatchManagementInstancesOptionValue *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_patch_management_instances_set_options (
 *   pmih,
 *   TALER_MERCHANT_patch_management_instances_option_email (
 *     "foo@example.com"));
 *
 * @param pmih the request to set the options for
 * @param ... the list of the options, each option must be created
 *            by helpers TALER_MERCHANT_patch_management_instances_option_NAME(VALUE)
 * @return #GNUNET_OK on success,
 *         #GNUNET_NO on failure,
 *         #GNUNET_SYSERR on internal error
 */
#define TALER_MERCHANT_patch_management_instances_set_options(pmih,...)                 \
        TALER_MERCHANT_patch_management_instances_set_options_ (                         \
          pmih,                                                                          \
          TALER_MERCHANT_COMMON_OPTIONS_ARRAY_MAX_SIZE,                                 \
          ((const struct TALER_MERCHANT_PatchManagementInstancesOptionValue[])           \
           {__VA_ARGS__, TALER_MERCHANT_patch_management_instances_option_end_ \
              () }     \
          ))


/**
 * Response details for a PATCH /management/instances/$INSTANCE request.
 */
struct TALER_MERCHANT_PatchManagementInstancesResponse
{
  /**
   * HTTP response details.
   */
  struct TALER_MERCHANT_HttpResponse hr;
};


#ifndef TALER_MERCHANT_PATCH_MANAGEMENT_INSTANCES_RESULT_CLOSURE
/**
 * Type of the closure used by
 * the #TALER_MERCHANT_PatchManagementInstancesCallback.
 */
#define TALER_MERCHANT_PATCH_MANAGEMENT_INSTANCES_RESULT_CLOSURE void
#endif /* TALER_MERCHANT_PATCH_MANAGEMENT_INSTANCES_RESULT_CLOSURE */

/**
 * Callback for a PATCH /management/instances/$INSTANCE request.
 *
 * @param cls closure
 * @param result response details
 */
typedef void
(*TALER_MERCHANT_PatchManagementInstancesCallback)(
  TALER_MERCHANT_PATCH_MANAGEMENT_INSTANCES_RESULT_CLOSURE *cls,
  const struct TALER_MERCHANT_PatchManagementInstancesResponse *result);


/**
 * Start PATCH /management/instances/$INSTANCE 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_management_instances_start (
  struct TALER_MERCHANT_PatchManagementInstancesHandle *handle,
  TALER_MERCHANT_PatchManagementInstancesCallback cb,
  TALER_MERCHANT_PATCH_MANAGEMENT_INSTANCES_RESULT_CLOSURE *cb_cls);


/**
 * Cancel PATCH /management/instances/$INSTANCE 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_management_instances_cancel (
  struct TALER_MERCHANT_PatchManagementInstancesHandle *handle);


#endif /* _TALER_MERCHANT__PATCH_MANAGEMENT_INSTANCES_INSTANCE_H */
