CREATE extension IF NOT EXISTS pgcrypto;
SET TIMEZONE='UTC';

-- AuthN
--
--DROP TABLE IF EXISTS "authn";
CREATE TABLE IF NOT EXISTS "authn" (
    "id" TEXT PRIMARY KEY DEFAULT gen_random_uuid(),
    "ppid" TEXT NOT NULL,
    "email" TEXT NOT NULL,
    "verified" BOOL NOT NULL DEFAULT FALSE,
    "created_at" TIMESTAMP NOT NULL DEFAULT (now() AT TIME ZONE 'UTC'),
    "updated_at" TIMESTAMP NOT NULL DEFAULT (now() AT TIME ZONE 'UTC'),
    "deleted_at" TIMESTAMP NOT NULL DEFAULT ('epoch' AT TIME ZONE 'UTC')
);
--CREATE INDEX CONCURRENTLY IF NOT EXISTS "idx_slug" ON "authn" ("ppid");
CREATE INDEX IF NOT EXISTS "idx_ppid" ON "authn" ("ppid");
CREATE INDEX IF NOT EXISTS "idx_email" ON "authn" ("email");

-- Events
--
--DROP TABLE IF EXISTS "events";
CREATE TABLE IF NOT EXISTS "events" (
    "id" TEXT PRIMARY KEY DEFAULT gen_random_uuid(),
    "action" TEXT NOT NULL,
    "table" TEXT NOT NULL,
    "record" TEXT NOT NULL,
    "by" TEXT NOT NULL,
    "at" TIMESTAMP NOT NULL DEFAULT (now() AT TIME ZONE 'UTC'),
);
--CREATE INDEX CONCURRENTLY IF NOT EXISTS "idx_record" ON "events" ("record");
CREATE INDEX IF NOT EXISTS "idx_record" ON authn ("record");
CREATE INDEX IF NOT EXISTS "idx_by" ON authn ("by");
