CREATE TABLE roles (
  id TEXT PRIMARY KEY,
  key TEXT NOT NULL UNIQUE,
  name TEXT NOT NULL,
  summary TEXT NOT NULL,
  badge TEXT NOT NULL,
  tone TEXT NOT NULL,
  icon TEXT NOT NULL,
  permissions_json JSONB NOT NULL,
  created_at TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP
);

ALTER TABLE users ADD COLUMN role_id TEXT;
ALTER TABLE users
  ADD CONSTRAINT users_role_id_fkey
  FOREIGN KEY (role_id) REFERENCES roles(id)
  ON DELETE SET NULL
  ON UPDATE CASCADE;

CREATE INDEX users_role_id_idx ON users(role_id);
