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

#include <taler/merchant/common.h>


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

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

  /**
   * Long polling timeout.
   */
  TALER_MERCHANT_GET_PRIVATE_ORDER_OPTION_TIMEOUT,

  /**
   * If set to true, try to obtain the wire transfer status
   * for this order from the exchange.
   * @deprecated since protocol v6.
   */
  TALER_MERCHANT_GET_PRIVATE_ORDER_OPTION_TRANSFER,

  /**
   * Long-poll ETag to suppress responses that have not changed.
   * ShortHashCode value.
   * @since protocol v25.
   */
  TALER_MERCHANT_GET_PRIVATE_ORDER_OPTION_LP_NOT_ETAG,

  /**
   * If true, refunded orders may be returned under
   * "already_paid_order_id".
   * @since protocol v9.
   */
  TALER_MERCHANT_GET_PRIVATE_ORDER_OPTION_ALLOW_REFUNDED_FOR_REPURCHASE

};


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

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

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

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

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

    /**
     * Value if @e option is
     * #TALER_MERCHANT_GET_PRIVATE_ORDER_OPTION_TRANSFER.
     */
    bool transfer;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_GET_PRIVATE_ORDER_OPTION_LP_NOT_ETAG.
     */
    const struct GNUNET_ShortHashCode *lp_not_etag;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_GET_PRIVATE_ORDER_OPTION_ALLOW_REFUNDED_FOR_REPURCHASE.
     */
    bool allow_refunded_for_repurchase;

  } details;

};


/**
 * Handle for a GET /private/orders/$ORDER_ID request.
 */
struct TALER_MERCHANT_GetPrivateOrderHandle;


/**
 * Set up GET /private/orders/$ORDER_ID 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 order_id identifier of the order to retrieve
 * @return handle to operation
 */
struct TALER_MERCHANT_GetPrivateOrderHandle *
TALER_MERCHANT_get_private_order_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_GetPrivateOrderOptionValue
 */
#define TALER_MERCHANT_get_private_order_option_end_()                \
        (const struct TALER_MERCHANT_GetPrivateOrderOptionValue)      \
        {                                                              \
          .option = TALER_MERCHANT_GET_PRIVATE_ORDER_OPTION_END       \
        }

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

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

/**
 * Set transfer flag (try to obtain wire transfer status from exchange).
 * @deprecated since protocol v6.
 *
 * @param b true to request wire transfer status
 * @return representation of the option as a struct TALER_MERCHANT_GetPrivateOrderOptionValue
 */
#define TALER_MERCHANT_get_private_order_option_transfer(b)                        \
        (const struct TALER_MERCHANT_GetPrivateOrderOptionValue)                    \
        {                                                                            \
          .option = TALER_MERCHANT_GET_PRIVATE_ORDER_OPTION_TRANSFER,               \
          .details.transfer = (b)                                                    \
        }

/**
 * Set long-poll ETag to suppress unchanged responses.
 *
 * @param e pointer to the short hash code ETag
 * @return representation of the option as a struct TALER_MERCHANT_GetPrivateOrderOptionValue
 */
#define TALER_MERCHANT_get_private_order_option_lp_not_etag(e)                      \
        (const struct TALER_MERCHANT_GetPrivateOrderOptionValue)                     \
        {                                                                             \
          .option = TALER_MERCHANT_GET_PRIVATE_ORDER_OPTION_LP_NOT_ETAG,             \
          .details.lp_not_etag = (e)                                                  \
        }

/**
 * Set allow-refunded-for-repurchase flag.
 *
 * @param b true to allow refunded orders under already_paid_order_id
 * @return representation of the option as a struct TALER_MERCHANT_GetPrivateOrderOptionValue
 */
#define TALER_MERCHANT_get_private_order_option_allow_refunded_for_repurchase(b)                \
        (const struct TALER_MERCHANT_GetPrivateOrderOptionValue)                                 \
        {                                                                                         \
          .option = TALER_MERCHANT_GET_PRIVATE_ORDER_OPTION_ALLOW_REFUNDED_FOR_REPURCHASE,       \
          .details.allow_refunded_for_repurchase = (b)                                            \
        }


/**
 * 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_order_set_options_ (
  struct TALER_MERCHANT_GetPrivateOrderHandle *oph,
  unsigned int num_options,
  const struct TALER_MERCHANT_GetPrivateOrderOptionValue *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_order_set_options (
 *   oph,
 *   TALER_MERCHANT_get_private_order_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_private_order_option_NAME(VALUE)
 * @return #GNUNET_OK on success,
 *         #GNUNET_NO on failure,
 *         #GNUNET_SYSERR on internal error
 */
#define TALER_MERCHANT_get_private_order_set_options(oph,...)                \
        TALER_MERCHANT_get_private_order_set_options_ (                       \
          oph,                                                                \
          TALER_MERCHANT_COMMON_OPTIONS_ARRAY_MAX_SIZE,                      \
          ((const struct TALER_MERCHANT_GetPrivateOrderOptionValue[])        \
           {__VA_ARGS__, TALER_MERCHANT_get_private_order_option_end_ () }   \
          ))


/**
 * Order status code.
 */
