/*
  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/post-private-token.h
 * @brief C interface for the POST /private/token endpoint
 * @author Christian Grothoff
 */
#ifndef _TALER_MERCHANT__POST_PRIVATE_TOKEN_H
#define _TALER_MERCHANT__POST_PRIVATE_TOKEN_H

#include <taler/merchant/common.h>


/**
 * Handle for a POST /private/token request.
 */
struct TALER_MERCHANT_PostPrivateTokenHandle;


/**
 * Possible options for the POST /private/token request.
 */
enum TALER_MERCHANT_PostPrivateTokenOption
{
  /**
   * End of list of options.
   */
  TALER_MERCHANT_POST_PRIVATE_TOKEN_OPTION_END = 0,

  /**
   * Duration for the token validity.
   */
  TALER_MERCHANT_POST_PRIVATE_TOKEN_OPTION_DURATION,

  /**
   * Whether the token may be refreshed.
   */
  TALER_MERCHANT_POST_PRIVATE_TOKEN_OPTION_REFRESHABLE,

  /**
   * Description for the token.
   */
  TALER_MERCHANT_POST_PRIVATE_TOKEN_OPTION_DESCRIPTION

};


/**
 * Value for an option for the POST /private/token request.
 */
struct TALER_MERCHANT_PostPrivateTokenOptionValue
{

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

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

    /**
     * Value if @e option is
     * #TALER_MERCHANT_POST_PRIVATE_TOKEN_OPTION_DURATION.
     */
    struct GNUNET_TIME_Relative duration;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_POST_PRIVATE_TOKEN_OPTION_REFRESHABLE.
     */
    bool refreshable;

    /**
     * Value if @e option is
     * #TALER_MERCHANT_POST_PRIVATE_TOKEN_OPTION_DESCRIPTION.
     */
    const char *description;

  } details;

};


/**
 * Response details for a POST /private/token request.
 */
struct TALER_MERCHANT_PostPrivateTokenResponse
{

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

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

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

      /**
       * The access token (RFC 8959 secret-token: URI).
       */
      const char *access_token;

      /**
       * The scope of the token.
       */
      const char *scope;

      /**
       * Whether the token may be refreshed.
       */
      bool refreshable;

      /**
       * When the token expires.
       */
      struct GNUNET_TIME_Timestamp expiration;

    } ok;

    /**
     * Details on #MHD_HTTP_ACCEPTED.
     */
    struct TALER_MERCHANT_MfaChallengeResponse accepted;

  } details;

};


/**
 * Terminate the list of the options.
 *
 * @return the terminating object
 */
#define TALER_MERCHANT_post_private_token_option_end_()                \
        (const struct TALER_MERCHANT_PostPrivateTokenOptionValue)      \
        {                                                               \
          .option = TALER_MERCHANT_POST_PRIVATE_TOKEN_OPTION_END       \
        }

/**
 * Set duration for token validity.
 *
 * @param d the duration
 * @return representation of the option
 */
#define TALER_MERCHANT_post_private_token_option_duration(d)           \
        (const struct TALER_MERCHANT_PostPrivateTokenOptionValue)      \
        {                                                               \
          .option = TALER_MERCHANT_POST_PRIVATE_TOKEN_OPTION_DURATION, \
          .details.duration = (d)                                       \
        }

/**
 * Set whether token is refreshable.
 *
 * @param r true if token may be refreshed
 * @return representation of the option
 */
#define TALER_MERCHANT_post_private_token_option_refreshable(r)        \
        (const struct TALER_MERCHANT_PostPrivateTokenOptionValue)      \
        {                                                               \
          .option = TALER_MERCHANT_POST_PRIVATE_TOKEN_OPTION_REFRESHABLE, \
          .details.refreshable = (r)                                    \
        }

/**
 * Set description for the token.
 *
 * @param d description string
 * @return representation of the option
 */
#define TALER_MERCHANT_post_private_token_option_description(d)        \
        (const struct TALER_MERCHANT_PostPrivateTokenOptionValue)      \
        {                                                               \
          .option = TALER_MERCHANT_POST_PRIVATE_TOKEN_OPTION_DESCRIPTION, \
          .details.description = (d)                                    \
        }


/**
 * Set the requested options for the operation.
 *
 * @param ppth 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_post_private_token_set_options_ (
  struct TALER_MERCHANT_PostPrivateTokenHandle *ppth,
  unsigned int num_options,
  const struct TALER_MERCHANT_PostPrivateTokenOptionValue *options);


/**
 * Set the requested options for the operation.
 *
 * @param ppth the request to set the options for
 * @param ... the list of the options
 * @return #GNUNET_OK on success,
 *         #GNUNET_NO on failure,
 *         #GNUNET_SYSERR on internal error
 */
#define TALER_MERCHANT_post_private_token_set_options(ppth,...)                \
        TALER_MERCHANT_post_private_token_set_options_ (                       \
          ppth,                                                                \
          TALER_MERCHANT_COMMON_OPTIONS_ARRAY_MAX_SIZE,                        \
          ((const struct TALER_MERCHANT_PostPrivateTokenOptionValue[])          \
           {__VA_ARGS__, TALER_MERCHANT_post_private_token_option_end_ () }    \
          ))


/**
 * Set up POST /private/token 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 instance_id identifier of the instance
 * @param scope scope for the token
 * @return handle to operation
 */
struct TALER_MERCHANT_PostPrivateTokenHandle *
TALER_MERCHANT_post_private_token_create (
  struct GNUNET_CURL_Context *ctx,
  const char *url,
  const char *instance_id,
  const char *scope);


#ifndef TALER_MERCHANT_POST_PRIVATE_TOKEN_RESULT_CLOSURE
/**
 * Type of the closure used by
 * the #TALER_MERCHANT_PostPrivateTokenCallback.
 */
#define TALER_MERCHANT_POST_PRIVATE_TOKEN_RESULT_CLOSURE void
#endif /* TALER_MERCHANT_POST_PRIVATE_TOKEN_RESULT_CLOSURE */

/**
 * Callback for a POST /private/token request.
 *
 * @param cls closure
 * @param ptr response details
 */
typedef void
(*TALER_MERCHANT_PostPrivateTokenCallback)(
  TALER_MERCHANT_POST_PRIVATE_TOKEN_RESULT_CLOSURE *cls,
  const struct TALER_MERCHANT_PostPrivateTokenResponse *ptr);


/**
 * Start POST /private/token operation.
 *
 * @param[in,out] ppth 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_post_private_token_start (
  struct TALER_MERCHANT_PostPrivateTokenHandle *ppth,
  TALER_MERCHANT_PostPrivateTokenCallback cb,
  TALER_MERCHANT_POST_PRIVATE_TOKEN_RESULT_CLOSURE *cb_cls);


/**
 * Cancel POST /private/token operation.  This function must not be
 * called by clients after the TALER_MERCHANT_PostPrivateTokenCallback
 * has been invoked (as in those cases it'll be called internally by the
 * implementation already).
 *
 * @param[in] ppth operation to cancel
 */
void
TALER_MERCHANT_post_private_token_cancel (
  struct TALER_MERCHANT_PostPrivateTokenHandle *ppth);


#endif /* _TALER_MERCHANT__POST_PRIVATE_TOKEN_H */
