/*
  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-orders-ORDER_ID.h
 * @brief C interface for the GET /orders/$ORDER_ID endpoint (wallet-facing)
 * @author Christian Grothoff
 */
#ifndef _TALER_MERCHANT__GET_ORDERS_ORDER_ID_H
#define _TALER_MERCHANT__GET_ORDERS_ORDER_ID_H

#include <taler/merchant/common.h>


/**
 * Possible options we can set for the GET /orders/$ORDER_ID request.
 */
enum TALER_MERCHANT_GetOrdersOption
{
  /**
   * End of list of options.
   */
  TALER_MERCHANT_GET_ORDERS_OPTION_END = 0,

  /**
   * Long polling timeout.
   */
  TALER_MERCHANT_GET_ORDERS_OPTION_TIMEOUT,

  /**
   * Session ID for repurchase detection.
   */
  TALER_MERCHANT_GET_ORDERS_OPTION_SESSION_ID,

  /**
   * Minimum refund amount to wait for.
   */
  TALER_MERCHANT_GET_ORDERS_OPTION_MIN_REFUND,

  /**
   * If true, wait until refund is confirmed obtained.
   */
  TALER_MERCHANT_GET_ORDERS_OPTION_AWAIT_REFUND_OBTAINED,

  /**
   * Hash of the contract terms (for authentication).
   */
  TALER_MERCHANT_GET_ORDERS_OPTION_H_CONTRACT,

  /**
   * Claim token for unclaimed order authentication.
   */
  TALER_MERCHANT_GET_ORDERS_OPTION_TOKEN,

  /**
   * Whether refunded orders count for repurchase detection.
   */
  TALER_MERCHANT_GET_ORDERS_OPTION_ALLOW_REFUNDED_FOR_REPURCHASE

};


/**
 * Value for an option for the GET /orders/$ORDER_ID request.
 */
struct TALER_MERCHANT_GetOrdersOptionValue
{

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

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

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

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

    /**
     * Value if @e option is
     * #TALER_MERCHANT_GET_ORDERS_OPTION_MIN_REFUND.
     */
    struct TALER_Amount min_refund;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_GET_ORDERS_OPTION_AWAIT_REFUND_OBTAINED.
     */
    bool await_refund_obtained;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_GET_ORDERS_OPTION_H_CONTRACT.
     */
    struct TALER_PrivateContractHashP h_contract;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_GET_ORDERS_OPTION_TOKEN.
     */
    struct TALER_ClaimTokenP token;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_GET_ORDERS_OPTION_ALLOW_REFUNDED_FOR_REPURCHASE.
     */
    enum TALER_EXCHANGE_YesNoAll allow_refunded_for_repurchase;

  } details;

};


/**
 * Handle for a GET /orders/$ORDER_ID request (wallet-facing).
 */
struct TALER_MERCHANT_GetOrdersHandle;


/**
 * Set up GET /orders/$ORDER_ID operation (wallet-facing).
 * 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 order_id identifier of the order to check
 * @return handle to operation
 */
struct TALER_MERCHANT_GetOrdersHandle *
TALER_MERCHANT_get_orders_create (
  struct GNUNET_CURL_Context *ctx,
  const char *url,
  const char *order_id);


/**
 * Terminate the list of the options.
 *
 * @return the terminating object of struct TALER_MERCHANT_GetOrdersOptionValue
 */
#define TALER_MERCHANT_get_orders_option_end_()                \
        (const struct TALER_MERCHANT_GetOrdersOptionValue)     \
        {                                                       \
          .option = TALER_MERCHANT_GET_ORDERS_OPTION_END       \
        }

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

/**
 * Set session ID for repurchase detection.
 *
 * @param s session ID to use
 * @return representation of the option as a struct TALER_MERCHANT_GetOrdersOptionValue
 */
#define TALER_MERCHANT_get_orders_option_session_id(s)              \
        (const struct TALER_MERCHANT_GetOrdersOptionValue)          \
        {                                                            \
          .option = TALER_MERCHANT_GET_ORDERS_OPTION_SESSION_ID,    \
          .details.session_id = (s)                                  \
        }

/**
 * Set minimum refund amount to wait for.
 *
 * @param a minimum refund amount
 * @return representation of the option as a struct TALER_MERCHANT_GetOrdersOptionValue
 */
#define TALER_MERCHANT_get_orders_option_min_refund(a)              \
        (const struct TALER_MERCHANT_GetOrdersOptionValue)          \
        {                                                            \
          .option = TALER_MERCHANT_GET_ORDERS_OPTION_MIN_REFUND,    \
          .details.min_refund = (a)                                  \
        }

/**
 * Set await refund obtained flag.
 *
 * @param b true to wait until refund is confirmed obtained
 * @return representation of the option as a struct TALER_MERCHANT_GetOrdersOptionValue
 */
#define TALER_MERCHANT_get_orders_option_await_refund_obtained(b)              \
        (const struct TALER_MERCHANT_GetOrdersOptionValue)                     \
        {                                                                       \
          .option = TALER_MERCHANT_GET_ORDERS_OPTION_AWAIT_REFUND_OBTAINED,    \
          .details.await_refund_obtained = (b)                                  \
        }

/**
 * Set hash of the contract terms (for authentication).
 *
 * @param h hash of contract terms
 * @return representation of the option as a struct TALER_MERCHANT_GetOrdersOptionValue
 */
