--
-- 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/>
--

CREATE TABLE IF NOT EXISTS auditor_aml_holds
(
    row_id BIGINT GENERATED BY DEFAULT AS IDENTITY UNIQUE PRIMARY KEY,
    wtid BYTEA NOT NULL UNIQUE CHECK (LENGTH(wtid)=32),
    wire_target_h_payto BYTEA NOT NULL CHECK (LENGTH(wire_target_h_payto)=32),
    account TEXT NOT NULL,
    amount taler_amount NOT NULL,
    deferral_reason INT4 NOT NULL,
    legitimization_measure_serial_id BIGINT NOT NULL DEFAULT (0),
    suppressed BOOLEAN NOT NULL DEFAULT FALSE,
    creation_date INT8 NOT NULL
      DEFAULT (EXTRACT(EPOCH FROM NOW()) * 1000 * 1000)::BIGINT
);

-- Deliberately not garbage collected: a row here is not a finding that ages
-- out, it is money the exchange is still sitting on.  It disappears when the
-- wire transfer is executed, and not before.

COMMENT ON TABLE auditor_aml_holds
  IS 'Wire transfers the exchange aggregated but did not execute: it wrote the aggregation_tracking rows and marked the deposits done, but there is no wire_out row. One row per wire transfer identifier, deleted once the transfer is made.';

COMMENT ON COLUMN auditor_aml_holds.row_id
  IS 'Unique identifier of the report in the auditor database';
COMMENT ON COLUMN auditor_aml_holds.wtid
  IS 'Wire transfer identifier the deposits were aggregated into';
COMMENT ON COLUMN auditor_aml_holds.wire_target_h_payto
  IS 'Hash of the bank account the transfer should be made to';
COMMENT ON COLUMN auditor_aml_holds.account
  IS 'Payto URI of that bank account, for the benefit of the reader';
COMMENT ON COLUMN auditor_aml_holds.amount
  IS 'What the exchange still owes on this transfer: deposits minus refunds minus deposit fees, before the wire fee';
COMMENT ON COLUMN auditor_aml_holds.deferral_reason
  IS 'Why the exchange says it has not made the transfer, taken from its aggregation_deferrals row: 1 for an aggregate too small to cover the wire fee, 2 for an open KYC/AML requirement, 0 if the exchange gave no reason at all';
COMMENT ON COLUMN auditor_aml_holds.legitimization_measure_serial_id
  IS 'The measure the exchange named as blocking the payout, or 0 if it named none';
COMMENT ON COLUMN auditor_aml_holds.suppressed
  IS 'True if the report was suppressed by an administrator; the amount still counts towards the balances, as the funds are held either way';
COMMENT ON COLUMN auditor_aml_holds.creation_date
  IS 'When the auditor first saw this transfer being held';
