-- Migration 005: Add consent management fields to groups table
-- Date: 2025-11-09
-- Description: Adds fields for workshop display consent, consent timestamp, and management token

-- Add consent-related columns to groups table
ALTER TABLE groups ADD COLUMN display_in_workshop BOOLEAN NOT NULL DEFAULT 0;
ALTER TABLE groups ADD COLUMN consent_timestamp DATETIME;
ALTER TABLE groups ADD COLUMN management_token TEXT; -- For Phase 2: Self-service portal

-- Create indexes for better query performance
CREATE INDEX IF NOT EXISTS idx_groups_display_consent ON groups(display_in_workshop);
CREATE UNIQUE INDEX IF NOT EXISTS idx_groups_management_token ON groups(management_token) WHERE management_token IS NOT NULL;

-- Update existing groups to default values (retroactively grant consent for approved groups)
-- This assumes existing groups have been approved and displayed
UPDATE groups SET display_in_workshop = 1, consent_timestamp = created_at WHERE id > 0;