#define TALER_MERCHANT_get_orders_option_h_contract(h)                  \
        (const struct TALER_MERCHANT_GetOrdersOptionValue)              \
        {                                                                \
          .option = TALER_MERCHANT_GET_ORDERS_OPTION_H_CONTRACT,        \
          .details.h_contract = (h)                                      \
        }

/**
 * Set claim token for unclaimed order authentication.
 *
 * @param t claim token
 * @return representation of the option as a struct TALER_MERCHANT_GetOrdersOptionValue
 */
#define TALER_MERCHANT_get_orders_option_token(t)                       \
        (const struct TALER_MERCHANT_GetOrdersOptionValue)              \
        {                                                                \
          .option = TALER_MERCHANT_GET_ORDERS_OPTION_TOKEN,             \
          .details.token = (t)                                           \
        }

/**
 * Set whether refunded orders count for repurchase detection.
 *
 * @param v yes/no/all value
 * @return representation of the option as a struct TALER_MERCHANT_GetOrdersOptionValue
 */
#define TALER_MERCHANT_get_orders_option_allow_refunded_for_repurchase(v)              \
        (const struct TALER_MERCHANT_GetOrdersOptionValue)                             \
        {                                                                               \
          .option = TALER_MERCHANT_GET_ORDERS_OPTION_ALLOW_REFUNDED_FOR_REPURCHASE,    \
          .details.allow_refunded_for_repurchase = (v)                                  \
        }


/**
 * 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_orders_set_options_ (
  struct TALER_MERCHANT_GetOrdersHandle *oph,
  unsigned int num_options,
  const struct TALER_MERCHANT_GetOrdersOptionValue *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_orders_set_options (
 *   oph,
 *   TALER_MERCHANT_get_orders_option_timeout (
 *     GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)),
 *   TALER_MERCHANT_get_orders_option_session_id ("mysession"));
 *
 * @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_orders_option_NAME(VALUE)
 * @return #GNUNET_OK on success,
 *         #GNUNET_NO on failure,
 *         #GNUNET_SYSERR on internal error
 */
#define TALER_MERCHANT_get_orders_set_options(oph,...)                \
        TALER_MERCHANT_get_orders_set_options_ (                       \
          oph,                                                          \
          TALER_MERCHANT_COMMON_OPTIONS_ARRAY_MAX_SIZE,                \
          ((const struct TALER_MERCHANT_GetOrdersOptionValue[])        \
           {__VA_ARGS__, TALER_MERCHANT_get_orders_option_end_ () }    \
          ))


/**
 * Response details for a GET /orders/$ORDER_ID request (wallet-facing).
 */
struct TALER_MERCHANT_GetOrdersResponse
{

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

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

    /**
     * Details on #MHD_HTTP_OK (order is paid, refund info available).
     */
    struct
    {

      /**
       * True if a refund has been granted.
       */
      bool refunded;

      /**
       * True if a refund is still pending.
       */
      bool refund_pending;

      /**
       * Total amount refunded.
       */
      struct TALER_Amount refund_amount;

      /**
       * Total amount of refunds taken by the wallet so far.
       */
      struct TALER_Amount refund_taken;

    } ok;

    /**
     * Details on #MHD_HTTP_ACCEPTED (order claimed, goto reorder URL).
     */
    struct
    {

      /**
       * URL where the client should go to create a fresh order
       * or trigger repurchase detection.
       */
      const char *public_reorder_url;

    } accepted;

    /**
     * Details on #MHD_HTTP_PAYMENT_REQUIRED (order not yet paid).
     */
    struct
    {

      /**
       * Taler pay URI for this order.
       */
      const char *taler_pay_uri;

      /**
       * Order ID of a previously paid order that covers this one,
       * or NULL if none.
       */
      const char *already_paid_order_id;

      /**
       * Fulfillment URL of the order, or NULL if not available.
       */
      const char *fulfillment_url;

    } payment_required;

  } details;

};


#ifndef TALER_MERCHANT_GET_ORDERS_RESULT_CLOSURE
/**
 * Type of the closure used by
 * the #TALER_MERCHANT_GetOrdersCallback.
 */
#define TALER_MERCHANT_GET_ORDERS_RESULT_CLOSURE void
#endif /* TALER_MERCHANT_GET_ORDERS_RESULT_CLOSURE */

/**
 * Callback for a GET /orders/$ORDER_ID request (wallet-facing).
 *
 * @param cls closure
 * @param owgr response details
 */
typedef void
(*TALER_MERCHANT_GetOrdersCallback)(
  TALER_MERCHANT_GET_ORDERS_RESULT_CLOSURE *cls,
  const struct TALER_MERCHANT_GetOrdersResponse *owgr);


/**
 * Start GET /orders/$ORDER_ID operation (wallet-facing).
 *
 * @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_orders_start (
  struct TALER_MERCHANT_GetOrdersHandle *oph,
  TALER_MERCHANT_GetOrdersCallback cb,
  TALER_MERCHANT_GET_ORDERS_RESULT_CLOSURE *cb_cls);


/**
 * Cancel GET /orders/$ORDER_ID operation (wallet-facing).  This function
 * must not be called by clients after the TALER_MERCHANT_GetOrdersCallback
 * 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_orders_cancel (
  struct TALER_MERCHANT_GetOrdersHandle *oph);


#endif /* _TALER_MERCHANT__GET_ORDERS_ORDER_ID_H */
