/*
  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-products-PRODUCT_ID-lock.h
 * @brief C interface for the POST /private/products/$PRODUCT_ID/lock endpoint
 * @author Christian Grothoff
 */
#ifndef _TALER_MERCHANT__POST_PRIVATE_PRODUCTS_PRODUCT_ID_LOCK_H
#define _TALER_MERCHANT__POST_PRIVATE_PRODUCTS_PRODUCT_ID_LOCK_H

#include <taler/merchant/common.h>


/**
 * Possible options for the POST /private/products/$PRODUCT_ID/lock request.
 */
enum TALER_MERCHANT_PostPrivateProductsLockOption
{
  /**
   * End of list of options.
   */
  TALER_MERCHANT_POST_PRIVATE_PRODUCTS_LOCK_OPTION_END = 0,

  /**
   * Fractional part of the quantity to lock.
   */
  TALER_MERCHANT_POST_PRIVATE_PRODUCTS_LOCK_OPTION_QUANTITY_FRAC,

  /**
   * Whether to use fractional quantity.
   */
  TALER_MERCHANT_POST_PRIVATE_PRODUCTS_LOCK_OPTION_USE_FRACTIONAL_QUANTITY

};


/**
 * Value for an option for the POST /private/products/$PRODUCT_ID/lock request.
 */
struct TALER_MERCHANT_PostPrivateProductsLockOptionValue
{

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

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

    /**
     * Value if @e option is
     * #TALER_MERCHANT_POST_PRIVATE_PRODUCTS_LOCK_OPTION_QUANTITY_FRAC.
     */
    uint32_t quantity_frac;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_POST_PRIVATE_PRODUCTS_LOCK_OPTION_USE_FRACTIONAL_QUANTITY.
     */
    bool use_fractional_quantity;

  } details;

};


/**
 * Handle for a POST /private/products/$PRODUCT_ID/lock request.
 */
struct TALER_MERCHANT_PostPrivateProductsLockHandle;


/**
 * Response details for a POST /private/products/$PRODUCT_ID/lock request.
 */
struct TALER_MERCHANT_PostPrivateProductsLockResponse
{

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

};


/**
 * Terminate the list of the options.
 *
 * @return the terminating object
 */
#define TALER_MERCHANT_post_private_products_lock_option_end_()                \
        (const struct TALER_MERCHANT_PostPrivateProductsLockOptionValue)       \
        {                                                                       \
          .option = TALER_MERCHANT_POST_PRIVATE_PRODUCTS_LOCK_OPTION_END       \
        }

/**
 * Set fractional part of quantity.
 *
 * @param f fractional part
 * @return representation of the option
 */
#define TALER_MERCHANT_post_private_products_lock_option_quantity_frac(f)       \
        (const struct TALER_MERCHANT_PostPrivateProductsLockOptionValue)        \
        {                                                                        \
          .option = \
            TALER_MERCHANT_POST_PRIVATE_PRODUCTS_LOCK_OPTION_QUANTITY_FRAC, \
          .details.quantity_frac = (f)                                            \
        }

/**
 * Set to use fractional quantity.
 *
 * @return representation of the option
 */
#define TALER_MERCHANT_post_private_products_lock_option_use_frac_quantity() \
        (const struct TALER_MERCHANT_PostPrivateProductsLockOptionValue)            \
        {                                                                            \
          .option = \
            TALER_MERCHANT_POST_PRIVATE_PRODUCTS_LOCK_OPTION_USE_FRACTIONAL_QUANTITY, \
          .details.use_fractional_quantity = true                                    \
        }


/**
 * Set the requested options for the operation.
 *
 * @param pplh 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_products_lock_set_options_ (
  struct TALER_MERCHANT_PostPrivateProductsLockHandle *pplh,
  unsigned int num_options,
  const struct TALER_MERCHANT_PostPrivateProductsLockOptionValue *options);


/**
 * Set the requested options for the operation.
 *
 * @param pplh 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_products_lock_set_options(pplh,...)                \
        TALER_MERCHANT_post_private_products_lock_set_options_ (                       \
          pplh,                                                                        \
          TALER_MERCHANT_COMMON_OPTIONS_ARRAY_MAX_SIZE,                               \
          ((const struct TALER_MERCHANT_PostPrivateProductsLockOptionValue[])          \
           {__VA_ARGS__, TALER_MERCHANT_post_private_products_lock_option_end_ \
              () }   \
          ))


/**
 * Set up POST /private/products/$PRODUCT_ID/lock 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 lock
 * @param uuid idempotency UUID for this lock operation
 * @param duration how long to lock the inventory
 * @param quantity number of units to lock
 * @return handle to operation
 */
struct TALER_MERCHANT_PostPrivateProductsLockHandle *
TALER_MERCHANT_post_private_products_lock_create (
  struct GNUNET_CURL_Context *ctx,
  const char *url,
  const char *product_id,
  const char *uuid,
  struct GNUNET_TIME_Relative duration,
  uint64_t quantity);


#ifndef TALER_MERCHANT_POST_PRIVATE_PRODUCTS_LOCK_RESULT_CLOSURE
/**
 * Type of the closure used by
 * the #TALER_MERCHANT_PostPrivateProductsLockCallback.
 */
#define TALER_MERCHANT_POST_PRIVATE_PRODUCTS_LOCK_RESULT_CLOSURE void
#endif /* TALER_MERCHANT_POST_PRIVATE_PRODUCTS_LOCK_RESULT_CLOSURE */

/**
 * Callback for a POST /private/products/$PRODUCT_ID/lock request.
 *
 * @param cls closure
 * @param plr response details
 */
typedef void
(*TALER_MERCHANT_PostPrivateProductsLockCallback)(
  TALER_MERCHANT_POST_PRIVATE_PRODUCTS_LOCK_RESULT_CLOSURE *cls,
  const struct TALER_MERCHANT_PostPrivateProductsLockResponse *plr);


/**
 * Start POST /private/products/$PRODUCT_ID/lock operation.
 *
 * @param[in,out] pplh 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_products_lock_start (
  struct TALER_MERCHANT_PostPrivateProductsLockHandle *pplh,
  TALER_MERCHANT_PostPrivateProductsLockCallback cb,
  TALER_MERCHANT_POST_PRIVATE_PRODUCTS_LOCK_RESULT_CLOSURE *cb_cls);


/**
 * Cancel POST /private/products/$PRODUCT_ID/lock operation.  This function
 * must not be called by clients after the
 * TALER_MERCHANT_PostPrivateProductsLockCallback has been invoked (as in
 * those cases it'll be called internally by the implementation already).
 *
 * @param[in] pplh operation to cancel
 */
void
TALER_MERCHANT_post_private_products_lock_cancel (
  struct TALER_MERCHANT_PostPrivateProductsLockHandle *pplh);


#endif /* _TALER_MERCHANT__POST_PRIVATE_PRODUCTS_PRODUCT_ID_LOCK_H */
