/*
  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/merchant_parse.c
 * @brief shared logic for merchant metadata 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"


/**
 * Parse merchant details of given JSON order terms.
 *
 * @param cls closure, unused parameter
 * @param root the JSON object representing data
 * @param[out] ospec where to write the data
 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
 */
static enum GNUNET_GenericReturnValue
parse_merchant_details (void *cls,
                        json_t *root,
                        struct GNUNET_JSON_Specification *ospec)
{
  struct TALER_MERCHANT_MetaData *merchant = ospec->ptr;
  struct GNUNET_JSON_Specification spec[] = {
    GNUNET_JSON_spec_string_copy ("name",
                                  &merchant->name),
    GNUNET_JSON_spec_mark_optional (
      GNUNET_JSON_spec_string_copy ("email",
                                    &merchant->email),
      NULL),
    GNUNET_JSON_spec_mark_optional (
      TALER_JSON_spec_web_url_copy ("website",
                                    &merchant->website),
      NULL),
    GNUNET_JSON_spec_mark_optional (
      GNUNET_JSON_spec_string_copy ("logo",
                                    &merchant->logo),
      NULL),
    GNUNET_JSON_spec_mark_optional (
      GNUNET_JSON_spec_object_copy ("address",
                                    &merchant->address),
      NULL),
    GNUNET_JSON_spec_mark_optional (
      GNUNET_JSON_spec_object_copy ("jurisdiction",
                                    &merchant->jurisdiction),
      NULL),
    GNUNET_JSON_spec_end ()
  };
  const char *error_name;
  unsigned int error_line;

  (void) cls;
  if (GNUNET_OK !=
      GNUNET_JSON_parse (root,
                         spec,
                         &error_name,
                         &error_line))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                "Failed to parse %s at %u: %s\n",
                spec[error_line].field,
                error_line,
                error_name);
    GNUNET_break_op (0);
    return GNUNET_SYSERR;
  }
  return GNUNET_OK;
}


json_t *
TALER_MERCHANT_metadata_to_json (
  const struct TALER_MERCHANT_MetaData *merchant)
{
  return GNUNET_JSON_PACK (
    GNUNET_JSON_pack_string ("name",
                             merchant->name),
    GNUNET_JSON_pack_allow_null (
      GNUNET_JSON_pack_string ("email",
                               merchant->email)),
    GNUNET_JSON_pack_allow_null (
      GNUNET_JSON_pack_string ("website",
                               merchant->website)),
    GNUNET_JSON_pack_allow_null (
      GNUNET_JSON_pack_string ("logo",
                               merchant->logo)),
    GNUNET_JSON_pack_allow_null (
      GNUNET_JSON_pack_object_incref ("address",
                                      merchant->address)),
    GNUNET_JSON_pack_allow_null (
      GNUNET_JSON_pack_object_incref ("jurisdiction",
                                      merchant->jurisdiction)));
}


struct GNUNET_JSON_Specification
TALER_MERCHANT_spec_merchant_details (
  const char *name,
  struct TALER_MERCHANT_MetaData *merchant)
{
  struct GNUNET_JSON_Specification ret = {
    .parser = &parse_merchant_details,
    .field = name,
    .ptr = merchant,
  };

  return ret;
}


void
TALER_MERCHANT_metadata_free (
  struct TALER_MERCHANT_MetaData *merchant)
{
  GNUNET_free (merchant->name);
  GNUNET_free (merchant->website);
  GNUNET_free (merchant->email);
  GNUNET_free (merchant->logo);
  if (NULL != merchant->address)
  {
    json_decref (merchant->address);
    merchant->address = NULL;
  }
  if (NULL != merchant->jurisdiction)
  {
    json_decref (merchant->jurisdiction);
    merchant->jurisdiction = NULL;
  }
}
