-- Activity tracking for recent activity feed CREATE TABLE IF NOT EXISTS activity_log ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), org_id UUID NOT NULL REFERENCES organizations(id) ON DELETE CASCADE, user_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE, action TEXT NOT NULL, -- 'create', 'update', 'delete', 'move', 'assign', etc. entity_type TEXT NOT NULL, -- 'document', 'kanban_card', 'kanban_board', 'member', etc. entity_id UUID, entity_name TEXT, metadata JSONB DEFAULT '{}', created_at TIMESTAMPTZ DEFAULT now() ); -- Enable RLS ALTER TABLE activity_log ENABLE ROW LEVEL SECURITY; -- Users can view activity for orgs they belong to CREATE POLICY "Users can view org activity" ON activity_log FOR SELECT USING ( EXISTS ( SELECT 1 FROM org_members m WHERE m.org_id = activity_log.org_id AND m.user_id = auth.uid() ) ); -- Users can insert their own activity CREATE POLICY "Users can insert own activity" ON activity_log FOR INSERT WITH CHECK (user_id = auth.uid()); -- Indexes for faster queries CREATE INDEX IF NOT EXISTS idx_activity_log_org ON activity_log(org_id); CREATE INDEX IF NOT EXISTS idx_activity_log_created ON activity_log(created_at DESC); CREATE INDEX IF NOT EXISTS idx_activity_log_entity ON activity_log(entity_type, entity_id); -- User preferences table for themes and settings CREATE TABLE IF NOT EXISTS user_preferences ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), user_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE UNIQUE, theme TEXT DEFAULT 'dark' CHECK (theme IN ('dark', 'light', 'system')), accent_color TEXT DEFAULT '#00A3E0', use_org_theme BOOLEAN DEFAULT true, -- Whether org theme overrides user theme sidebar_collapsed BOOLEAN DEFAULT false, created_at TIMESTAMPTZ DEFAULT now(), updated_at TIMESTAMPTZ DEFAULT now() ); -- Enable RLS ALTER TABLE user_preferences ENABLE ROW LEVEL SECURITY; -- Users can manage their own preferences CREATE POLICY "Users can manage own preferences" ON user_preferences FOR ALL USING (user_id = auth.uid()); -- Index CREATE INDEX IF NOT EXISTS idx_user_preferences_user ON user_preferences(user_id);