/*
  This file is part of GNU Taler
  Copyright (C) 2024, 2025 Taler Systems SA

  TALER is free software; you can redistribute it and/or modify it under the
  terms of the GNU General Public License as published by the Free Software
  Foundation; either version 3, 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 General Public License for more details.

  You should have received a copy of the GNU General Public License along with
  TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
*/
/**
 * @file src/util/order_choice_serialize.c
 * @brief shared logic for order choice serialization
 * @author Iván Ávalos
 * @author Christian Grothoff
 */
#include "platform.h"
#include <gnunet/gnunet_json_lib.h>
#include <gnunet/gnunet_common.h>
#include <taler/taler_json_lib.h>
#include <jansson.h>
#include "taler/taler_util.h"
#include "taler/taler_merchant_util.h"

/**
 * Get JSON representation of order choice input.
 *
 * @param[in] input order terms choice input
 * @return JSON representation of @a input; NULL on error
 */
static json_t *
json_from_order_input (
  const struct TALER_MERCHANT_OrderInput *input)
{
  switch (input->type)
  {
  case TALER_MERCHANT_CONTRACT_INPUT_TYPE_INVALID:
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "invalid order input type");
    GNUNET_assert (0);
    return NULL;
  case TALER_MERCHANT_CONTRACT_INPUT_TYPE_TOKEN:
    return GNUNET_JSON_PACK (
      GNUNET_JSON_pack_string ("type",
                               "token"),
      GNUNET_JSON_pack_string ("token_family_slug",
                               input->details.token.token_family_slug),
      GNUNET_JSON_pack_uint64 ("count",
                               input->details.token.count));
  }

  GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
              "unsupported order input type %d",
              input->type);
  GNUNET_assert (0);
  return NULL;
}


/**
 * Get JSON representation of order choice output.
 *
 * @param[in] output order terms choice output
 * @return JSON representation of @a output; NULL on error
 */
static json_t *
json_from_order_output (
  const struct TALER_MERCHANT_OrderOutput *output)
{
  switch (output->type)
  {
  case TALER_MERCHANT_CONTRACT_OUTPUT_TYPE_INVALID:
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "invalid order output type");
    GNUNET_assert (0);
    return NULL;
  case TALER_MERCHANT_CONTRACT_OUTPUT_TYPE_TOKEN:
    return GNUNET_JSON_PACK (
      GNUNET_JSON_pack_string ("type",
                               "token"),
      GNUNET_JSON_pack_string ("token_family_slug",
                               output->details.token.token_family_slug),
      GNUNET_JSON_pack_uint64 ("count",
                               output->details.token.count),
      GNUNET_JSON_pack_conditional (
        ! GNUNET_TIME_absolute_is_zero (output->details.token.valid_at.abs_time),
        GNUNET_JSON_pack_timestamp ("valid_at",
                                    output->details.token.valid_at)));
  case TALER_MERCHANT_CONTRACT_OUTPUT_TYPE_DONATION_RECEIPT:
    return GNUNET_JSON_PACK (
      GNUNET_JSON_pack_string ("type",
                               "tax-receipt"),
      GNUNET_JSON_pack_allow_null (
        TALER_JSON_pack_amount ("amount",
                                output->details.donation_receipt.no_amount
                                ? NULL
                                : &output->details.donation_receipt.amount)));
  }
  GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
              "Unsupported order output type %d",
              output->type);
  GNUNET_assert (0);
  return NULL;
}


json_t *
TALER_MERCHANT_json_from_order_choice (
  const struct TALER_MERCHANT_OrderChoice *choice)
{
  json_t *inputs;
  json_t *outputs;

  inputs = json_array ();
  GNUNET_assert (NULL != inputs);
  for (unsigned int i = 0; i < choice->inputs_len; i++)
    GNUNET_assert (0 ==
                   json_array_append_new (inputs,
                                          json_from_order_input (
                                            &choice->inputs[i])));
  outputs = json_array ();
  GNUNET_assert (NULL != outputs);
  for (unsigned int i = 0; i < choice->outputs_len; i++)
    GNUNET_assert (0 ==
                   json_array_append_new (outputs,
                                          json_from_order_output (
                                            &choice->outputs[i])));

  return GNUNET_JSON_PACK (
    TALER_JSON_pack_amount ("amount",
                            &choice->amount),
    GNUNET_JSON_pack_allow_null (
      TALER_JSON_pack_amount ("tip",
                              choice->no_tip
                              ? NULL
                              : &choice->tip)),
    GNUNET_JSON_pack_allow_null (
      GNUNET_JSON_pack_string ("description",
                               choice->description)),
    GNUNET_JSON_pack_allow_null (
      GNUNET_JSON_pack_object_incref ("description_i18n",
                                      choice->description_i18n)),
    GNUNET_JSON_pack_allow_null (
      TALER_JSON_pack_amount ("max_fee",
                              choice->no_max_fee
                            ? NULL
                            : &choice->max_fee)),
    GNUNET_JSON_pack_array_steal ("inputs",
                                  inputs),
    GNUNET_JSON_pack_array_steal ("outputs",
                                  outputs));
}
