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

#include <taler/merchant/common.h>


/**
 * Possible options we can set for the GET /private/orders request.
 */
enum TALER_MERCHANT_GetPrivateOrdersOption
{
  /**
   * End of list of options.
   */
  TALER_MERCHANT_GET_PRIVATE_ORDERS_OPTION_END = 0,

  /**
   * Filter by paid status.
   */
  TALER_MERCHANT_GET_PRIVATE_ORDERS_OPTION_PAID,

  /**
   * Filter by refunded status.
   */
  TALER_MERCHANT_GET_PRIVATE_ORDERS_OPTION_REFUNDED,

  /**
   * Filter by wired status.
   */
  TALER_MERCHANT_GET_PRIVATE_ORDERS_OPTION_WIRED,

  /**
   * Return at most N values. Negative values
   * to return before offset, positive to return after offset.
   * Default is -20.
   */
  TALER_MERCHANT_GET_PRIVATE_ORDERS_OPTION_LIMIT,

  /**
   * Starting row_id for pagination.
   */
  TALER_MERCHANT_GET_PRIVATE_ORDERS_OPTION_OFFSET,

  /**
   * Date threshold for filtering.
   */
  TALER_MERCHANT_GET_PRIVATE_ORDERS_OPTION_DATE,

  /**
   * Long polling timeout.
   */
  TALER_MERCHANT_GET_PRIVATE_ORDERS_OPTION_TIMEOUT,

  /**
   * Filter by session ID.
   */
  TALER_MERCHANT_GET_PRIVATE_ORDERS_OPTION_SESSION_ID,

  /**
   * Filter by fulfillment URL.
   */
  TALER_MERCHANT_GET_PRIVATE_ORDERS_OPTION_FULFILLMENT_URL,

  /**
   * Filter by summary text (case-insensitive substring match).
   */
  TALER_MERCHANT_GET_PRIVATE_ORDERS_OPTION_SUMMARY_FILTER,

  /**
   * Only return orders younger than the specified age.
   * Only applicable if limit is positive.
   * @since protocol v27.
   */
  TALER_MERCHANT_GET_PRIVATE_ORDERS_OPTION_MAX_AGE

};


/**
 * Value for an option for the GET /private/orders request.
 */
struct TALER_MERCHANT_GetPrivateOrdersOptionValue
{

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

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

    /**
     * Value if @e option is
     * #TALER_MERCHANT_GET_PRIVATE_ORDERS_OPTION_PAID.
     */
    enum TALER_EXCHANGE_YesNoAll paid;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_GET_PRIVATE_ORDERS_OPTION_REFUNDED.
     */
    enum TALER_EXCHANGE_YesNoAll refunded;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_GET_PRIVATE_ORDERS_OPTION_WIRED.
     */
    enum TALER_EXCHANGE_YesNoAll wired;

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

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

    /**
     * Value if @e option is
     * #TALER_MERCHANT_GET_PRIVATE_ORDERS_OPTION_DATE.
     */
    struct GNUNET_TIME_Timestamp date;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_GET_PRIVATE_ORDERS_OPTION_TIMEOUT.
     */
    struct GNUNET_TIME_Relative timeout;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_GET_PRIVATE_ORDERS_OPTION_SESSION_ID.
     */
    const char *session_id;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_GET_PRIVATE_ORDERS_OPTION_FULFILLMENT_URL.
     */
    const char *fulfillment_url;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_GET_PRIVATE_ORDERS_OPTION_SUMMARY_FILTER.
     */
    const char *summary_filter;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_GET_PRIVATE_ORDERS_OPTION_MAX_AGE.
     */
    struct GNUNET_TIME_Relative max_age;

  } details;

};


/**
 * Handle for a GET /private/orders request.
 */
struct TALER_MERCHANT_GetPrivateOrdersHandle;


/**
 * Set up GET /private/orders 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_GetPrivateOrdersHandle *
TALER_MERCHANT_get_private_orders_create (
  struct GNUNET_CURL_Context *ctx,
  const char *url);


/**
 * Terminate the list of the options.
 *
 * @return the terminating object of struct TALER_MERCHANT_GetPrivateOrdersOptionValue
 */
#define TALER_MERCHANT_get_private_orders_option_end_()                \
        (const struct TALER_MERCHANT_GetPrivateOrdersOptionValue)      \
        {                                                               \
          .option = TALER_MERCHANT_GET_PRIVATE_ORDERS_OPTION_END       \
        }

/**
 * Set filter on paid status.
 *
 * @param p paid filter to use
 * @return representation of the option as a struct TALER_MERCHANT_GetPrivateOrdersOptionValue
 */
