/*
  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-management-instances.h
 * @brief C interface for the POST /management/instances endpoint
 * @author Christian Grothoff
 */
#ifndef _TALER_MERCHANT__POST_MANAGEMENT_INSTANCES_H
#define _TALER_MERCHANT__POST_MANAGEMENT_INSTANCES_H

#include <taler/merchant/common.h>


/**
 * Possible options we can set for the POST /management/instances request.
 */
enum TALER_MERCHANT_PostManagementInstancesOption
{
  /**
   * End of list of options.
   */
  TALER_MERCHANT_POST_MANAGEMENT_INSTANCES_OPTION_END = 0,

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

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

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

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

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

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

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

  /**
   * Set the logo.
   */
  TALER_MERCHANT_POST_MANAGEMENT_INSTANCES_OPTION_LOGO,

  /**
   * Set the authentication password.
   */
  TALER_MERCHANT_POST_MANAGEMENT_INSTANCES_OPTION_AUTH_PASSWORD

};


/**
 * Value for an option for the POST /management/instances request.
 */
struct TALER_MERCHANT_PostManagementInstancesOptionValue
{

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

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

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

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

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

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

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

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

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

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

    /**
     * Value if @e option is
     * #TALER_MERCHANT_POST_MANAGEMENT_INSTANCES_OPTION_AUTH_PASSWORD.
     */
    const char *auth_password;

  } details;

};


/**
 * Handle for a POST /management/instances request.
 */
struct TALER_MERCHANT_PostManagementInstancesHandle;


/**
 * Response details for a POST /management/instances request.
 */
struct TALER_MERCHANT_PostManagementInstancesResponse
{

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

  /**
   * Details depending on the HTTP status code.
   */
  union
  {
    /**
     * Details on #MHD_HTTP_ACCEPTED.
     */
    struct TALER_MERCHANT_MfaChallengeResponse accepted;
  } details;

};


/**
 * Set up POST /management/instances 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 for the new instance
 * @param name human-readable name
 * @param address address (JSON)
 * @param jurisdiction jurisdiction (JSON)
 * @param use_stefan whether to use the STEFAN curve for fee calculations
 * @return handle to operation
 */
struct TALER_MERCHANT_PostManagementInstancesHandle *
TALER_MERCHANT_post_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_PostManagementInstancesOptionValue
 */
#define TALER_MERCHANT_post_management_instances_option_end_()                 \
        (const struct TALER_MERCHANT_PostManagementInstancesOptionValue)        \
        {                                                                       \
          .option = TALER_MERCHANT_POST_MANAGEMENT_INSTANCES_OPTION_END        \
        }

/**
 * Set default pay delay.
 *
 * @param d delay to set
 * @return representation of the option
 */
