CREATE TABLE config (
  ConfigID    INTEGER NOT NULL PRIMARY KEY, -- Always 1.
  Password    BLOB    NOT NULL  -- bcrypt password for web interface
) WITHOUT ROWID;

CREATE TABLE sessions (
  SessionID  TEXT    NOT NULL PRIMARY KEY,
  CSRF       TEXT    NOT NULL,
  SignedIn   INTEGER NOT NULL,
  CreatedAt  INTEGER NOT NULL,
  LastSeenAt INTEGER NOT NULL
) WITHOUT ROWID;

CREATE INDEX sessions_last_seen_index ON sessions(LastSeenAt);

CREATE TABLE networks (
  NetworkID   INTEGER NOT NULL PRIMARY KEY,
  Name        TEXT    NOT NULL UNIQUE, -- Network/interface name.
  Network     BLOB    NOT NULL UNIQUE  -- Network (/24), example 10.51.50.0
) WITHOUT ROWID;

CREATE TABLE peers (
  NetworkID  INTEGER NOT NULL,
  PeerIP     INTEGER NOT NULL,             -- Final byte of IP.
  Version    INTEGER NOT NULL,             -- Changes when updated.
  APIKey     TEXT    NOT NULL UNIQUE,      -- Peer's secret API key.
  Name       TEXT    NOT NULL UNIQUE,      -- For humans.
  PublicIP   BLOB    NOT NULL,
  Port       INTEGER NOT NULL,
  Relay      INTEGER NOT NULL DEFAULT 0,   -- Boolean if peer will forward packets. Must also have public address.
  PubKey     BLOB    NOT NULL,
  PubSignKey BLOB    NOT NULL,
  PRIMARY KEY(NetworkID, PeerIP)
) WITHOUT ROWID;
