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

#include <taler/merchant/common.h>


/**
 * Possible options for the POST /private/units request.
 */
enum TALER_MERCHANT_PostPrivateUnitsOption
{
  /**
   * End of list of options.
   */
  TALER_MERCHANT_POST_PRIVATE_UNITS_OPTION_END = 0,

  /**
   * Internationalized long names (JSON).
   */
  TALER_MERCHANT_POST_PRIVATE_UNITS_OPTION_UNIT_NAME_LONG_I18N,

  /**
   * Internationalized short names (JSON).
   */
  TALER_MERCHANT_POST_PRIVATE_UNITS_OPTION_UNIT_NAME_SHORT_I18N,

  /**
   * Whether fractional quantities are allowed (bool).
   * Default: false.
   */
  TALER_MERCHANT_POST_PRIVATE_UNITS_OPTION_UNIT_ALLOW_FRACTION,

  /**
   * Precision level for fractional quantities (uint32_t).
   * Default: 0.
   */
  TALER_MERCHANT_POST_PRIVATE_UNITS_OPTION_UNIT_PRECISION_LEVEL,

  /**
   * Whether the unit is active (bool).
   * Default: true.
   */
  TALER_MERCHANT_POST_PRIVATE_UNITS_OPTION_UNIT_ACTIVE

};


/**
 * Value for an option for the POST /private/units request.
 */
struct TALER_MERCHANT_PostPrivateUnitsOptionValue
{

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

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

    /**
     * Value if @e option is
     * #TALER_MERCHANT_POST_PRIVATE_UNITS_OPTION_UNIT_NAME_LONG_I18N.
     */
    const json_t *unit_name_long_i18n;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_POST_PRIVATE_UNITS_OPTION_UNIT_NAME_SHORT_I18N.
     */
    const json_t *unit_name_short_i18n;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_POST_PRIVATE_UNITS_OPTION_UNIT_ALLOW_FRACTION.
     */
    bool unit_allow_fraction;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_POST_PRIVATE_UNITS_OPTION_UNIT_PRECISION_LEVEL.
     */
    uint32_t unit_precision_level;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_POST_PRIVATE_UNITS_OPTION_UNIT_ACTIVE.
     */
    bool unit_active;

  } details;

};


/**
 * Handle for a POST /private/units request.
 */
struct TALER_MERCHANT_PostPrivateUnitsHandle;


/**
 * Response details for a POST /private/units request.
 */
struct TALER_MERCHANT_PostPrivateUnitsResponse
{

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

};


/**
 * Terminate the list of the options.
 *
 * @return the terminating object
 */
#define TALER_MERCHANT_post_private_units_option_end_()                \
        (const struct TALER_MERCHANT_PostPrivateUnitsOptionValue)      \
        {                                                               \
          .option = TALER_MERCHANT_POST_PRIVATE_UNITS_OPTION_END       \
        }

/**
 * Set internationalized long names.
 *
 * @param j long names JSON object
 * @return representation of the option
 */
#define TALER_MERCHANT_post_private_units_option_unit_name_long_i18n(j)       \
        (const struct TALER_MERCHANT_PostPrivateUnitsOptionValue)              \
        {                                                                       \
          .option = TALER_MERCHANT_POST_PRIVATE_UNITS_OPTION_UNIT_NAME_LONG_I18N \
          , \
          .details.unit_name_long_i18n = (j)                                    \
        }

/**
 * Set internationalized short names.
 *
 * @param j short names JSON object
 * @return representation of the option
 */
#define TALER_MERCHANT_post_private_units_option_unit_name_short_i18n(j)       \
        (const struct TALER_MERCHANT_PostPrivateUnitsOptionValue)               \
        {                                                                        \
          .option = \
            TALER_MERCHANT_POST_PRIVATE_UNITS_OPTION_UNIT_NAME_SHORT_I18N, \
          .details.unit_name_short_i18n = (j)                                    \
        }


/**
 * Set whether fractional quantities are allowed.
 *
 * @param v true to allow fractions
 * @return representation of the option
 */
