-- Users

CREATE TABLE users (
  id                  BIGINT IDENTITY (1,1) PRIMARY KEY,
  org_id              BIGINT NOT NULL REFERENCES orgs (id),
  name                NVARCHAR(255) NOT NULL,
  email               NVARCHAR(128),
  admin               BIT NOT NULL DEFAULT 0,
  active              BIT NOT NULL DEFAULT 1,

  password_hash       VARCHAR(255),
  password_changed_at DATETIMEOFFSET,

  created_at          DATETIMEOFFSET NOT NULL DEFAULT SYSDATETIMEOFFSET(),
  logged_in_at        DATETIMEOFFSET
);

CREATE INDEX users_org_id_ix
  ON users (org_id);
CREATE UNIQUE INDEX users_email_uniq
  ON users (email)
  WHERE email IS NOT NULL;
