CREATE TABLE IF NOT EXISTS "users" (
  id BIGSERIAL PRIMARY KEY,
  username VARCHAR(20) UNIQUE NOT NULL,
  verified BOOLEAN DEFAULT FALSE,
  banned BOOLEAN DEFAULT FALSE,
  registration_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

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) UNIQUE,
  password_hash VARCHAR(512) NOT NULL,
  totp_encrypted VARCHAR(512),
  email_2fa_enabled BOOLEAN DEFAULT FALSE,
  password_change_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  password_times_changed INTEGER DEFAULT 0
);

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 DEFAULT CURRENT_TIMESTAMP + INTERVAL '10 minutes',
  used BOOLEAN DEFAULT FALSE,
  deleted BOOLEAN DEFAULT FALSE
);

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 DEFAULT CURRENT_TIMESTAMP,
  last_seen_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  terminated BOOLEAN DEFAULT FALSE
);

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
);
