-- ================================================
-- Migration: Session attendance lock
-- Date: 2026-07-22
-- A Coordinator can lock a session's attendance once it has been reviewed and
-- finalized. While locked, only a Coordinator-role actor may continue to edit
-- that session's attendance — RM, Admin, QR self/manual check-in, and the Zoom
-- webhook auto-attendance path are all blocked.
-- Safe to re-run (IF NOT EXISTS).
-- ================================================

ALTER TABLE program_session
  ADD COLUMN IF NOT EXISTS is_attendance_locked BOOLEAN NOT NULL DEFAULT FALSE;

ALTER TABLE program_session
  ADD COLUMN IF NOT EXISTS attendance_locked_by INT NULL;

ALTER TABLE program_session
  ADD COLUMN IF NOT EXISTS attendance_locked_at TIMESTAMPTZ NULL;

COMMENT ON COLUMN program_session.is_attendance_locked IS
  'True once a Coordinator has locked this session''s attendance; only Coordinator-role actors may edit attendance while true.';
COMMENT ON COLUMN program_session.attendance_locked_by IS
  'User id of the Coordinator who last locked this session''s attendance. Null while unlocked.';
COMMENT ON COLUMN program_session.attendance_locked_at IS
  'When this session''s attendance was last locked. Null while unlocked.';

-- Verification
SELECT column_name, data_type, is_nullable, column_default
FROM information_schema.columns
WHERE table_name = 'program_session'
  AND column_name IN ('is_attendance_locked', 'attendance_locked_by', 'attendance_locked_at');