#define TALER_MERCHANT_get_private_orders_option_paid(p)               \
        (const struct TALER_MERCHANT_GetPrivateOrdersOptionValue)      \
        {                                                               \
          .option = TALER_MERCHANT_GET_PRIVATE_ORDERS_OPTION_PAID,     \
          .details.paid = (p)                                           \
        }

/**
 * Set filter on refunded status.
 *
 * @param r refunded filter to use
 * @return representation of the option as a struct TALER_MERCHANT_GetPrivateOrdersOptionValue
 */
#define TALER_MERCHANT_get_private_orders_option_refunded(r)            \
        (const struct TALER_MERCHANT_GetPrivateOrdersOptionValue)       \
        {                                                                \
          .option = TALER_MERCHANT_GET_PRIVATE_ORDERS_OPTION_REFUNDED,  \
          .details.refunded = (r)                                        \
        }

/**
 * Set filter on wired status.
 *
 * @param w wired filter to use
 * @return representation of the option as a struct TALER_MERCHANT_GetPrivateOrdersOptionValue
 */
#define TALER_MERCHANT_get_private_orders_option_wired(w)              \
        (const struct TALER_MERCHANT_GetPrivateOrdersOptionValue)      \
        {                                                               \
          .option = TALER_MERCHANT_GET_PRIVATE_ORDERS_OPTION_WIRED,    \
          .details.wired = (w)                                          \
        }

/**
 * 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_GetPrivateOrdersOptionValue
 */
