/*
  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-management-instances-INSTANCE-statistics-amount-SLUG.h
 * @brief C interface for GET .../statistics/amount/$SLUG of the merchant backend
 * @author Christian Grothoff
 */
#ifndef \
  _TALER_MERCHANT__GET_MANAGEMENT_INSTANCES_INSTANCE_STATISTICS_AMOUNT_SLUG_H
#define \
        _TALER_MERCHANT__GET_MANAGEMENT_INSTANCES_INSTANCE_STATISTICS_AMOUNT_SLUG_H

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


/**
 * Statistic amount aggregated by bucket.
 */
struct TALER_MERCHANT_StatisticAmountByBucket
{
  /**
   * 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.
   */
  struct TALER_Amount *cumulative_amounts;

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


/**
 * Statistic amount aggregated by interval.
 */
struct TALER_MERCHANT_StatisticAmountByInterval
{
  /**
   * 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.
   */
  struct TALER_Amount *cumulative_amounts;

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

};


/**
 * Handle for a GET .../statistics/amount/$SLUG request.
 */
struct TALER_MERCHANT_StatisticsAmountGetHandle;


/**
 * Response details for a GET .../statistics/amount/$SLUG request.
 */
struct TALER_MERCHANT_StatisticsAmountGetResponse
{

  /**
   * 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_StatisticAmountByBucket *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_StatisticAmountByInterval *intervals;

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

    } ok;

  } details;

};


/**
 * Callback for a GET .../statistics/amount/$SLUG request.
 *
 * @param cls closure
 * @param sagr response details
 */
typedef void
(*TALER_MERCHANT_StatisticsAmountGetCallback)(
  void *cls,
  const struct TALER_MERCHANT_StatisticsAmountGetResponse *sagr);


/**
 * Issue a GET .../statistics/amount/$SLUG request.
 *
 * @param ctx CURL context to use
 * @param backend_url base URL of the merchant backend
 * @param slug statistics slug to retrieve
 * @param stype aggregation mode to request
 * @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_StatisticsAmountGetHandle *
TALER_MERCHANT_statistic_amount_get (
  struct GNUNET_CURL_Context *ctx,
  const char *backend_url,
  const char *slug,
  enum TALER_MERCHANT_StatisticsType stype,
  TALER_MERCHANT_StatisticsAmountGetCallback cb,
  void *cb_cls);


/**
 * Cancel a GET .../statistics/amount/$SLUG request.
 *
 * @param[in] sagh handle to cancel
 */
void
TALER_MERCHANT_statistic_amount_get_cancel (
  struct TALER_MERCHANT_StatisticsAmountGetHandle *sagh);


#endif