#define TALER_MERCHANT_post_management_instances_option_default_pay_delay(d)   \
        (const struct TALER_MERCHANT_PostManagementInstancesOptionValue)        \
        {                                                                       \
          .option =                                                             \
            TALER_MERCHANT_POST_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_post_management_instances_option_default_refund_delay(d)   \
        (const struct TALER_MERCHANT_PostManagementInstancesOptionValue)           \
        {                                                                          \
          .option =                                                                \
            TALER_MERCHANT_POST_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_post_management_instances_option_default_wire_transfer_delay(d) \
        (const struct TALER_MERCHANT_PostManagementInstancesOptionValue)                  \
        {                                                                                 \
          .option =                                                                       \
            TALER_MERCHANT_POST_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_post_management_instances_option_default_wire_transfer_rounding_interval(ri) \
        (const struct TALER_MERCHANT_PostManagementInstancesOptionValue)                             \
        {                                                                                            \
          .option =                                                                                  \
            TALER_MERCHANT_POST_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_post_management_instances_option_email(e)                \
        (const struct TALER_MERCHANT_PostManagementInstancesOptionValue)         \
        {                                                                        \
          .option =                                                              \
            TALER_MERCHANT_POST_MANAGEMENT_INSTANCES_OPTION_EMAIL,              \
          .details.email = (e)                                                   \
        }

/**
 * Set phone number.
 *
 * @param p phone number to set
 * @return representation of the option
 */
#define TALER_MERCHANT_post_management_instances_option_phone_number(p)         \
        (const struct TALER_MERCHANT_PostManagementInstancesOptionValue)         \
        {                                                                        \
          .option =                                                              \
            TALER_MERCHANT_POST_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_post_management_instances_option_website(w)              \
        (const struct TALER_MERCHANT_PostManagementInstancesOptionValue)         \
        {                                                                        \
          .option =                                                              \
            TALER_MERCHANT_POST_MANAGEMENT_INSTANCES_OPTION_WEBSITE,            \
          .details.website = (w)                                                 \
        }

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

/**
 * Set authentication password.
 *
 * @param pw password to set
 * @return representation of the option
 */
#define TALER_MERCHANT_post_management_instances_option_auth_password(pw)       \
        (const struct TALER_MERCHANT_PostManagementInstancesOptionValue)         \
        {                                                                        \
          .option =                                                              \
            TALER_MERCHANT_POST_MANAGEMENT_INSTANCES_OPTION_AUTH_PASSWORD,      \
          .details.auth_password = (pw)                                          \
        }


/**
 * 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_post_management_instances_set_options_ (
  struct TALER_MERCHANT_PostManagementInstancesHandle *pmih,
  unsigned int num_options,
  const struct TALER_MERCHANT_PostManagementInstancesOptionValue *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_management_instances_set_options (
 *   pmih,
 *   TALER_MERCHANT_post_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_post_management_instances_option_NAME(VALUE)
 * @return #GNUNET_OK on success,
 *         #GNUNET_NO on failure,
 *         #GNUNET_SYSERR on internal error
 */
#define TALER_MERCHANT_post_management_instances_set_options(pmih,...)                \
        TALER_MERCHANT_post_management_instances_set_options_ (                       \
          pmih,                                                                       \
          TALER_MERCHANT_COMMON_OPTIONS_ARRAY_MAX_SIZE,                              \
          ((const struct TALER_MERCHANT_PostManagementInstancesOptionValue[])         \
           {__VA_ARGS__, TALER_MERCHANT_post_management_instances_option_end_ () \
           }   \
          ))


#ifndef TALER_MERCHANT_POST_MANAGEMENT_INSTANCES_RESULT_CLOSURE
/**
 * Type of the closure used by
 * the #TALER_MERCHANT_PostManagementInstancesCallback.
 */
#define TALER_MERCHANT_POST_MANAGEMENT_INSTANCES_RESULT_CLOSURE void
#endif /* TALER_MERCHANT_POST_MANAGEMENT_INSTANCES_RESULT_CLOSURE */

/**
 * Callback for a POST /management/instances request.
 *
 * @param cls closure
 * @param mir response details
 */
typedef void
(*TALER_MERCHANT_PostManagementInstancesCallback)(
  TALER_MERCHANT_POST_MANAGEMENT_INSTANCES_RESULT_CLOSURE *cls,
  const struct TALER_MERCHANT_PostManagementInstancesResponse *mir);


/**
 * Start POST /management/instances operation.
 *
 * @param[in,out] pmih 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_management_instances_start (
  struct TALER_MERCHANT_PostManagementInstancesHandle *pmih,
  TALER_MERCHANT_PostManagementInstancesCallback cb,
  TALER_MERCHANT_POST_MANAGEMENT_INSTANCES_RESULT_CLOSURE *cb_cls);


/**
 * Cancel POST /management/instances operation.  This function must not be
 * called by clients after the TALER_MERCHANT_PostManagementInstancesCallback
 * has been invoked (as in those cases it'll be called internally by the
 * implementation already).
 *
 * @param[in] pmih operation to cancel
 */
void
TALER_MERCHANT_post_management_instances_cancel (
  struct TALER_MERCHANT_PostManagementInstancesHandle *pmih);


#endif /* _TALER_MERCHANT__POST_MANAGEMENT_INSTANCES_H */
