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

-- Everything in one big transaction
BEGIN;

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

CREATE SCHEMA taler_mailbox;
COMMENT ON SCHEMA taler_mailbox IS 'taler-mailbox data';

SET search_path TO taler_mailbox;

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

CREATE TABLE IF NOT EXISTS inbox_entries
  (serial BIGSERIAL PRIMARY KEY
  ,hashed_signing_key BYTEA
  ,body BYTEA NOT NULL
  );
COMMENT ON TABLE inbox_entries
 IS 'Mailbox entries (messages)';
COMMENT ON COLUMN inbox_entries.hashed_signing_key
 IS 'The hashed signing key of the mailbox owner';

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

CREATE TABLE IF NOT EXISTS mailbox_metadata
  (serial BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY
  ,hashed_signing_key BYTEA NOT NULL
  ,signing_key BYTEA NOT NULL
  ,signing_key_type BYTEA NOT NULL
  ,encryption_key BYTEA NOT NULL
  ,encryption_key_type BYTEA NOT NULL
  ,expiration INT8 NOT NULL
  ,info BYTEA NOT NULL
  );
COMMENT ON TABLE mailbox_metadata
 IS 'Mailbox configurations / instances';

CREATE TABLE IF NOT EXISTS pending_mailbox_registrations
  (serial BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY
  ,created_at INT8 NOT NULL
  ,hashed_signing_key BYTEA NOT NULL
  ,order_id BYTEA NOT NULL
  ,registration_duration INT8 NOT NULL
  );
COMMENT ON TABLE pending_mailbox_registrations
 IS 'Pending mailbox registrations';


-- Complete transaction
COMMIT;