enum TALER_MERCHANT_OrderStatusCode
{
  /**
   * The order is unpaid.
   */
  TALER_MERCHANT_OSC_UNPAID = 0,

  /**
   * The order has been claimed by a wallet.
   */
  TALER_MERCHANT_OSC_CLAIMED = 1,

  /**
   * The order has been paid.
   */
  TALER_MERCHANT_OSC_PAID = 2
};


/**
 * Refund detail in a paid order (merchant view).
 */
struct TALER_MERCHANT_GetPrivateOrderRefundDetail
{

  /**
   * Human-readable reason given for the refund.
   */
  const char *reason;

  /**
   * Time when the refund was granted.
   */
  struct GNUNET_TIME_Timestamp refund_time;

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

  /**
   * True if a refund is still available for the wallet
   * for this payment.
   */
  bool pending;

};


/**
 * Response details for a GET /private/orders/$ORDER_ID request.
 */
struct TALER_MERCHANT_GetPrivateOrderResponse
{

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

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

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

      /**
       * Current status of the order.
       */
      enum TALER_MERCHANT_OrderStatusCode status;

      /**
       * Details depending on @a status.
       */
      union
      {

        /**
         * Details when @a status is #TALER_MERCHANT_OSC_UNPAID.
         */
        struct
        {

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

          /**
           * Fulfillment URL of an already paid order, or NULL.
           */
          const char *already_paid_fulfillment_url;

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

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

          /**
           * Proto contract terms (contract terms without nonce).
           * @since protocol v25.
           */
          const json_t *proto_contract_terms;

          /**
           * Status URL for the order.
           */
          const char *order_status_url;

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

          /**
           * Deadline when the offer expires.
           * @since protocol v21.
           */
          struct GNUNET_TIME_Timestamp pay_deadline;

        } unpaid;

        /**
         * Details when @a status is #TALER_MERCHANT_OSC_CLAIMED.
         */
        struct
        {

          /**
           * Contract terms (JSON).
           */
          const json_t *contract_terms;

          /**
           * Status URL for the order.
           * @since protocol v19.
           */
          const char *order_status_url;

        } claimed;

        /**
         * Details when @a status is #TALER_MERCHANT_OSC_PAID.
         */
        struct
        {

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

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

          /**
           * True if the payment has been wired to the merchant.
           */
          bool wired;

          /**
           * Total amount deposited.
           */
          struct TALER_Amount deposit_total;

          /**
           * Error code from the exchange, or TALER_EC_NONE.
           */
          enum TALER_ErrorCode exchange_ec;

          /**
           * HTTP status returned by the exchange.
           */
          unsigned int exchange_hc;

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

          /**
           * Contract terms (JSON).
           */
          const json_t *contract_terms;

          /**
           * Number of wire transfers in @a wts.
           */
          unsigned int wts_len;

          /**
           * Array of wire transfer details.
           */
          struct TALER_MERCHANT_WireTransfer *wts;

          /**
           * Number of refund details in @a refunds.
           */
          unsigned int refunds_len;

          /**
           * Array of per-coin refund details.
           */
          struct TALER_MERCHANT_GetPrivateOrderRefundDetail *refunds;

          /**
           * Timestamp of the last payment.
           */
          struct GNUNET_TIME_Timestamp last_payment;

          /**
           * Status URL for the order.
           */
          const char *order_status_url;

          /**
           * Index of the selected choice within the choices array
           * of contract terms, or -1 if not set.
           * @since protocol v21.
           */
          int choice_index;

        } paid;

      } details;

    } ok;

  } details;

};


#ifndef TALER_MERCHANT_GET_PRIVATE_ORDER_RESULT_CLOSURE
/**
 * Type of the closure used by
 * the #TALER_MERCHANT_GetPrivateOrderCallback.
 */
#define TALER_MERCHANT_GET_PRIVATE_ORDER_RESULT_CLOSURE void
#endif /* TALER_MERCHANT_GET_PRIVATE_ORDER_RESULT_CLOSURE */

/**
 * Callback for a GET /private/orders/$ORDER_ID request.
 *
 * @param cls closure
 * @param osr response details
 */
typedef void
(*TALER_MERCHANT_GetPrivateOrderCallback)(
  TALER_MERCHANT_GET_PRIVATE_ORDER_RESULT_CLOSURE *cls,
  const struct TALER_MERCHANT_GetPrivateOrderResponse *osr);


/**
 * Start GET /private/orders/$ORDER_ID 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_order_start (
  struct TALER_MERCHANT_GetPrivateOrderHandle *oph,
  TALER_MERCHANT_GetPrivateOrderCallback cb,
  TALER_MERCHANT_GET_PRIVATE_ORDER_RESULT_CLOSURE *cb_cls);


/**
 * Cancel GET /private/orders/$ORDER_ID operation.  This function must not be
 * called by clients after the TALER_MERCHANT_GetPrivateOrderCallback
 * 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_order_cancel (
  struct TALER_MERCHANT_GetPrivateOrderHandle *oph);


#endif /* _TALER_MERCHANT__GET_PRIVATE_ORDERS_ORDER_ID_H */
