-- TODO: login information must exist too
CREATE TABLE IF NOT EXISTS "users" (
  id BIGSERIAL PRIMARY KEY,
  username VARCHAR(20) UNIQUE NOT NULL,
  verified BOOLEAN,
  banned BOOLEAN,
  registration_date TIMESTAMP NOT NULL
);

CREATE TABLE IF NOT EXISTS "login_informations"  (
  id BIGSERIAL PRIMARY KEY,
  user_id BIGINT UNIQUE NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  email VARCHAR(75),
  password_hash VARCHAR(512) NOT NULL,
  totp_encrypted VARCHAR(512),
  email_2fa_enabled BOOLEAN,
  password_change_time TIMESTAMP
);

CREATE TABLE IF NOT EXISTS "confirmation_codes" (
  id BIGSERIAL PRIMARY KEY,
  user_id BIGINT UNIQUE NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  type INTEGER NOT NULL,
  code VARCHAR(36) NOT NULL,
  expires_at TIMESTAMP NOT NULL,
  used BOOLEAN,
  deleted BOOLEAN
);

CREATE TABLE IF NOT EXISTS "sessions" (
  id BIGSERIAL PRIMARY KEY,
  user_id BIGINT UNIQUE NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  guid VARCHAR(36) NOT NULL,
  name VARCHAR(100),
  platform VARCHAR(32),
  latest_ip VARCHAR(16),
  login_time TIMESTAMP NOT NULL,
  last_seen_date TIMESTAMP NOT NULL,
  terminated BOOLEAN
);

CREATE TABLE IF NOT EXISTS "profiles" (
  id BIGSERIAL PRIMARY KEY,
  user_id BIGINT UNIQUE NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  name VARCHAR(75) NOT NULL,
  avatar_url VARCHAR(255),
  birthday TIMESTAMP
);
