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

#include <taler/merchant/common.h>


/**
 * Possible options we can set for the GET /private/statistics-amount/$SLUG request.
 */
enum TALER_MERCHANT_GetPrivateStatisticsAmountOption
{
  /**
   * End of list of options.
   */
  TALER_MERCHANT_GET_PRIVATE_STATISTICS_AMOUNT_OPTION_END = 0,

  /**
   * Aggregation mode to request (BUCKET, INTERVAL, or ANY).
   */
  TALER_MERCHANT_GET_PRIVATE_STATISTICS_AMOUNT_OPTION_TYPE

};


/**
 * Value for an option for the GET /private/statistics-amount/$SLUG request.
 */
struct TALER_MERCHANT_GetPrivateStatisticsAmountOptionValue
{

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

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

    /**
     * Value if @e option is
     * #TALER_MERCHANT_GET_PRIVATE_STATISTICS_AMOUNT_OPTION_TYPE.
     */
    enum TALER_MERCHANT_StatisticsType type;

  } details;

};


/**
 * Handle for a GET /private/statistics-amount/$SLUG request.
 */
struct TALER_MERCHANT_GetPrivateStatisticsAmountHandle;


/**
 * Set up GET /private/statistics-amount/$SLUG 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 slug statistics slug to retrieve
 * @return handle to operation
 */
struct TALER_MERCHANT_GetPrivateStatisticsAmountHandle *
TALER_MERCHANT_get_private_statistics_amount_create (
  struct GNUNET_CURL_Context *ctx,
  const char *url,
  const char *slug);


/**
 * Terminate the list of the options.
 *
 * @return the terminating object of struct TALER_MERCHANT_GetPrivateStatisticsAmountOptionValue
 */
#define TALER_MERCHANT_get_private_statistics_amount_option_end_()                \
        (const struct TALER_MERCHANT_GetPrivateStatisticsAmountOptionValue)       \
        {                                                                          \
          .option = TALER_MERCHANT_GET_PRIVATE_STATISTICS_AMOUNT_OPTION_END       \
        }

/**
 * Set aggregation mode type.
 *
 * @param t aggregation mode to request
 * @return representation of the option as a struct TALER_MERCHANT_GetPrivateStatisticsAmountOptionValue
 */
#define TALER_MERCHANT_get_private_statistics_amount_option_type(t)              \
        (const struct TALER_MERCHANT_GetPrivateStatisticsAmountOptionValue)      \
        {                                                                         \
          .option = TALER_MERCHANT_GET_PRIVATE_STATISTICS_AMOUNT_OPTION_TYPE,    \
          .details.type = (t)                                                     \
        }


/**
 * Set the requested options for the operation.
 *
 * If any option fail other options may be or may be not applied.
 *
 * @param sah 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_statistics_amount_set_options_ (
  struct TALER_MERCHANT_GetPrivateStatisticsAmountHandle *sah,
  unsigned int num_options,
  const struct TALER_MERCHANT_GetPrivateStatisticsAmountOptionValue *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_statistics_amount_set_options (
 *   sah,
 *   TALER_MERCHANT_get_private_statistics_amount_option_type (
 *     TALER_MERCHANT_STATISTICS_TYPE_BUCKET));
 *
 * @param sah the request to set the options for
 * @param ... the list of the options, each option must be created
 *            by helpers TALER_MERCHANT_get_private_statistics_amount_option_NAME(VALUE)
 * @return #GNUNET_OK on success,
 *         #GNUNET_NO on failure,
 *         #GNUNET_SYSERR on internal error
 */
#define TALER_MERCHANT_get_private_statistics_amount_set_options(sah,...)                \
        TALER_MERCHANT_get_private_statistics_amount_set_options_ (                       \
          sah,                                                                            \
          TALER_MERCHANT_COMMON_OPTIONS_ARRAY_MAX_SIZE,                                  \
          ((const struct TALER_MERCHANT_GetPrivateStatisticsAmountOptionValue[])         \
           {__VA_ARGS__, \
            TALER_MERCHANT_get_private_statistics_amount_option_end_ () }   \
          ))


/**
 * Statistic amount aggregated by bucket.
 */
