/*
  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/token_family_serialize.c
 * @brief shared logic for token family 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 contract token family key.
 *
 * @param[in] key contract token family key
 * @return JSON representation of @a key; NULL on error
 */
static json_t *
json_from_token_family_key (
  const struct TALER_MERCHANT_ContractTokenFamilyKey *key)
{
  return GNUNET_JSON_PACK (
    GNUNET_JSON_pack_timestamp ("signature_validity_start",
                                key->valid_after),
    GNUNET_JSON_pack_timestamp ("signature_validity_end",
                                key->valid_before),
    TALER_JSON_pack_token_pub (NULL,
                               &key->pub));
}


/**
 * Get JSON representation of contract token family details.
 *
 * @param[in] family contract token family
 * @return JSON representation of @a family->details; NULL on error
 */
static json_t *
json_from_token_family_details (
  const struct TALER_MERCHANT_ContractTokenFamily *family)
{
  switch (family->kind)
  {
  case TALER_MERCHANT_CONTRACT_TOKEN_KIND_INVALID:
    break;
  case TALER_MERCHANT_CONTRACT_TOKEN_KIND_SUBSCRIPTION:
    {
      json_t *trusted_domains;

      trusted_domains = json_array ();
      GNUNET_assert (NULL != trusted_domains);
      for (unsigned int i = 0;
           i < family->details.subscription.trusted_domains_len;
           i++)
        GNUNET_assert (0 ==
                       json_array_append_new (
                         trusted_domains,
                         json_string (
                           family->details.subscription.trusted_domains[i])));

      return GNUNET_JSON_PACK (
        GNUNET_JSON_pack_string ("class",
                                 "subscription"),
        GNUNET_JSON_pack_array_steal ("trusted_domains",
                                      trusted_domains));
    }
  case TALER_MERCHANT_CONTRACT_TOKEN_KIND_DISCOUNT:
    {
      json_t *expected_domains;

      expected_domains = json_array ();
      GNUNET_assert (NULL != expected_domains);
      for (unsigned int i = 0;
           i < family->details.discount.expected_domains_len;
           i++)
        GNUNET_assert (0 ==
                       json_array_append_new (
                         expected_domains,
                         json_string (
                           family->details.discount.expected_domains[i])));

      return GNUNET_JSON_PACK (
        GNUNET_JSON_pack_string ("class",
                                 "discount"),
        GNUNET_JSON_pack_array_steal ("expected_domains",
                                      expected_domains));
    }
  }

  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
              "unsupported token family kind %d",
              family->kind);
  GNUNET_assert (0);
  return NULL;
}


json_t *
TALER_MERCHANT_json_from_token_family (
  const struct TALER_MERCHANT_ContractTokenFamily *family)
{
  json_t *keys;

  keys = json_array ();
  GNUNET_assert (NULL != keys);
  for (unsigned int i = 0; i < family->keys_len; i++)
    GNUNET_assert (0 == json_array_append_new (
                     keys,
                     json_from_token_family_key (
                       &family->keys[i])));

  return GNUNET_JSON_PACK (
    GNUNET_JSON_pack_string ("name",
                             family->name),
    GNUNET_JSON_pack_string ("description",
                             family->description),
    GNUNET_JSON_pack_allow_null (
      GNUNET_JSON_pack_object_incref ("description_i18n",
                                      family->description_i18n)),
    GNUNET_JSON_pack_array_steal ("keys",
                                  keys),
    GNUNET_JSON_pack_object_steal ("details",
                                   json_from_token_family_details (family)),
    GNUNET_JSON_pack_bool ("critical",
                           family->critical));
}
