/*
  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-units-UNIT.h
 * @brief C interface for PATCH /private/units/$UNIT
 * @author Christian Grothoff
 */
#ifndef _TALER_MERCHANT__PATCH_PRIVATE_UNITS_UNIT_H
#define _TALER_MERCHANT__PATCH_PRIVATE_UNITS_UNIT_H

#include <taler/merchant/common.h>


/**
 * Possible options we can set for the PATCH /private/units/$UNIT request.
 */
enum TALER_MERCHANT_PatchPrivateUnitOption
{
  /**
   * End of list of options.
   */
  TALER_MERCHANT_PATCH_PRIVATE_UNIT_OPTION_END = 0,

  /**
   * Set the long name of the unit.
   */
  TALER_MERCHANT_PATCH_PRIVATE_UNIT_OPTION_UNIT_NAME_LONG,

  /**
   * Set the short name (symbol) of the unit.
   */
  TALER_MERCHANT_PATCH_PRIVATE_UNIT_OPTION_UNIT_NAME_SHORT,

  /**
   * Set internationalized long names (JSON).
   */
  TALER_MERCHANT_PATCH_PRIVATE_UNIT_OPTION_UNIT_NAME_LONG_I18N,

  /**
   * Set internationalized short names (JSON).
   */
  TALER_MERCHANT_PATCH_PRIVATE_UNIT_OPTION_UNIT_NAME_SHORT_I18N,

  /**
   * Set whether fractional quantities are allowed.
   */
  TALER_MERCHANT_PATCH_PRIVATE_UNIT_OPTION_UNIT_ALLOW_FRACTION,

  /**
   * Set precision level for fractions.
   */
  TALER_MERCHANT_PATCH_PRIVATE_UNIT_OPTION_UNIT_PRECISION_LEVEL,

  /**
   * Set active status of the unit.
   */
  TALER_MERCHANT_PATCH_PRIVATE_UNIT_OPTION_UNIT_ACTIVE

};


/**
 * Value for an option for the PATCH /private/units/$UNIT request.
 */
struct TALER_MERCHANT_PatchPrivateUnitOptionValue
{

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

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

    /**
     * Value if @e option is
     * #TALER_MERCHANT_PATCH_PRIVATE_UNIT_OPTION_UNIT_NAME_LONG.
     */
    const char *unit_name_long;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_PATCH_PRIVATE_UNIT_OPTION_UNIT_NAME_SHORT.
     */
    const char *unit_name_short;

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

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

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

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

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

  } details;

};


/**
 * Handle for a PATCH /private/units/$UNIT operation.
 */
struct TALER_MERCHANT_PatchPrivateUnitHandle;


/**
 * Set up PATCH /private/units/$UNIT 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 of the unit to update
 * @return handle to operation, NULL on error
 */
struct TALER_MERCHANT_PatchPrivateUnitHandle *
TALER_MERCHANT_patch_private_unit_create (
  struct GNUNET_CURL_Context *ctx,
  const char *url,
  const char *unit_id);


/**
 * Terminate the list of the options.
 *
 * @return the terminating object of struct TALER_MERCHANT_PatchPrivateUnitOptionValue
 */
#define TALER_MERCHANT_patch_private_unit_option_end_()                \
        (const struct TALER_MERCHANT_PatchPrivateUnitOptionValue)       \
        {                                                               \
          .option = TALER_MERCHANT_PATCH_PRIVATE_UNIT_OPTION_END       \
        }

/**
 * Set the long name of the unit.
 *
 * @param n long name to set
 * @return representation of the option as a struct TALER_MERCHANT_PatchPrivateUnitOptionValue
 */
#define TALER_MERCHANT_patch_private_unit_option_unit_name_long(n)              \
        (const struct TALER_MERCHANT_PatchPrivateUnitOptionValue)                \
        {                                                                        \
          .option = TALER_MERCHANT_PATCH_PRIVATE_UNIT_OPTION_UNIT_NAME_LONG,   \
          .details.unit_name_long = (n)                                          \
        }

/**
 * Set the short name (symbol) of the unit.
 *
 * @param n short name to set
 * @return representation of the option as a struct TALER_MERCHANT_PatchPrivateUnitOptionValue
 */
#define TALER_MERCHANT_patch_private_unit_option_unit_name_short(n)              \
        (const struct TALER_MERCHANT_PatchPrivateUnitOptionValue)                 \
        {                                                                         \
          .option = TALER_MERCHANT_PATCH_PRIVATE_UNIT_OPTION_UNIT_NAME_SHORT,   \
          .details.unit_name_short = (n)                                          \
        }

/**
 * Set internationalized long names.
 *
 * @param j JSON object with internationalized long names
 * @return representation of the option as a struct TALER_MERCHANT_PatchPrivateUnitOptionValue
 */
#define TALER_MERCHANT_patch_private_unit_option_unit_name_long_i18n(j)              \
        (const struct TALER_MERCHANT_PatchPrivateUnitOptionValue)                     \
        {                                                                             \
          .option = TALER_MERCHANT_PATCH_PRIVATE_UNIT_OPTION_UNIT_NAME_LONG_I18N \
          ,   \
          .details.unit_name_long_i18n = (j)                                          \
        }

/**
 * Set internationalized short names.
 *
 * @param j JSON object with internationalized short names
 * @return representation of the option as a struct TALER_MERCHANT_PatchPrivateUnitOptionValue
 */
