CREATE TABLE IF NOT EXISTS key_packages (
    id           INTEGER PRIMARY KEY AUTOINCREMENT,
    identity_key BLOB NOT NULL,
    package_data BLOB NOT NULL,
    created_at   INTEGER DEFAULT (strftime('%s','now'))
);

CREATE TABLE IF NOT EXISTS deliveries (
    id            INTEGER PRIMARY KEY AUTOINCREMENT,
    recipient_key BLOB NOT NULL,
    channel_id    BLOB NOT NULL DEFAULT X'',
    payload       BLOB NOT NULL,
    created_at    INTEGER DEFAULT (strftime('%s','now'))
);

CREATE TABLE IF NOT EXISTS hybrid_keys (
    identity_key      BLOB PRIMARY KEY,
    hybrid_public_key BLOB NOT NULL
);

CREATE INDEX IF NOT EXISTS idx_kp_identity
    ON key_packages(identity_key);

CREATE INDEX IF NOT EXISTS idx_del_recipient_channel
    ON deliveries(recipient_key, channel_id);

CREATE TABLE IF NOT EXISTS server_setup (
    id         INTEGER PRIMARY KEY CHECK (id = 1),
    setup_data BLOB NOT NULL
);

CREATE TABLE IF NOT EXISTS users (
    username      TEXT PRIMARY KEY,
    opaque_record BLOB NOT NULL,
    created_at    INTEGER DEFAULT (strftime('%s','now'))
);

CREATE TABLE IF NOT EXISTS user_identity_keys (
    username     TEXT PRIMARY KEY,
    identity_key BLOB NOT NULL
);

CREATE TABLE IF NOT EXISTS endpoints (
    identity_key BLOB PRIMARY KEY,
    node_addr    BLOB NOT NULL,
    updated_at   INTEGER DEFAULT (strftime('%s','now'))
);
