/*
  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 taler-merchant-httpd_post-orders-ORDER_ID-unclaim.c
 * @brief headers for POST /orders/$ID/unclaim handler
 * @author Christian Grothoff
 */
#include "taler/platform.h"
#include <jansson.h>
#include <taler/taler_signatures.h>
#include <taler/taler_dbevents.h>
#include <taler/taler_json_lib.h>
#include "taler-merchant-httpd_get-private-orders.h"
#include "taler-merchant-httpd_post-orders-ORDER_ID-unclaim.h"


MHD_RESULT
TMH_post_orders_ID_unclaim (const struct TMH_RequestHandler *rh,
                            struct MHD_Connection *connection,
                            struct TMH_HandlerContext *hc)
{
  const char *order_id = hc->infix;
  struct GNUNET_CRYPTO_EddsaPublicKey nonce;
  struct GNUNET_CRYPTO_EddsaSignature nsig;
  struct GNUNET_HashCode h_contract;
  enum GNUNET_DB_QueryStatus qs;

  {
    struct GNUNET_JSON_Specification spec[] = {
      GNUNET_JSON_spec_fixed_auto ("unclaim_sig",
                                   &nsig),
      GNUNET_JSON_spec_fixed_auto ("nonce",
                                   &nonce),
      GNUNET_JSON_spec_fixed_auto ("h_contract",
                                   &h_contract),
      GNUNET_JSON_spec_end ()
    };
    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;
    }
  }

  if (GNUNET_OK !=
      TALER_wallet_order_unclaim_verify (&h_contract,
                                         &nonce,
                                         &nsig))
  {
    GNUNET_break_op (0);
    return TALER_MHD_reply_with_error (
      connection,
      MHD_HTTP_BAD_REQUEST,
      TALER_EC_GENERIC_PARAMETER_MALFORMED,
      "unclaim_sig");
  }
  TMH_db->preflight (TMH_db->cls);
  qs = TMH_db->insert_unclaim_signature (TMH_db->cls,
                                         hc->instance->settings.id,
                                         &hc->instance->merchant_pub,
                                         order_id,
                                         &nonce,
                                         &h_contract,
                                         &nsig);
  switch (qs)
  {
  case GNUNET_DB_STATUS_HARD_ERROR:
    GNUNET_break (0);
    return TALER_MHD_reply_with_error (connection,
                                       MHD_HTTP_INTERNAL_SERVER_ERROR,
                                       TALER_EC_GENERIC_DB_COMMIT_FAILED,
                                       "insert_unclaim_signature");
  case GNUNET_DB_STATUS_SOFT_ERROR:
    GNUNET_break (0);
    return TALER_MHD_reply_with_error (connection,
                                       MHD_HTTP_INTERNAL_SERVER_ERROR,
                                       TALER_EC_GENERIC_DB_SOFT_FAILURE,
                                       "insert_unclaim_signature");
  case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
    GNUNET_break (0);
    return TALER_MHD_reply_with_error (
      connection,
      MHD_HTTP_NOT_FOUND,
      TALER_EC_MERCHANT_POST_ORDERS_ID_CLAIM_NOT_FOUND,
      order_id);
  case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
    break; /* Good! return signature (below) */
  }

  return TALER_MHD_reply_static (connection,
                                 MHD_HTTP_NO_CONTENT,
                                 NULL,
                                 NULL,
                                 0);
}


/* end of taler-merchant-httpd_post-orders-ORDER_ID-unclaim.c */
