/*
  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/get-private-products.h
 * @brief C interface for the GET /private/products endpoint
 * @author Christian Grothoff
 */
#ifndef _TALER_MERCHANT__GET_PRIVATE_PRODUCTS_H
#define _TALER_MERCHANT__GET_PRIVATE_PRODUCTS_H

#include <taler/merchant/common.h>


/**
 * Possible options we can set for the GET /private/products request.
 */
enum TALER_MERCHANT_GetPrivateProductsOption
{
  /**
   * End of list of options.
   */
  TALER_MERCHANT_GET_PRIVATE_PRODUCTS_OPTION_END = 0,

  /**
   * Return at most N values. Negative for descending by row ID,
   * positive for ascending by row ID. Default is 20.
   * @since protocol v12.
   */
  TALER_MERCHANT_GET_PRIVATE_PRODUCTS_OPTION_LIMIT,

  /**
   * Starting product_serial_id for pagination.
   * @since protocol v12.
   */
  TALER_MERCHANT_GET_PRIVATE_PRODUCTS_OPTION_OFFSET,

  /**
   * Filter by category name (case-insensitive substring match).
   * @since protocol v23.
   */
  TALER_MERCHANT_GET_PRIVATE_PRODUCTS_OPTION_CATEGORY_FILTER,

  /**
   * Filter by product name (case-insensitive substring match).
   * @since protocol v23.
   */
  TALER_MERCHANT_GET_PRIVATE_PRODUCTS_OPTION_NAME_FILTER,

  /**
   * Filter by product description (case-insensitive substring match).
   * @since protocol v23.
   */
  TALER_MERCHANT_GET_PRIVATE_PRODUCTS_OPTION_DESCRIPTION_FILTER,

  /**
   * Filter by product group serial.
   * @since protocol v25.
   */
  TALER_MERCHANT_GET_PRIVATE_PRODUCTS_OPTION_PRODUCT_GROUP_SERIAL

};


/**
 * Value for an option for the GET /private/products request.
 */
struct TALER_MERCHANT_GetPrivateProductsOptionValue
{

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

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

    /**
     * Value if @e option is
     * #TALER_MERCHANT_GET_PRIVATE_PRODUCTS_OPTION_LIMIT.
     */
    int64_t limit;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_GET_PRIVATE_PRODUCTS_OPTION_OFFSET.
     */
    uint64_t offset;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_GET_PRIVATE_PRODUCTS_OPTION_CATEGORY_FILTER.
     */
    const char *category_filter;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_GET_PRIVATE_PRODUCTS_OPTION_NAME_FILTER.
     */
    const char *name_filter;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_GET_PRIVATE_PRODUCTS_OPTION_DESCRIPTION_FILTER.
     */
    const char *description_filter;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_GET_PRIVATE_PRODUCTS_OPTION_PRODUCT_GROUP_SERIAL.
     */
    uint64_t product_group_serial;

  } details;

};


/**
 * Handle for a GET /private/products request.
 */
struct TALER_MERCHANT_GetPrivateProductsHandle;


/**
 * Inventory entry for the GET /private/products response.
 */
struct TALER_MERCHANT_GetPrivateProductsInventoryEntry
{

  /**
   * Identifier of the product.
   */
  const char *product_id;

  /**
   * Serial ID of the product.
   */
  uint64_t product_serial;

};


/**
 * Response details for a GET /private/products request.
 */
struct TALER_MERCHANT_GetPrivateProductsResponse
{

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

  /**
   * Details depending on the HTTP status code.
   */
  union
  {

    /**
     * Details on #MHD_HTTP_OK.
     */
    struct
    {

      /**
       * Number of products in @a products.
       */
      unsigned int products_length;

      /**
       * Array of product entries.
       */
      const struct TALER_MERCHANT_GetPrivateProductsInventoryEntry *products;

    } ok;

  } details;

};


/**
 * Set up GET /private/products 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
 * @return handle to operation
 */
struct TALER_MERCHANT_GetPrivateProductsHandle *
TALER_MERCHANT_get_private_products_create (
  struct GNUNET_CURL_Context *ctx,
  const char *url);


/**
 * Terminate the list of the options.
 *
 * @return the terminating object of struct TALER_MERCHANT_GetPrivateProductsOptionValue
 */
#define TALER_MERCHANT_get_private_products_option_end_()                \
        (const struct TALER_MERCHANT_GetPrivateProductsOptionValue)      \
        {                                                                \
          .option = TALER_MERCHANT_GET_PRIVATE_PRODUCTS_OPTION_END      \
        }

/**
 * Set limit on the number of results to return.
 *
 * @param l limit on the number of results to return
 * @return representation of the option as a struct TALER_MERCHANT_GetPrivateProductsOptionValue
 */
#define TALER_MERCHANT_get_private_products_option_limit(l)              \
        (const struct TALER_MERCHANT_GetPrivateProductsOptionValue)      \
        {                                                                \
          .option = TALER_MERCHANT_GET_PRIVATE_PRODUCTS_OPTION_LIMIT,   \
          .details.limit = (l)                                           \
        }

/**
 * Set row offset from which to return results.
 *
 * @param o offset to use
 * @return representation of the option as a struct TALER_MERCHANT_GetPrivateProductsOptionValue
 */
#define TALER_MERCHANT_get_private_products_option_offset(o)              \
        (const struct TALER_MERCHANT_GetPrivateProductsOptionValue)       \
        {                                                                 \
          .option = TALER_MERCHANT_GET_PRIVATE_PRODUCTS_OPTION_OFFSET,   \
          .details.offset = (o)                                           \
        }

