/*
   This file is part of TALER
   Copyright (C) 2026 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 backenddb/pg_insert_unclaim_signature.c
 * @brief Implementation of the insert_unclaim_signature function for Postgres
 * @author Christian Grothoff
 */
#include "taler/platform.h"
#include <taler/taler_error_codes.h>
#include <taler/taler_dbevents.h>
#include <taler/taler_pq_lib.h>
#include "pg_insert_unclaim_signature.h"
#include "pg_helper.h"


static char *
get_notify_str (const char *order_id,
                const struct TALER_MerchantPublicKeyP *merchant_pub)
{
  struct TMH_OrderPayEventP pay_eh = {
    .header.size = htons (sizeof (pay_eh)),
    .header.type = htons (TALER_DBEVENT_MERCHANT_ORDER_STATUS_CHANGED),
    .merchant_pub = *merchant_pub
  };

  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "Notifying clients about status change of order %s\n",
              order_id);
  GNUNET_CRYPTO_hash (order_id,
                      strlen (order_id),
                      &pay_eh.h_order_id);
  return GNUNET_PQ_get_event_notify_channel (&pay_eh.header);
}


enum GNUNET_DB_QueryStatus
TMH_PG_insert_unclaim_signature (
  void *cls,
  const char *instance_id,
  const struct TALER_MerchantPublicKeyP *merchant_pub,
  const char *order_id,
  const struct GNUNET_CRYPTO_EddsaPublicKey *nonce,
  const struct GNUNET_HashCode *h_contract,
  const struct GNUNET_CRYPTO_EddsaSignature *nsig)
{
  struct PostgresClosure *pg = cls;
  char *nonce_str = GNUNET_STRINGS_data_to_string_alloc (nonce,
                                                         sizeof (*nonce));
  char *notify_str = get_notify_str (order_id,
                                     merchant_pub);
  struct GNUNET_PQ_QueryParam params[] = {
    GNUNET_PQ_query_param_string (instance_id),
    GNUNET_PQ_query_param_string (order_id),
    GNUNET_PQ_query_param_string (nonce_str),
    GNUNET_PQ_query_param_string (notify_str),
    GNUNET_PQ_query_param_auto_from_type (h_contract),
    GNUNET_PQ_query_param_auto_from_type (nsig),
    GNUNET_PQ_query_param_end
  };
  bool found;
  struct GNUNET_PQ_ResultSpec rs[] = {
    GNUNET_PQ_result_spec_bool ("out_found",
                                &found),
    GNUNET_PQ_result_spec_end
  };
  enum GNUNET_DB_QueryStatus qs;

  check_connection (pg);
  PREPARE (pg,
           "insert_unclaim_signature",
           "SELECT"
           "  out_found"
           "  FROM merchant_do_insert_unclaim_signature"
           "($1, $2, $3, $4, $5);");
  qs = GNUNET_PQ_eval_prepared_singleton_select (
    pg->conn,
    "insert_unclaim_signature",
    params,
    rs);
  GNUNET_free (nonce_str);
  GNUNET_free (notify_str);
  if (qs <= 0)
    return qs;
  return found
    ? GNUNET_DB_STATUS_SUCCESS_ONE_RESULT
    : GNUNET_DB_STATUS_SUCCESS_NO_RESULTS;
}