#define TALER_MERCHANT_get_private_orders_option_limit(l)              \
        (const struct TALER_MERCHANT_GetPrivateOrdersOptionValue)      \
        {                                                               \
          .option = TALER_MERCHANT_GET_PRIVATE_ORDERS_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_GetPrivateOrdersOptionValue
 */
#define TALER_MERCHANT_get_private_orders_option_offset(o)              \
        (const struct TALER_MERCHANT_GetPrivateOrdersOptionValue)       \
        {                                                                \
          .option = TALER_MERCHANT_GET_PRIVATE_ORDERS_OPTION_OFFSET,    \
          .details.offset = (o)                                          \
        }

/**
 * Set date threshold for filtering.
 *
 * @param d date threshold to use
 * @return representation of the option as a struct TALER_MERCHANT_GetPrivateOrdersOptionValue
 */
#define TALER_MERCHANT_get_private_orders_option_date(d)               \
        (const struct TALER_MERCHANT_GetPrivateOrdersOptionValue)      \
        {                                                               \
          .option = TALER_MERCHANT_GET_PRIVATE_ORDERS_OPTION_DATE,     \
          .details.date = (d)                                           \
        }

/**
 * Set long polling timeout.
 *
 * @param t timeout to use
 * @return representation of the option as a struct TALER_MERCHANT_GetPrivateOrdersOptionValue
 */
#define TALER_MERCHANT_get_private_orders_option_timeout(t)             \
        (const struct TALER_MERCHANT_GetPrivateOrdersOptionValue)       \
        {                                                                \
          .option = TALER_MERCHANT_GET_PRIVATE_ORDERS_OPTION_TIMEOUT,   \
          .details.timeout = (t)                                         \
        }

/**
 * Set session ID filter.
 *
 * @param s session ID to filter by
 * @return representation of the option as a struct TALER_MERCHANT_GetPrivateOrdersOptionValue
 */
#define TALER_MERCHANT_get_private_orders_option_session_id(s)          \
        (const struct TALER_MERCHANT_GetPrivateOrdersOptionValue)       \
        {                                                                \
          .option = TALER_MERCHANT_GET_PRIVATE_ORDERS_OPTION_SESSION_ID, \
          .details.session_id = (s)                                      \
        }

/**
 * Set fulfillment URL filter.
 *
 * @param u fulfillment URL to filter by
 * @return representation of the option as a struct TALER_MERCHANT_GetPrivateOrdersOptionValue
 */
#define TALER_MERCHANT_get_private_orders_option_fulfillment_url(u)          \
        (const struct TALER_MERCHANT_GetPrivateOrdersOptionValue)            \
        {                                                                     \
          .option = TALER_MERCHANT_GET_PRIVATE_ORDERS_OPTION_FULFILLMENT_URL, \
          .details.fulfillment_url = (u)                                      \
        }

/**
 * Set summary text filter.
 *
 * @param f summary filter string (case-insensitive substring match)
 * @return representation of the option as a struct TALER_MERCHANT_GetPrivateOrdersOptionValue
 */
#define TALER_MERCHANT_get_private_orders_option_summary_filter(f)          \
        (const struct TALER_MERCHANT_GetPrivateOrdersOptionValue)           \
        {                                                                    \
          .option = TALER_MERCHANT_GET_PRIVATE_ORDERS_OPTION_SUMMARY_FILTER, \
          .details.summary_filter = (f)                                      \
        }

/**
 * Set maximum age filter.  Only return orders younger than the
 * specified age.  Only applicable if limit is positive.
 * Transmitted with a granularity of one second, so sub-second
 * parts of @a a are rounded down.
 * @since protocol v27.
 *
 * @param a maximum age as a relative time
 * @return representation of the option as a struct TALER_MERCHANT_GetPrivateOrdersOptionValue
 */
#define TALER_MERCHANT_get_private_orders_option_max_age(a)                \
        (const struct TALER_MERCHANT_GetPrivateOrdersOptionValue)          \
        {                                                                   \
          .option = TALER_MERCHANT_GET_PRIVATE_ORDERS_OPTION_MAX_AGE,      \
          .details.max_age = (a)                                            \
        }


/**
 * Set the requested options for the operation.
 *
 * If any option fail other options may be or may be not applied.
 *
 * @param oph 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_orders_set_options_ (
  struct TALER_MERCHANT_GetPrivateOrdersHandle *oph,
  unsigned int num_options,
  const struct TALER_MERCHANT_GetPrivateOrdersOptionValue *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_orders_set_options (
 *   oph,
 *   TALER_MERCHANT_get_private_orders_option_paid (
 *     TALER_EXCHANGE_YNA_YES),
 *   TALER_MERCHANT_get_private_orders_option_limit (-20));
 *
 * @param oph the request to set the options for
 * @param ... the list of the options, each option must be created
 *            by helpers TALER_MERCHANT_get_private_orders_option_NAME(VALUE)
 * @return #GNUNET_OK on success,
 *         #GNUNET_NO on failure,
 *         #GNUNET_SYSERR on internal error
 */
#define TALER_MERCHANT_get_private_orders_set_options(oph,...)                \
        TALER_MERCHANT_get_private_orders_set_options_ (                       \
          oph,                                                                 \
          TALER_MERCHANT_COMMON_OPTIONS_ARRAY_MAX_SIZE,                       \
          ((const struct TALER_MERCHANT_GetPrivateOrdersOptionValue[])        \
           {__VA_ARGS__, TALER_MERCHANT_get_private_orders_option_end_ () }   \
          ))


/**
 * Entry for one order in the GET /private/orders response.
 */
struct TALER_MERCHANT_GetPrivateOrdersOrderEntry
{

  /**
   * The order identifier string.
   */
  const char *order_id;

  /**
   * Timestamp when the order was created.
   */
  struct GNUNET_TIME_Timestamp timestamp;

  /**
   * Serial number of the order in the database.
   */
  uint64_t order_serial;

  /**
   * Total amount of the order.
   */
  struct TALER_Amount amount;

  /**
   * Amount refunded so far.
   */
  struct TALER_Amount refund_amount;

  /**
   * Amount still pending refund.
   */
  struct TALER_Amount pending_refund_amount;

  /**
   * Short summary of the order.
   */
  const char *summary;

  /**
   * True if refund is possible.
   */
  bool refundable;

  /**
   * True if the order has been paid.
   */
  bool paid;

};


/**
 * Response details for a GET /private/orders request.
 */
struct TALER_MERCHANT_GetPrivateOrdersResponse
{

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

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

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

      /**
       * Number of orders in @a orders.
       */
      unsigned int orders_length;

      /**
       * Array of order entries.
       */
      const struct TALER_MERCHANT_GetPrivateOrdersOrderEntry *orders;

    } ok;

  } details;

};


#ifndef TALER_MERCHANT_GET_PRIVATE_ORDERS_RESULT_CLOSURE
/**
 * Type of the closure used by
 * the #TALER_MERCHANT_GetPrivateOrdersCallback.
 */
#define TALER_MERCHANT_GET_PRIVATE_ORDERS_RESULT_CLOSURE void
#endif /* TALER_MERCHANT_GET_PRIVATE_ORDERS_RESULT_CLOSURE */

/**
 * Callback for a GET /private/orders request.
 *
 * @param cls closure
 * @param ogr response details
 */
typedef void
(*TALER_MERCHANT_GetPrivateOrdersCallback)(
  TALER_MERCHANT_GET_PRIVATE_ORDERS_RESULT_CLOSURE *cls,
  const struct TALER_MERCHANT_GetPrivateOrdersResponse *ogr);


/**
 * Start GET /private/orders operation.
 *
 * @param[in,out] oph 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_orders_start (
  struct TALER_MERCHANT_GetPrivateOrdersHandle *oph,
  TALER_MERCHANT_GetPrivateOrdersCallback cb,
  TALER_MERCHANT_GET_PRIVATE_ORDERS_RESULT_CLOSURE *cb_cls);


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


#endif /* _TALER_MERCHANT__GET_PRIVATE_ORDERS_H */
