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

#include <taler/taler-merchant/common.h>


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


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

  /**
   * 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_OrdersGetResponse
{

  /**
   * 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_OrderEntry *orders;

    } ok;

  } details;

};


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


/**
 * Issue a GET /private/orders request (simple form).
 *
 * @param ctx CURL context to use
 * @param backend_url base URL of the merchant backend
 * @param cb callback to invoke with the result
 * @param cb_cls closure for @a cb
 * @return handle for the request, NULL on hard error
 */
struct TALER_MERCHANT_OrdersGetHandle *
TALER_MERCHANT_orders_get (struct GNUNET_CURL_Context *ctx,
                           const char *backend_url,
                           TALER_MERCHANT_OrdersGetCallback cb,
                           void *cb_cls);


/**
 * Issue a GET /private/orders request with filters.
 *
 * @param ctx CURL context to use
 * @param backend_url base URL of the merchant backend
 * @param paid filter by paid status
 * @param refunded filter by refunded status
 * @param wired filter by wired status
 * @param date date to use for pagination
 * @param start_row row to start from
 * @param delta number of results to return (negative for backwards)
 * @param timeout long-polling timeout
 * @param cb callback to invoke with the result
 * @param cb_cls closure for @a cb
 * @return handle for the request, NULL on hard error
 */
struct TALER_MERCHANT_OrdersGetHandle *
TALER_MERCHANT_orders_get2 (
  struct GNUNET_CURL_Context *ctx,
  const char *backend_url,
  enum TALER_EXCHANGE_YesNoAll paid,
  enum TALER_EXCHANGE_YesNoAll refunded,
  enum TALER_EXCHANGE_YesNoAll wired,
  struct GNUNET_TIME_Timestamp date,
  uint64_t start_row,
  int64_t delta,
  struct GNUNET_TIME_Relative timeout,
  TALER_MERCHANT_OrdersGetCallback cb,
  void *cb_cls);


/**
 * Issue a GET /private/orders request with extended filters including
 * session and fulfillment URL filtering.
 *
 * @param ctx CURL context to use
 * @param backend_url base URL of the merchant backend
 * @param paid filter by paid status
 * @param refunded filter by refunded status
 * @param wired filter by wired status
 * @param session_id filter by session ID, or NULL
 * @param fulfillment_url filter by fulfillment URL, or NULL
 * @param date date to use for pagination
 * @param start_row row to start from
 * @param delta number of results to return (negative for backwards)
 * @param timeout long-polling timeout
 * @param cb callback to invoke with the result
 * @param cb_cls closure for @a cb
 * @return handle for the request, NULL on hard error
 */
struct TALER_MERCHANT_OrdersGetHandle *
TALER_MERCHANT_orders_get3 (
  struct GNUNET_CURL_Context *ctx,
  const char *backend_url,
  enum TALER_EXCHANGE_YesNoAll paid,
  enum TALER_EXCHANGE_YesNoAll refunded,
  enum TALER_EXCHANGE_YesNoAll wired,
  const char *session_id,
  const char *fulfillment_url,
  struct GNUNET_TIME_Timestamp date,
  uint64_t start_row,
  int64_t delta,
  struct GNUNET_TIME_Relative timeout,
  TALER_MERCHANT_OrdersGetCallback cb,
  void *cb_cls);


/**
 * Cancel a GET /private/orders request.
 *
 * @param[in] ogh handle to cancel
 */
void
TALER_MERCHANT_orders_get_cancel (
  struct TALER_MERCHANT_OrdersGetHandle *ogh);


#endif
