--
-- 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 taler-directory-0001.sql
-- @brief database schema for TalDir
-- @author Christian Grothoff
-- @author Martin Schanzenbach

-- Everything in one big transaction
BEGIN;

-- Check patch versioning is in place.
SELECT _v.register_patch('taler-directory-0001', NULL, NULL);

CREATE SCHEMA taler_directory;
COMMENT ON SCHEMA taler_directory IS 'taler-directory data';

SET search_path TO taler_directory;

---------------- Entries ---------------------------

CREATE TABLE IF NOT EXISTS entries
  (entry_serial BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY
  ,hs_alias BYTEA NOT NULL
  ,created_at INT8 NOT NULL
  ,target_uri BYTEA NOT NULL 
  ,duration INT8 NOT NULL
  ,UNIQUE (hs_alias)
  );
COMMENT ON TABLE entries
 IS 'An entry in the directory';
COMMENT ON COLUMN entries.hs_alias
 IS 'The salted and hashed alias';

---------------- Validations ---------------------------

CREATE TABLE IF NOT EXISTS validations
  (validation_serial BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY
  ,created_at INT8 NOT NULL
  ,h_alias BYTEA NOT NULL
  ,duration INT8 NOT NULL
  ,target_uri BYTEA NOT NULL
  ,challenge BYTEA NOT NULL
  ,challenge_sent BOOLEAN NOT NULL
  ,requires_payment BOOLEAN NOT NULL
  ,solution_attempt_count INT NOT NULL
  ,last_solution_timeframe_start INT8 NOT NULL
  ,order_id BYTEA
  ,validator_name BYTEA NOT NULL
  );
COMMENT ON TABLE validations
 IS 'A pending validation in the directory';
COMMENT ON COLUMN validations.h_alias
 IS 'The hashed alias';


-- Complete transaction
COMMIT;
