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

#include <taler/merchant/common.h>


/**
 * Possible options we can set for the PATCH /private/products/$PRODUCT_ID request.
 */
enum TALER_MERCHANT_PatchPrivateProductOption
{
  /**
   * End of list of options.
   */
  TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_END = 0,

  /**
   * Set value and fractional part of total stock.
   */
  TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_TOTAL_STOCK,

  /**
   * Set value part of total stock (-1 for unlimited).
   */
  TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_TOTAL_STOCK_VAL,

  /**
   * Set fractional part of total stock.
   */
  TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_TOTAL_STOCK_FRAC,

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

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

  /**
   * Internationalized descriptions (JSON).
   */
  TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_DESCRIPTION_I18N,

  /**
   * Product image (base64-encoded data URL).
   */
  TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_IMAGE,

  /**
   * Tax information (JSON array).
   */
  TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_TAXES,

  /**
   * Total units lost/expired.
   */
  TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_TOTAL_LOST,

  /**
   * Storage location (JSON).
   */
  TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_ADDRESS,

  /**
   * Expected restock time.
   */
  TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_NEXT_RESTOCK,

  /**
   * Explicit product name (distinct from description).
   */
  TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_PRODUCT_NAME,

  /**
   * Category IDs.
   */
  TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_CATEGORIES,

  /**
   * Minimum age requirement.
   */
  TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_MINIMUM_AGE,

  /**
   * Product group ID.
   */
  TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_PRODUCT_GROUP_ID,

  /**
   * Money pot ID.
   */
  TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_MONEY_POT_ID,

  /**
   * Whether the price is net (before tax).
   */
  TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_PRICE_IS_NET

};


/**
 * Value for an option for the PATCH /private/products/$PRODUCT_ID request.
 */
struct TALER_MERCHANT_PatchPrivateProductOptionValue
{

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

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

    /**
     * Value if @e option is
     * #TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_TOTAL_STOCK_FRAC.
     */
    uint32_t total_stock_frac;

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

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

    /**
     * Value if @e option is
     * #TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_DESCRIPTION_I18N.
     */
    const json_t *description_i18n;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_IMAGE.
     */
    const char *image;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_TAXES.
     */
    const json_t *taxes;

    struct
    {

      /**
       * Value if @e option is
       * #TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_TOTAL_STOCK.
       * -1 for infinite stock.
       */
      int64_t val;

      /**
       * Value if @e option is
       * #TALER_MERCHANT_POST_PRIVATE_PRODUCTS_OPTION_TOTAL_STOCK_FRAC.
       */
      uint32_t frac;
    } total_stock;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_TOTAL_LOST.
     */
    uint64_t total_lost;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_ADDRESS.
     */
    const json_t *address;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_NEXT_RESTOCK.
     */
    struct GNUNET_TIME_Timestamp next_restock;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_PRODUCT_NAME.
     */
    const char *product_name;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_CATEGORIES.
     */
    struct
    {
      unsigned int num;
      const uint64_t *cats;
    } categories;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_MINIMUM_AGE.
     */
    uint32_t minimum_age;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_PRODUCT_GROUP_ID.
     */
    uint64_t product_group_id;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_MONEY_POT_ID.
     */
    uint64_t money_pot_id;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_PRICE_IS_NET.
     */
    bool price_is_net;

  } details;

};


/**
 * Handle for a PATCH /private/products/$PRODUCT_ID operation.
 */
struct TALER_MERCHANT_PatchPrivateProductHandle;


/**
 * Set up PATCH /private/products/$PRODUCT_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 product_id identifier of the product to update
 * @param description new human-readable description
 * @param unit unit of measurement
 * @param num_prices length of the @e prices array
 * @param prices new unit prices
 * @return handle to operation, NULL on error
 */
struct TALER_MERCHANT_PatchPrivateProductHandle *
TALER_MERCHANT_patch_private_product_create (
  struct GNUNET_CURL_Context *ctx,
  const char *url,
  const char *product_id,
  const char *description,
  const char *unit,
  unsigned int num_prices,
  const struct TALER_Amount prices[static num_prices]);


/**
 * Terminate the list of the options.
 *
 * @return the terminating object of struct TALER_MERCHANT_PatchPrivateProductOptionValue
 */
#define TALER_MERCHANT_patch_private_product_option_end_()                \
        (const struct TALER_MERCHANT_PatchPrivateProductOptionValue)       \
        {                                                                  \
          .option = TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_END       \
        }

/**
 * Set value and fractional part of total stock.
 *
 * @param v value part of stock
 * @param f fractional stock value
 * @return representation of the option as a struct TALER_MERCHANT_PatchPrivateProductOptionValue
 */
#define TALER_MERCHANT_patch_private_product_option_total_stock(v,f)              \
        (const struct TALER_MERCHANT_PatchPrivateProductOptionValue)              \
        {                                                                         \
          .option = TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_TOTAL_STOCK_FRAC, \
          .details.total_stock.val = (v),                                         \
          .details.total_stock.frac = (f)                                         \
        }

