--
-- 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 merchant-0030.sql
-- @brief Add field for long-poll signalling to taler-merchant-kyccheck
-- @author Christian Grothoff

BEGIN;

-- Check patch versioning is in place.
SELECT _v.register_patch('merchant-0030', NULL, NULL);

SET search_path TO merchant;

CREATE TABLE IF NOT EXISTS merchant_unclaim_signatures
  (unclaim_serial BIGINT GENERATED BY DEFAULT AS IDENTITY UNIQUE
  ,h_contract_terms BYTEA NOT NULL CHECK (LENGTH(h_contract_terms)=64)
  ,unclaim_sig BYTEA PRIMARY KEY CHECK (LENGTH(unclaim_sig)=64)
  ,expiration_time INT8 NOT NULL
);

COMMENT ON TABLE merchant_unclaim_signatures
 IS 'Here we store proofs of wallets unclaiming a claim to a contract. Right now not really used, but theoretically legally important as this shows that the wallet was the one abandoing its rights to the proposal.';
COMMENT ON COLUMN merchant_unclaim_signatures.h_contract_terms
 IS 'Hash over the contract terms of the unclaimed contract';
COMMENT ON COLUMN merchant_unclaim_signatures.unclaim_sig
 IS 'Signature of purpose CONTRACT_UNCLAIM';
COMMENT ON COLUMN merchant_unclaim_signatures.expiration_time
 IS 'Payment deadline of the original contract, at this point we should be OK to delete the unclaim signature via garbage collection.';


CREATE INDEX merchant_unclaim_signatures_by_expiration
  ON merchant_unclaim_signatures
    (expiration_time);
COMMENT ON INDEX merchant_unclaim_signatures_by_expiration
 IS 'For garbage collection of unclaim signatrues after payment deadlines have been passed.';


ALTER TABLE merchant_exchange_keys
  ADD COLUMN exchange_http_status INT4 DEFAULT(0)
 ,ADD COLUMN exchange_ec_code INT4 DEFAULT(0)
 ,ALTER COLUMN keys_json DROP NOT NULL;

COMMENT ON COLUMN merchant_exchange_keys.exchange_http_status
  IS 'Last HTTP status code from trying to fetch /keys; 0 for timeout or not attempted yet';
COMMENT ON COLUMN merchant_exchange_keys.exchange_ec_code
  IS 'Error code from our last attempt to fetch /keys; 0 for no error in combination with an HTTP status of 200';

COMMIT;
