/*
   This file is part of TALER
   Copyright (C) 2022, 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/backenddb/update_product.c
 * @brief Implementation of the update_product function for Postgres
 * @author Christian Grothoff
 * @author Iván Ávalos
 */
#include "platform.h"
#include <taler/taler_pq_lib.h>
#include "merchant-database/update_product.h"
#include "helper.h"


enum GNUNET_DB_QueryStatus
TALER_MERCHANTDB_update_product (
  struct TALER_MERCHANTDB_PostgresContext *pg,
  const char *instance_id,
  const char *product_id,
  const struct TALER_MERCHANTDB_ProductDetails *pd,
  size_t num_cats,
  const uint64_t *cats,
  ssize_t *no_cat,
  bool *no_product,
  bool *lost_reduced,
  bool *sold_reduced,
  bool *stocked_reduced,
  bool *no_group,
  bool *no_pot)
{
  struct GNUNET_PQ_QueryParam params[] = {
    GNUNET_PQ_query_param_string (product_id), /* $1 */
    GNUNET_PQ_query_param_string (pd->description),
    TALER_PQ_query_param_json (pd->description_i18n), /* $3 */
    GNUNET_PQ_query_param_string (pd->unit),
    GNUNET_PQ_query_param_string (pd->image), /* $5 */
    TALER_PQ_query_param_json (pd->taxes), /* $6 */
    TALER_PQ_query_param_array_amount_with_currency (
      pd->price_array_length,
      pd->price_array,
      "merchant",
      pg->conn), /* $7 */
    GNUNET_PQ_query_param_uint64 (&pd->total_stock),  /* $8 */
    GNUNET_PQ_query_param_uint32 (&pd->total_stock_frac), /* $9 */
    GNUNET_PQ_query_param_bool (pd->allow_fractional_quantity), /* $10 */
    GNUNET_PQ_query_param_uint32 (&pd->fractional_precision_level),
    GNUNET_PQ_query_param_uint64 (&pd->total_lost),
    TALER_PQ_query_param_json (pd->address), /* $13 */
    GNUNET_PQ_query_param_timestamp (&pd->next_restock),
    GNUNET_PQ_query_param_uint32 (&pd->minimum_age),
    GNUNET_PQ_query_param_array_uint64 (num_cats,
                                        cats,
                                        pg->conn), /* $16 */
    GNUNET_PQ_query_param_string (pd->product_name), /* $17 */
    (0 == pd->product_group_id)
    ? GNUNET_PQ_query_param_null ()
    : GNUNET_PQ_query_param_uint64 (&pd->product_group_id),
    (0 == pd->money_pot_id)
    ? GNUNET_PQ_query_param_null ()
    : GNUNET_PQ_query_param_uint64 (&pd->money_pot_id), /* $19 */
    GNUNET_PQ_query_param_bool (pd->price_is_net),
    GNUNET_PQ_query_param_end
  };
  uint64_t ncat;
  bool cats_found = true;
  struct GNUNET_PQ_ResultSpec rs[] = {
    GNUNET_PQ_result_spec_bool ("no_product",
                                no_product),
    GNUNET_PQ_result_spec_bool ("lost_reduced",
                                lost_reduced),
    GNUNET_PQ_result_spec_bool ("sold_reduced",
                                sold_reduced),
    GNUNET_PQ_result_spec_bool ("stocked_reduced",
                                stocked_reduced),
    GNUNET_PQ_result_spec_allow_null (
      GNUNET_PQ_result_spec_uint64 ("no_cat",
                                    &ncat),
      &cats_found),
    GNUNET_PQ_result_spec_bool ("no_group",
                                no_group),
    GNUNET_PQ_result_spec_bool ("no_pot",
                                no_pot),
    GNUNET_PQ_result_spec_end
  };
  enum GNUNET_DB_QueryStatus qs;

  if ( (pd->total_stock < pd->total_lost + pd->total_sold) ||
       (pd->total_lost > pd->total_lost
        + pd->total_sold) /* integer overflow */)
  {
    GNUNET_break (0);
    return GNUNET_DB_STATUS_HARD_ERROR;
  }
  GNUNET_assert (NULL != pg->current_merchant_id);
  GNUNET_assert (0 == strcmp (instance_id,
                              pg->current_merchant_id));
  TMH_PQ_prepare_anon (pg,
                       "SELECT"
                       " out_lost_reduced AS lost_reduced"
                       ",out_sold_reduced AS sold_reduced"
                       ",out_stocked_reduced AS stocked_reduced"
                       ",out_no_product AS no_product"
                       ",out_no_cat AS no_cat"
                       ",out_no_group AS no_group"
                       ",out_no_pot AS no_pot"
                       " FROM merchant_do_update_product"
                       "($1,$2,$3::TEXT::JSONB,$4,$5,$6::TEXT::JSONB,$7,$8"
                       ",$9,$10,$11,$12,$13::TEXT::JSONB,$14,$15,$16,$17"
                       ",$18,$19,$20);");
  qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
                                                 "",
                                                 params,
                                                 rs);
  GNUNET_PQ_cleanup_query_params_closures (params);
  *no_cat = (cats_found) ? -1 : (ssize_t) ncat;
  return qs;
}
