-- Migration 0002: relay support
-- Adds viewer_token to sessions and enrollment_tokens table

ALTER TABLE sessions
  ADD COLUMN IF NOT EXISTS viewer_token UUID DEFAULT gen_random_uuid();

CREATE TABLE IF NOT EXISTS enrollment_tokens (
  id           UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  token        UUID UNIQUE NOT NULL DEFAULT gen_random_uuid(),
  label        TEXT,
  created_by   UUID REFERENCES users(id) ON DELETE SET NULL,
  created_at   TIMESTAMPTZ NOT NULL DEFAULT now(),
  expires_at   TIMESTAMPTZ,
  used_count   INTEGER NOT NULL DEFAULT 0,
  max_uses     INTEGER,
  revoked_at   TIMESTAMPTZ
);

CREATE INDEX IF NOT EXISTS idx_enrollment_tokens_token ON enrollment_tokens(token);