#define TALER_MERCHANT_post_private_units_option_unit_allow_fraction(v)         \
        (const struct TALER_MERCHANT_PostPrivateUnitsOptionValue)               \
        {                                                                        \
          .option =                                                              \
            TALER_MERCHANT_POST_PRIVATE_UNITS_OPTION_UNIT_ALLOW_FRACTION,       \
          .details.unit_allow_fraction = (v)                                     \
        }

/**
 * Set the precision level for fractional quantities.
 *
 * @param v precision level
 * @return representation of the option
 */
#define TALER_MERCHANT_post_private_units_option_unit_precision_level(v)        \
        (const struct TALER_MERCHANT_PostPrivateUnitsOptionValue)               \
        {                                                                        \
          .option =                                                              \
            TALER_MERCHANT_POST_PRIVATE_UNITS_OPTION_UNIT_PRECISION_LEVEL,      \
          .details.unit_precision_level = (v)                                    \
        }

/**
 * Set whether the unit is active.
 *
 * @param v true if active
 * @return representation of the option
 */
#define TALER_MERCHANT_post_private_units_option_unit_active(v)                \
        (const struct TALER_MERCHANT_PostPrivateUnitsOptionValue)               \
        {                                                                        \
          .option =                                                              \
            TALER_MERCHANT_POST_PRIVATE_UNITS_OPTION_UNIT_ACTIVE,              \
          .details.unit_active = (v)                                             \
        }


/**
 * Set the requested options for the operation.
 *
 * @param ppuh 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_units_set_options_ (
  struct TALER_MERCHANT_PostPrivateUnitsHandle *ppuh,
  unsigned int num_options,
  const struct TALER_MERCHANT_PostPrivateUnitsOptionValue *options);


/**
 * Set the requested options for the operation.
 *
 * @param ppuh 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_post_private_units_set_options(ppuh,...)                \
        TALER_MERCHANT_post_private_units_set_options_ (                       \
          ppuh,                                                                \
          TALER_MERCHANT_COMMON_OPTIONS_ARRAY_MAX_SIZE,                       \
          ((const struct TALER_MERCHANT_PostPrivateUnitsOptionValue[])         \
           {__VA_ARGS__, TALER_MERCHANT_post_private_units_option_end_ () }   \
          ))


/**
 * Set up POST /private/units 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 unit_id identifier for the new unit
 * @param unit_name_long long human-readable name
 * @param unit_name_short short symbol for the unit
 * @return handle to operation
 */
struct TALER_MERCHANT_PostPrivateUnitsHandle *
TALER_MERCHANT_post_private_units_create (
  struct GNUNET_CURL_Context *ctx,
  const char *url,
  const char *unit_id,
  const char *unit_name_long,
  const char *unit_name_short);


#ifndef TALER_MERCHANT_POST_PRIVATE_UNITS_RESULT_CLOSURE
/**
 * Type of the closure used by
 * the #TALER_MERCHANT_PostPrivateUnitsCallback.
 */
#define TALER_MERCHANT_POST_PRIVATE_UNITS_RESULT_CLOSURE void
#endif /* TALER_MERCHANT_POST_PRIVATE_UNITS_RESULT_CLOSURE */

/**
 * Callback for a POST /private/units request.
 *
 * @param cls closure
 * @param pur response details
 */
typedef void
(*TALER_MERCHANT_PostPrivateUnitsCallback)(
  TALER_MERCHANT_POST_PRIVATE_UNITS_RESULT_CLOSURE *cls,
  const struct TALER_MERCHANT_PostPrivateUnitsResponse *pur);


/**
 * Start POST /private/units operation.
 *
 * @param[in,out] ppuh 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_units_start (
  struct TALER_MERCHANT_PostPrivateUnitsHandle *ppuh,
  TALER_MERCHANT_PostPrivateUnitsCallback cb,
  TALER_MERCHANT_POST_PRIVATE_UNITS_RESULT_CLOSURE *cb_cls);


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


#endif /* _TALER_MERCHANT__POST_PRIVATE_UNITS_H */
