/*
   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 src/exchangedb/get_open_legitimization_measure.c
 * @brief Implementation of the get_open_legitimization_measure function for Postgres
 * @author Christian Grothoff
 */
#include "taler/taler_pq_lib.h"
#include "helper.h"
#include "exchange-database/get_open_legitimization_measure.h"


enum GNUNET_DB_QueryStatus
TALER_EXCHANGEDB_get_open_legitimization_measure (
  struct TALER_EXCHANGEDB_PostgresContext *pg,
  const struct TALER_NormalizedPaytoHashP *h_payto,
  uint64_t *legitimization_measure_serial_id,
  struct GNUNET_TIME_Absolute *start_time)
{
  struct GNUNET_PQ_QueryParam params[] = {
    GNUNET_PQ_query_param_auto_from_type (h_payto),
    GNUNET_PQ_query_param_end
  };
  struct GNUNET_PQ_ResultSpec rs[] = {
    GNUNET_PQ_result_spec_uint64 ("legitimization_measure_serial_id",
                                  legitimization_measure_serial_id),
    GNUNET_PQ_result_spec_absolute_time ("start_time",
                                         start_time),
    GNUNET_PQ_result_spec_end
  };

  /* `legitimization_measures' is keyed by the account's access token, which
     lives in `kyc_targets', while `legitimization_processes' and
     `legitimization_outcomes' are keyed by the normalized payto hash --
     hence the join.

     The second of slack on `start_time' is the same one as in
     iterate_legitimization_outcomes_above_serial_id.c: `decision_time' is a
     rounded timestamp while `start_time' is not, so an honest process can
     start up to a second "after" the decision it fed.  Subtracting from
     start_time rather than adding to decision_time keeps a decision_time of
     "forever" from overflowing. */
  PREPARE (pg,
           "get_open_legitimization_measure",
           "SELECT"
           " lm.legitimization_measure_serial_id"
           ",lm.start_time"
           " FROM legitimization_measures lm"
           " JOIN kyc_targets kt"
           "   USING (access_token)"
           " WHERE kt.h_normalized_payto=$1"
           "   AND NOT EXISTS ("
           "     SELECT 1"
           "       FROM legitimization_processes lp"
           "      WHERE lp.legitimization_measure_serial_id"
           "            =lm.legitimization_measure_serial_id"
           "        AND EXISTS ("
           "          SELECT 1"
           "            FROM legitimization_outcomes lo"
           "           WHERE lo.h_payto=kt.h_normalized_payto"
           "             AND lo.decision_time>=lp.start_time-1000000))"
           " ORDER BY lm.legitimization_measure_serial_id ASC"
           " LIMIT 1;");
  return GNUNET_PQ_eval_prepared_singleton_select (
    pg->conn,
    "get_open_legitimization_measure",
    params,
    rs);
}
