/*
This file is part of TALER
  Copyright (C) 2014 - 2020 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 include/donaudb_lib.h
 * @brief IO operations for the donau's private keys
 * @author Florian Dold
 * @author Benedikt Mueller
 * @author Christian Grothoff
 */
#ifndef DONAUDB_LIB_H
#define DONAUDB_LIB_H

#include <taler/taler_util.h>
#include <taler/taler_json_lib.h>
#include <taler/taler_error_codes.h>
#include <taler/taler_dbevents.h>
#include <taler/taler_pq_lib.h>
#include "donau_util.h"
#include "donau_signatures.h"


/**
 * Main handle for the database.
 */
struct DONAUDB_PostgresContext;


/**
 * Meta data about an donau signing key.
 */
struct DONAUDB_SignkeyMetaData
{
  /**
   * Start time of the validity period for this key.
   */
  struct GNUNET_TIME_Timestamp valid_from;

  /**
   * The donau will sign messages with this key between @e start and this time.
   */
  struct GNUNET_TIME_Timestamp expire_sign;

  /**
   * When do signatures with this sign key become invalid?
   * After this point, these signatures cannot be used in (legal)
   * disputes anymore, as the Donau is then allowed to destroy its side
   * of the evidence.  @e expire_legal is expected to be significantly
   * larger than @e expire_sign (by a year or more).
   */
  struct GNUNET_TIME_Timestamp expire_legal;

};

/**
 * Meta data about a charity.
 */
struct DONAUDB_CharityMetaData
{
  /**
   * Charity public key
   */
  struct DONAU_CharityPublicKeyP charity_pub;

  /**
   * Charity name
   */
  char *charity_name;

  /**
   * Charity url
   */
  char *charity_url;

  /**
   * Charity yearly donation limit
   */
  struct TALER_Amount max_per_year;

  /**
   * Charity donations received in the current year
   */
  struct TALER_Amount receipts_to_date;

  /**
   * Current year
   */
  uint32_t current_year;

};

/**
 * Meta data about issued receipts of a request.
 */
struct DONAUDB_IssuedReceiptsMetaData
{
  /**
   * Charity id
   */
  uint64_t charity_id;

  /**
   * total issued amount of the receipts
   */
  struct TALER_Amount amount;

  /**
   * number of signatures
   */
  size_t num_sig;

  /**
   * Array of blinded signatures
   */
  struct DONAU_BlindedDonationUnitSignature *blinded_sigs;

};

/**
 * @brief All information about a donation unit key.
 */
struct DONAUDB_DonationUnitKey
{
  /**
   * The private key of the donation unit.
   * Will contain a NULL if the private
   * key is not available.
   */
  struct DONAU_DonationUnitPrivateKey donation_unit_priv;

  /**
   * Decoded donation unit public key.
   */
  struct DONAU_DonationUnitPublicKey donation_unit_pub;

};


/**
 * Connect to the donau database.
 *
 * @param cfg configuration to use
 * @return NULL on failure
 */
struct DONAUDB_PostgresContext *
DONAUDB_connect (
  const struct GNUNET_CONFIGURATION_Handle *cfg);


/**
 * Connect to the donau database for administration.
 * Disables the check that the database schema is current.
 *
 * @param cfg configuration to use
 * @return NULL on failure
 */
struct DONAUDB_PostgresContext *
DONAUDB_connect_admin (
  const struct GNUNET_CONFIGURATION_Handle *cfg);


/**
 * Disconnect from the donau database.
 *
 * @param pg context to disconnect and free
 */
void
DONAUDB_disconnect (struct DONAUDB_PostgresContext *pg);

#endif
