/*
  This file is part of TALER
  (C) 2024, 2025 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 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/base_terms_parse.c
 * @brief shared logic for order and contract terms parsing
 * @author Iván Ávalos
 * @author Christian Grothoff
 */
#include "platform.h"
#include <gnunet/gnunet_common.h>
#include <gnunet/gnunet_json_lib.h>
#include <jansson.h>
#include <stdbool.h>
#include <stdint.h>
#include <taler/taler_json_lib.h>
#include <taler/taler_util.h>
#include "taler/taler_merchant_util.h"


struct TALER_MERCHANT_ContractBaseTerms *
TALER_MERCHANT_base_terms_parse (
  json_t *input)
{
  struct TALER_MERCHANT_ContractBaseTerms *ct
    = GNUNET_new (struct TALER_MERCHANT_ContractBaseTerms);
  struct GNUNET_JSON_Specification espec[] = {
    GNUNET_JSON_spec_mark_optional (
      TALER_MERCHANT_spec_contract_version ("version",
                                            &ct->version),
      NULL),
    GNUNET_JSON_spec_string_copy ("summary",
                                  &ct->summary),
    /* FIXME: do i18n_str validation in the future */
    GNUNET_JSON_spec_mark_optional (
      GNUNET_JSON_spec_object_copy ("summary_i18n",
                                    &ct->summary_i18n),
      NULL),
    GNUNET_JSON_spec_mark_optional (
      TALER_JSON_spec_web_url_copy ("public_reorder_url",
                                    &ct->public_reorder_url),
      NULL),
    GNUNET_JSON_spec_mark_optional (
      GNUNET_JSON_spec_string_copy ("fulfillment_url",
                                    &ct->fulfillment_url),
      NULL),
    GNUNET_JSON_spec_mark_optional (
      GNUNET_JSON_spec_string_copy ("fulfillment_message",
                                    &ct->fulfillment_message),
      NULL),
    GNUNET_JSON_spec_mark_optional (
      GNUNET_JSON_spec_object_copy ("fulfillment_message_i18n",
                                    &ct->fulfillment_message_i18n),
      NULL),
    GNUNET_JSON_spec_mark_optional (
      GNUNET_JSON_spec_object_copy ("delivery_location",
                                    &ct->delivery_location),
      NULL),
    GNUNET_JSON_spec_mark_optional (
      GNUNET_JSON_spec_timestamp ("delivery_date",
                                  &ct->delivery_date),
      NULL),
    GNUNET_JSON_spec_mark_optional (
      GNUNET_JSON_spec_timestamp ("max_pickup_time",
                                  &ct->max_pickup_time),
      NULL),
    GNUNET_JSON_spec_mark_optional (
      GNUNET_JSON_spec_relative_time ("auto_refund",
                                      &ct->auto_refund),
      NULL),
    GNUNET_JSON_spec_mark_optional (
      GNUNET_JSON_spec_object_copy ("extra",
                                    &ct->extra),
      NULL),
    GNUNET_JSON_spec_mark_optional (
      GNUNET_JSON_spec_uint8 ("minimum_age",
                              &ct->minimum_age),
      NULL),
    GNUNET_JSON_spec_mark_optional (
      GNUNET_JSON_spec_uint64 ("order_default_money_pot",
                               &ct->default_money_pot),
      NULL),
    GNUNET_JSON_spec_end ()
  };
  enum GNUNET_GenericReturnValue res;
  const char *ename;
  unsigned int eline;

  GNUNET_assert (NULL != input);
  ct->max_pickup_time = GNUNET_TIME_UNIT_FOREVER_TS;
  ct->version = TALER_MERCHANT_CONTRACT_VERSION_0;
  res = GNUNET_JSON_parse (input,
                           espec,
                           &ename,
                           &eline);
  if (GNUNET_OK != res)
  {
    GNUNET_break (0);
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Failed to parse common terms at field %s\n",
                ename);
    goto cleanup;
  }
  return ct;

cleanup:
  TALER_MERCHANT_base_terms_free (ct);
  return NULL;
}


void
TALER_MERCHANT_base_terms_free (
  struct TALER_MERCHANT_ContractBaseTerms *ct)
{
  if (NULL == ct)
    return;
  GNUNET_free (ct->public_reorder_url);
  GNUNET_free (ct->summary);
  GNUNET_free (ct->fulfillment_url);
  GNUNET_free (ct->fulfillment_message);
  if (NULL != ct->fulfillment_message_i18n)
  {
    json_decref (ct->fulfillment_message_i18n);
    ct->fulfillment_message_i18n = NULL;
  }
  if (NULL != ct->delivery_location)
  {
    json_decref (ct->delivery_location);
    ct->delivery_location = NULL;
  }
  if (NULL != ct->extra)
  {
    json_decref (ct->extra);
    ct->extra = NULL;
  }
  if (NULL != ct->summary_i18n)
  {
    json_decref (ct->summary_i18n);
    ct->summary_i18n = NULL;
  }
  GNUNET_free (ct);
}