/**
 * Set fractional part of total stock.
 *
 * @param f fractional stock value
 * @return representation of the option as a struct TALER_MERCHANT_PatchPrivateProductOptionValue
 */
#define TALER_MERCHANT_patch_private_product_option_total_stock_frac(f)           \
        (const struct TALER_MERCHANT_PatchPrivateProductOptionValue)              \
        {                                                                         \
          .option = TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_TOTAL_STOCK_FRAC, \
          .details.total_stock.frac = (f)                                         \
        }

/**
 * Set value part of total stock (-1 for unlimited).
 *
 * @param v stock value
 * @return representation of the option as a struct TALER_MERCHANT_PatchPrivateProductOptionValue
 */
#define TALER_MERCHANT_patch_private_product_option_total_stock_val(v)           \
        (const struct TALER_MERCHANT_PatchPrivateProductOptionValue)             \
        {                                                                        \
          .option = TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_TOTAL_STOCK_VAL, \
          .details.total_stock.val = (v)                                         \
        }

/**
 * Set whether fractional quantities are allowed.
 *
 * @param a true if fractional quantities are allowed
 * @return representation of the option as a struct TALER_MERCHANT_PatchPrivateProductOptionValue
 */
#define TALER_MERCHANT_patch_private_product_option_unit_allow_fraction(a)           \
        (const struct TALER_MERCHANT_PatchPrivateProductOptionValue)                 \
        {                                                                            \
          .option = TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_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_PatchPrivateProductOptionValue
 */
#define TALER_MERCHANT_patch_private_product_option_unit_precision_level(p)              \
        (const struct TALER_MERCHANT_PatchPrivateProductOptionValue)                      \
        {                                                                                 \
          .option = \
            TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_UNIT_PRECISION_LEVEL,   \
          .details.unit_precision_level = (p)                                             \
        }

/**
 * Set internationalized descriptions.
 *
 * @param d descriptions JSON object
 * @return representation of the option
 */
#define TALER_MERCHANT_patch_private_product_option_description_i18n(d)         \
        (const struct TALER_MERCHANT_PatchPrivateProductOptionValue)             \
        {                                                                        \
          .option = \
            TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_DESCRIPTION_I18N,  \
          .details.description_i18n = (d)                                        \
        }

/**
 * Set product image (base64-encoded data URL).
 *
 * @param i image string
 * @return representation of the option
 */
#define TALER_MERCHANT_patch_private_product_option_image(i)                    \
        (const struct TALER_MERCHANT_PatchPrivateProductOptionValue)             \
        {                                                                        \
          .option = TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_IMAGE,          \
          .details.image = (i)                                                   \
        }

/**
 * Set tax information.
 *
 * @param t taxes JSON array
 * @return representation of the option
 */
#define TALER_MERCHANT_patch_private_product_option_taxes(t)                    \
        (const struct TALER_MERCHANT_PatchPrivateProductOptionValue)             \
        {                                                                        \
          .option = TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_TAXES,          \
          .details.taxes = (t)                                                   \
        }

/**
 * Set total units lost/expired.
 *
 * @param l total lost value
 * @return representation of the option
 */
#define TALER_MERCHANT_patch_private_product_option_total_lost(l)              \
        (const struct TALER_MERCHANT_PatchPrivateProductOptionValue)            \
        {                                                                       \
          .option = TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_TOTAL_LOST,   \
          .details.total_lost = (l)                                             \
        }

/**
 * Set storage address.
 *
 * @param a address JSON object
 * @return representation of the option
 */
#define TALER_MERCHANT_patch_private_product_option_address(a)                 \
        (const struct TALER_MERCHANT_PatchPrivateProductOptionValue)            \
        {                                                                       \
          .option = TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_ADDRESS,       \
          .details.address = (a)                                                \
        }

/**
 * Set expected restock time.
 *
 * @param r restock timestamp
 * @return representation of the option
 */
#define TALER_MERCHANT_patch_private_product_option_next_restock(r)            \
        (const struct TALER_MERCHANT_PatchPrivateProductOptionValue)            \
        {                                                                       \
          .option = TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_NEXT_RESTOCK,  \
          .details.next_restock = (r)                                           \
        }

/**
 * Set explicit product name (distinct from description).
 *
 * @param n product name string
 * @return representation of the option
 */
#define TALER_MERCHANT_patch_private_product_option_product_name(n)            \
        (const struct TALER_MERCHANT_PatchPrivateProductOptionValue)            \
        {                                                                       \
          .option = \
            TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_PRODUCT_NAME,      \
          .details.product_name = (n)                                           \
        }

/**
 * Set category IDs.
 *
 * @param n number of categories
 * @param c array of category IDs
 * @return representation of the option
 */
#define TALER_MERCHANT_patch_private_product_option_categories(n,c)            \
        (const struct TALER_MERCHANT_PatchPrivateProductOptionValue)            \
        {                                                                       \
          .option = TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_CATEGORIES,    \
          .details.categories = { .num = (n), .cats = (c) }                     \
        }

/**
 * Set minimum age requirement.
 *
 * @param a minimum age (0 for none)
 * @return representation of the option
 */
#define TALER_MERCHANT_patch_private_product_option_minimum_age(a)             \
        (const struct TALER_MERCHANT_PatchPrivateProductOptionValue)            \
        {                                                                       \
          .option = TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_MINIMUM_AGE,   \
          .details.minimum_age = (a)                                            \
        }

/**
 * Set product group ID.
 *
 * @param g product group ID
 * @return representation of the option
 */
#define TALER_MERCHANT_patch_private_product_option_product_group_id(g)        \
        (const struct TALER_MERCHANT_PatchPrivateProductOptionValue)            \
        {                                                                       \
          .option = \
            TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_PRODUCT_GROUP_ID,  \
          .details.product_group_id = (g)                                       \
        }

/**
 * Set money pot ID.
 *
 * @param m money pot ID
 * @return representation of the option
 */
#define TALER_MERCHANT_patch_private_product_option_money_pot_id(m)            \
        (const struct TALER_MERCHANT_PatchPrivateProductOptionValue)            \
        {                                                                       \
          .option = \
            TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_MONEY_POT_ID,      \
          .details.money_pot_id = (m)                                           \
        }

/**
 * Set whether the price is net (before tax).
 *
 * @param b true if price is net
 * @return representation of the option
 */
#define TALER_MERCHANT_patch_private_product_option_price_is_net(b)            \
        (const struct TALER_MERCHANT_PatchPrivateProductOptionValue)            \
        {                                                                       \
          .option = \
            TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_OPTION_PRICE_IS_NET,      \
          .details.price_is_net = (b)                                           \
        }


/**
 * Set the requested options for the operation.
 *
 * If any option fail other options may be or may be not applied.
 *
 * @param pph 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_product_set_options_ (
  struct TALER_MERCHANT_PatchPrivateProductHandle *pph,
  unsigned int num_options,
  const struct TALER_MERCHANT_PatchPrivateProductOptionValue *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_product_set_options (
 *   pph,
 *   TALER_MERCHANT_patch_private_product_option_total_stock_frac (500),
 *   TALER_MERCHANT_patch_private_product_option_unit_allow_fraction (true));
 *
 * @param pph the request to set the options for
 * @param ... the list of the options, each option must be created
 *            by helpers TALER_MERCHANT_patch_private_product_option_NAME(VALUE)
 * @return #GNUNET_OK on success,
 *         #GNUNET_NO on failure,
 *         #GNUNET_SYSERR on internal error
 */
#define TALER_MERCHANT_patch_private_product_set_options(pph,...)                \
        TALER_MERCHANT_patch_private_product_set_options_ (                       \
          pph,                                                                    \
          TALER_MERCHANT_COMMON_OPTIONS_ARRAY_MAX_SIZE,                          \
          ((const struct TALER_MERCHANT_PatchPrivateProductOptionValue[])         \
           {__VA_ARGS__, TALER_MERCHANT_patch_private_product_option_end_ () }   \
          ))


/**
 * Response details for a PATCH /private/products/$PRODUCT_ID request.
 */
struct TALER_MERCHANT_PatchPrivateProductResponse
{
  /**
   * HTTP response details.
   */
  struct TALER_MERCHANT_HttpResponse hr;
};


#ifndef TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_RESULT_CLOSURE
/**
 * Type of the closure used by
 * the #TALER_MERCHANT_PatchPrivateProductCallback.
 */
#define TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_RESULT_CLOSURE void
#endif /* TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_RESULT_CLOSURE */

/**
 * Callback for a PATCH /private/products/$PRODUCT_ID request.
 *
 * @param cls closure
 * @param result response details
 */
typedef void
(*TALER_MERCHANT_PatchPrivateProductCallback)(
  TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_RESULT_CLOSURE *cls,
  const struct TALER_MERCHANT_PatchPrivateProductResponse *result);


/**
 * Start PATCH /private/products/$PRODUCT_ID operation.
 *
 * @param[in,out] pph 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_product_start (
  struct TALER_MERCHANT_PatchPrivateProductHandle *pph,
  TALER_MERCHANT_PatchPrivateProductCallback cb,
  TALER_MERCHANT_PATCH_PRIVATE_PRODUCT_RESULT_CLOSURE *cb_cls);


/**
 * Cancel PATCH /private/products/$PRODUCT_ID operation.
 * This function must not be called by clients after the
 * callback has been invoked.
 *
 * @param[in] pph operation to cancel
 */
void
TALER_MERCHANT_patch_private_product_cancel (
  struct TALER_MERCHANT_PatchPrivateProductHandle *pph);


#endif /* _TALER_MERCHANT__PATCH_PRIVATE_PRODUCTS_PRODUCT_ID_H */