#define TALER_MERCHANT_patch_private_unit_option_unit_name_short_i18n(j)              \
        (const struct TALER_MERCHANT_PatchPrivateUnitOptionValue)                      \
        {                                                                              \
          .option = \
            TALER_MERCHANT_PATCH_PRIVATE_UNIT_OPTION_UNIT_NAME_SHORT_I18N,   \
          .details.unit_name_short_i18n = (j)                                          \
        }

/**
 * Set whether fractional quantities are allowed.
 *
 * @param a true if fractional quantities are allowed
 * @return representation of the option as a struct TALER_MERCHANT_PatchPrivateUnitOptionValue
 */
#define TALER_MERCHANT_patch_private_unit_option_unit_allow_fraction(a)              \
        (const struct TALER_MERCHANT_PatchPrivateUnitOptionValue)                     \
        {                                                                             \
          .option = TALER_MERCHANT_PATCH_PRIVATE_UNIT_OPTION_UNIT_ALLOW_FRACTION \
          ,   \
          .details.unit_allow_fraction = (a)                                          \
        }

/**
 * Set precision level for fractions.
 *
 * @param p precision level value
 * @return representation of the option as a struct TALER_MERCHANT_PatchPrivateUnitOptionValue
 */
#define TALER_MERCHANT_patch_private_unit_option_unit_precision_level(p)              \
        (const struct TALER_MERCHANT_PatchPrivateUnitOptionValue)                      \
        {                                                                              \
          .option = \
            TALER_MERCHANT_PATCH_PRIVATE_UNIT_OPTION_UNIT_PRECISION_LEVEL,   \
          .details.unit_precision_level = (p)                                          \
        }

/**
 * Set active status of the unit.
 *
 * @param a true if the unit should be active
 * @return representation of the option as a struct TALER_MERCHANT_PatchPrivateUnitOptionValue
 */
#define TALER_MERCHANT_patch_private_unit_option_unit_active(a)              \
        (const struct TALER_MERCHANT_PatchPrivateUnitOptionValue)             \
        {                                                                     \
          .option = TALER_MERCHANT_PATCH_PRIVATE_UNIT_OPTION_UNIT_ACTIVE,   \
          .details.unit_active = (a)                                          \
        }


/**
 * Set the requested options for the operation.
 *
 * If any option fail other options may be or may be not applied.
 *
 * @param uph 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_unit_set_options_ (
  struct TALER_MERCHANT_PatchPrivateUnitHandle *uph,
  unsigned int num_options,
  const struct TALER_MERCHANT_PatchPrivateUnitOptionValue *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_private_unit_set_options (
 *   uph,
 *   TALER_MERCHANT_patch_private_unit_option_unit_name_long ("Kilogram"),
 *   TALER_MERCHANT_patch_private_unit_option_unit_active (true));
 *
 * @param uph the request to set the options for
 * @param ... the list of the options, each option must be created
 *            by helpers TALER_MERCHANT_patch_private_unit_option_NAME(VALUE)
 * @return #GNUNET_OK on success,
 *         #GNUNET_NO on failure,
 *         #GNUNET_SYSERR on internal error
 */
#define TALER_MERCHANT_patch_private_unit_set_options(uph,...)                \
        TALER_MERCHANT_patch_private_unit_set_options_ (                       \
          uph,                                                                 \
          TALER_MERCHANT_COMMON_OPTIONS_ARRAY_MAX_SIZE,                       \
          ((const struct TALER_MERCHANT_PatchPrivateUnitOptionValue[])         \
           {__VA_ARGS__, TALER_MERCHANT_patch_private_unit_option_end_ () }   \
          ))


/**
 * Response details for a PATCH /private/units/$UNIT request.
 */
struct TALER_MERCHANT_PatchPrivateUnitResponse
{
  /**
   * HTTP response details.
   */
  struct TALER_MERCHANT_HttpResponse hr;
};


#ifndef TALER_MERCHANT_PATCH_PRIVATE_UNIT_RESULT_CLOSURE
/**
 * Type of the closure used by
 * the #TALER_MERCHANT_PatchPrivateUnitCallback.
 */
#define TALER_MERCHANT_PATCH_PRIVATE_UNIT_RESULT_CLOSURE void
#endif /* TALER_MERCHANT_PATCH_PRIVATE_UNIT_RESULT_CLOSURE */

/**
 * Callback for a PATCH /private/units/$UNIT request.
 *
 * @param cls closure
 * @param result response details
 */
typedef void
(*TALER_MERCHANT_PatchPrivateUnitCallback)(
  TALER_MERCHANT_PATCH_PRIVATE_UNIT_RESULT_CLOSURE *cls,
  const struct TALER_MERCHANT_PatchPrivateUnitResponse *result);


/**
 * Start PATCH /private/units/$UNIT operation.
 *
 * @param[in,out] uph 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_unit_start (
  struct TALER_MERCHANT_PatchPrivateUnitHandle *uph,
  TALER_MERCHANT_PatchPrivateUnitCallback cb,
  TALER_MERCHANT_PATCH_PRIVATE_UNIT_RESULT_CLOSURE *cb_cls);


/**
 * Cancel PATCH /private/units/$UNIT operation.
 * This function must not be called by clients after the
 * callback has been invoked.
 *
 * @param[in] uph operation to cancel
 */
void
TALER_MERCHANT_patch_private_unit_cancel (
  struct TALER_MERCHANT_PatchPrivateUnitHandle *uph);


#endif /* _TALER_MERCHANT__PATCH_PRIVATE_UNITS_UNIT_H */