struct TALER_MERCHANT_GetPrivateStatisticsAmountByBucket
{

  /**
   * Start time of the bucket (inclusive).
   */
  struct GNUNET_TIME_Timestamp start_time;

  /**
   * End time of the bucket (exclusive).
   */
  struct GNUNET_TIME_Timestamp end_time;

  /**
   * Range of the bucket.
   */
  const char *range;

  /**
   * Sum of all amounts falling under the given
   * SLUG within this timeframe.
   */
  const struct TALER_Amount *cumulative_amounts;

  /**
   * Length of array @a cumulative_amounts.
   */
  unsigned int cumulative_amount_len;

};


/**
 * Statistic amount aggregated by interval.
 */
struct TALER_MERCHANT_GetPrivateStatisticsAmountByInterval
{

  /**
   * Start time of the interval (inclusive).
   * The interval always ends at the response
   * generation time.
   */
  struct GNUNET_TIME_Timestamp start_time;

  /**
   * Sum of all amounts falling under the given
   * SLUG within this timeframe.
   */
  const struct TALER_Amount *cumulative_amounts;

  /**
   * Length of array @a cumulative_amounts.
   */
  unsigned int cumulative_amount_len;

};


/**
 * Response details for a GET /private/statistics-amount/$SLUG request.
 */
struct TALER_MERCHANT_GetPrivateStatisticsAmountResponse
{

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

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

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

      /**
       * Number of bucket entries in @a buckets.
       */
      unsigned int buckets_length;

      /**
       * Array of amount statistics per bucket.
       */
      const struct TALER_MERCHANT_GetPrivateStatisticsAmountByBucket *buckets;

      /**
       * Description of the bucket aggregation scheme, or NULL.
       */
      const char *buckets_description;

      /**
       * Number of interval entries in @a intervals.
       */
      unsigned int intervals_length;

      /**
       * Array of amount statistics per time interval.
       */
      const struct TALER_MERCHANT_GetPrivateStatisticsAmountByInterval *
        intervals;

      /**
       * Description of the interval aggregation scheme, or NULL.
       */
      const char *intervals_description;

    } ok;

  } details;

};


#ifndef TALER_MERCHANT_GET_PRIVATE_STATISTICS_AMOUNT_RESULT_CLOSURE
/**
 * Type of the closure used by
 * the #TALER_MERCHANT_GetPrivateStatisticsAmountCallback.
 */
#define TALER_MERCHANT_GET_PRIVATE_STATISTICS_AMOUNT_RESULT_CLOSURE void
#endif /* TALER_MERCHANT_GET_PRIVATE_STATISTICS_AMOUNT_RESULT_CLOSURE */

/**
 * Callback for a GET /private/statistics-amount/$SLUG request.
 *
 * @param cls closure
 * @param sagr response details
 */
typedef void
(*TALER_MERCHANT_GetPrivateStatisticsAmountCallback)(
  TALER_MERCHANT_GET_PRIVATE_STATISTICS_AMOUNT_RESULT_CLOSURE *cls,
  const struct TALER_MERCHANT_GetPrivateStatisticsAmountResponse *sagr);


/**
 * Start GET /private/statistics-amount/$SLUG operation.
 *
 * @param[in,out] sah 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_statistics_amount_start (
  struct TALER_MERCHANT_GetPrivateStatisticsAmountHandle *sah,
  TALER_MERCHANT_GetPrivateStatisticsAmountCallback cb,
  TALER_MERCHANT_GET_PRIVATE_STATISTICS_AMOUNT_RESULT_CLOSURE *cb_cls);


/**
 * Cancel GET /private/statistics-amount/$SLUG operation.  This function
 * must not be called by clients after the
 * TALER_MERCHANT_GetPrivateStatisticsAmountCallback has been invoked
 * (as in those cases it'll be called internally by the implementation
 * already).
 *
 * @param[in] sah operation to cancel
 */
void
TALER_MERCHANT_get_private_statistics_amount_cancel (
  struct TALER_MERCHANT_GetPrivateStatisticsAmountHandle *sah);


#endif /* _TALER_MERCHANT__GET_PRIVATE_STATISTICS_AMOUNT_SLUG_H */
