/*
  This file is part of TALER
  (C) 2026 Taler Systems SA

  TALER is free software; you can redistribute it and/or modify
  it under the terms of the GNU Affero 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/backend/taler-merchant-httpd_post-private-accept-tos-early.c
 * @brief implementing POST /private/accept-tos-early request handling
 * @author Christian Grothoff
 */
#include "platform.h"
#include "taler-merchant-httpd_post-private-accept-tos-early.h"
#include "taler-merchant-httpd_helper.h"
#include <taler/taler_json_lib.h>
#include "merchant-database/insert_tos_accepted_early.h"


/**
 * How often do we retry the simple INSERT database transaction?
 */
#define MAX_RETRIES 3


enum MHD_Result
TMH_private_post_accept_tos_early (
  const struct TMH_RequestHandler *rh,
  struct MHD_Connection *connection,
  struct TMH_HandlerContext *hc)
{
  struct TMH_MerchantInstance *mi = hc->instance;
  const char *exchange_url;
  const char *tos_version;
  struct GNUNET_JSON_Specification spec[] = {
    TALER_JSON_spec_web_url ("exchange_url",
                             &exchange_url),
    GNUNET_JSON_spec_string ("tos_version",
                             &tos_version),
    GNUNET_JSON_spec_end ()
  };
  enum GNUNET_DB_QueryStatus qs;

  (void) rh;
  GNUNET_assert (NULL != mi);
  {
    enum GNUNET_GenericReturnValue res;

    res = TALER_MHD_parse_json_data (connection,
                                     hc->request_body,
                                     spec);
    if (GNUNET_OK != res)
    {
      GNUNET_break_op (0);
      return (GNUNET_NO == res)
             ? MHD_YES
             : MHD_NO;
    }
  }
  for (unsigned int i = 0; i < MAX_RETRIES; i++)
  {
    qs = TALER_MERCHANTDB_insert_tos_accepted_early (TMH_db,
                                                     mi->settings.id,
                                                     exchange_url,
                                                     tos_version);
    if (GNUNET_DB_STATUS_SOFT_ERROR != qs)
      break;
  }
  GNUNET_JSON_parse_free (spec);
  if (qs < 0)
  {
    GNUNET_break (0);
    return TALER_MHD_reply_with_error (
      connection,
      MHD_HTTP_INTERNAL_SERVER_ERROR,
      (GNUNET_DB_STATUS_SOFT_ERROR == qs)
      ? TALER_EC_GENERIC_DB_SOFT_FAILURE
      : TALER_EC_GENERIC_DB_STORE_FAILED,
      NULL);
  }
  return TALER_MHD_reply_static (connection,
                                 MHD_HTTP_NO_CONTENT,
                                 NULL,
                                 NULL,
                                 0);
}


/* end of taler-merchant-httpd_post-private-accept-tos-early.c */
