--
-- 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/>
--
-- @author Christian Grothoff

DROP PROCEDURE IF EXISTS auditor_do_gc_auditor;
CREATE OR REPLACE PROCEDURE auditor_do_gc_auditor(
  IN cutoff INT8)
LANGUAGE plpgsql
AS $$
BEGIN

  -- Standard cases
  DELETE FROM auditor_amount_arithmetic_inconsistency
    WHERE suppressed
      AND creation_date < cutoff;
  DELETE FROM auditor_bad_sig_losses
    WHERE suppressed
      AND creation_date < cutoff;
  DELETE FROM auditor_closure_lags
    WHERE suppressed
      AND creation_date < cutoff;
  DELETE FROM auditor_coin_inconsistency
    WHERE suppressed
      AND creation_date < cutoff;
  DELETE FROM auditor_denomination_key_validity_withdraw_inconsistency
    WHERE suppressed
      AND creation_date < cutoff;
  DELETE FROM auditor_denominations_without_sigs
    WHERE suppressed
      AND creation_date < cutoff;
  DELETE FROM auditor_emergency
    WHERE suppressed
      AND creation_date < cutoff;
  DELETE FROM auditor_emergency_by_count
    WHERE suppressed
      AND creation_date < cutoff;
  DELETE FROM auditor_fee_time_inconsistency
    WHERE suppressed
      AND creation_date < cutoff;
  DELETE FROM auditor_misattribution_in_inconsistency
    WHERE suppressed
      AND creation_date < cutoff;
  DELETE FROM auditor_purse_not_closed_inconsistencies
    WHERE suppressed
      AND creation_date < cutoff;
  DELETE FROM auditor_refreshes_hanging
    WHERE suppressed
      AND creation_date < cutoff;
  DELETE FROM auditor_reserve_balance_insufficient_inconsistency
    WHERE suppressed
      AND creation_date < cutoff;
  DELETE FROM auditor_reserve_balance_summary_wrong_inconsistency
    WHERE suppressed
      AND creation_date < cutoff;
  DELETE FROM auditor_kycauth_in_inconsistency
    WHERE suppressed
      AND creation_date < cutoff;
  DELETE FROM auditor_reserve_in_inconsistency
    WHERE suppressed
      AND creation_date < cutoff;
  DELETE FROM auditor_reserve_not_closed_inconsistency
    WHERE suppressed
      AND creation_date < cutoff;
  DELETE FROM auditor_row_inconsistency
    WHERE suppressed
      AND creation_date < cutoff;
  DELETE FROM auditor_row_minor_inconsistencies
    WHERE suppressed
      AND creation_date < cutoff;
  DELETE FROM auditor_wire_format_inconsistency
    WHERE suppressed
      AND creation_date < cutoff;
  DELETE FROM auditor_early_aggregations
    WHERE suppressed
      AND creation_date < cutoff;

  -- special cases
  DELETE FROM auditor_deposit_confirmations
    WHERE suppressed
      AND exchange_timestamp < cutoff;
  DELETE FROM auditor_exchange_signkeys
    WHERE ep_expire_legal<(EXTRACT(EPOCH FROM NOW()) * 1000 * 1000)::BIGINT
      AND ep_expire_sign < cutoff;
  DELETE FROM auditor_purses
    WHERE expiration_date < cutoff;
  DELETE FROM auditor_reserves
    WHERE expiration_date < cutoff;
  DELETE FROM auditor_pending_deposits
    WHERE suppressed
      AND creation_date < cutoff
      AND deadline < cutoff;

END $$;

COMMENT ON PROCEDURE auditor_do_gc_auditor(INT8)
  IS 'Delete data from the auditor database that is no longer needed.';