/**
 * Set category name filter (case-insensitive substring match).
 *
 * @param f category filter string
 * @return representation of the option as a struct TALER_MERCHANT_GetPrivateProductsOptionValue
 */
#define TALER_MERCHANT_get_private_products_option_category_filter(f)              \
        (const struct TALER_MERCHANT_GetPrivateProductsOptionValue)                \
        {                                                                          \
          .option = TALER_MERCHANT_GET_PRIVATE_PRODUCTS_OPTION_CATEGORY_FILTER,   \
          .details.category_filter = (f)                                           \
        }

/**
 * Set product name filter (case-insensitive substring match).
 *
 * @param f name filter string
 * @return representation of the option as a struct TALER_MERCHANT_GetPrivateProductsOptionValue
 */
#define TALER_MERCHANT_get_private_products_option_name_filter(f)              \
        (const struct TALER_MERCHANT_GetPrivateProductsOptionValue)            \
        {                                                                      \
          .option = TALER_MERCHANT_GET_PRIVATE_PRODUCTS_OPTION_NAME_FILTER,   \
          .details.name_filter = (f)                                           \
        }

/**
 * Set product description filter (case-insensitive substring match).
 *
 * @param f description filter string
 * @return representation of the option as a struct TALER_MERCHANT_GetPrivateProductsOptionValue
 */
#define TALER_MERCHANT_get_private_products_option_description_filter(f)              \
        (const struct TALER_MERCHANT_GetPrivateProductsOptionValue)                   \
        {                                                                             \
          .option = TALER_MERCHANT_GET_PRIVATE_PRODUCTS_OPTION_DESCRIPTION_FILTER,   \
          .details.description_filter = (f)                                           \
        }

/**
 * Set product group serial filter.
 *
 * @param s product group serial to filter by
 * @return representation of the option as a struct TALER_MERCHANT_GetPrivateProductsOptionValue
 */
#define TALER_MERCHANT_get_private_products_option_product_group_serial(s)              \
        (const struct TALER_MERCHANT_GetPrivateProductsOptionValue)                     \
        {                                                                               \
          .option = TALER_MERCHANT_GET_PRIVATE_PRODUCTS_OPTION_PRODUCT_GROUP_SERIAL,   \
          .details.product_group_serial = (s)                                           \
        }


/**
 * Set the requested options for the operation.
 *
 * If any option fail other options may be or may be not applied.
 *
 * @param gpph 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_get_private_products_set_options_ (
  struct TALER_MERCHANT_GetPrivateProductsHandle *gpph,
  unsigned int num_options,
  const struct TALER_MERCHANT_GetPrivateProductsOptionValue *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_get_private_products_set_options (
 *   gpph,
 *   TALER_MERCHANT_get_private_products_option_limit (20),
 *   TALER_MERCHANT_get_private_products_option_name_filter ("widget"));
 *
 * @param gpph the request to set the options for
 * @param ... the list of the options, each option must be created
 *            by helpers TALER_MERCHANT_get_private_products_option_NAME(VALUE)
 * @return #GNUNET_OK on success,
 *         #GNUNET_NO on failure,
 *         #GNUNET_SYSERR on internal error
 */
#define TALER_MERCHANT_get_private_products_set_options(gpph,...)                \
        TALER_MERCHANT_get_private_products_set_options_ (                       \
          gpph,                                                                  \
          TALER_MERCHANT_COMMON_OPTIONS_ARRAY_MAX_SIZE,                         \
          ((const struct TALER_MERCHANT_GetPrivateProductsOptionValue[])        \
           {__VA_ARGS__, TALER_MERCHANT_get_private_products_option_end_ () }   \
          ))


#ifndef TALER_MERCHANT_GET_PRIVATE_PRODUCTS_RESULT_CLOSURE
/**
 * Type of the closure used by
 * the #TALER_MERCHANT_GetPrivateProductsCallback.
 */
#define TALER_MERCHANT_GET_PRIVATE_PRODUCTS_RESULT_CLOSURE void
#endif /* TALER_MERCHANT_GET_PRIVATE_PRODUCTS_RESULT_CLOSURE */

/**
 * Callback for a GET /private/products request.
 *
 * @param cls closure
 * @param pgr response details
 */
typedef void
(*TALER_MERCHANT_GetPrivateProductsCallback)(
  TALER_MERCHANT_GET_PRIVATE_PRODUCTS_RESULT_CLOSURE *cls,
  const struct TALER_MERCHANT_GetPrivateProductsResponse *pgr);


/**
 * Start GET /private/products operation.
 *
 * @param[in,out] gpph 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_get_private_products_start (
  struct TALER_MERCHANT_GetPrivateProductsHandle *gpph,
  TALER_MERCHANT_GetPrivateProductsCallback cb,
  TALER_MERCHANT_GET_PRIVATE_PRODUCTS_RESULT_CLOSURE *cb_cls);


/**
 * Cancel GET /private/products operation.  This function must not be
 * called by clients after the TALER_MERCHANT_GetPrivateProductsCallback
 * has been invoked (as in those cases it'll be called internally by the
 * implementation already).
 *
 * @param[in] gpph operation to cancel
 */
void
TALER_MERCHANT_get_private_products_cancel (
  struct TALER_MERCHANT_GetPrivateProductsHandle *gpph);


#endif /* _TALER_MERCHANT__GET_PRIVATE_PRODUCTS_H */
