-- Migration 006: Create social media platform configuration and consent tables
-- Date: 2025-11-09
-- Description: Creates extensible social media platform management and per-group consent tracking

-- ============================================================================
-- Table: social_media_platforms
-- Purpose: Configurable list of social media platforms for consent management
-- ============================================================================
CREATE TABLE IF NOT EXISTS social_media_platforms (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    platform_name TEXT UNIQUE NOT NULL,  -- Internal identifier (e.g., 'facebook', 'instagram', 'tiktok')
    display_name TEXT NOT NULL,           -- User-facing name (e.g., 'Facebook', 'Instagram', 'TikTok')
    icon_name TEXT,                       -- Material-UI Icon name for frontend display
    is_active BOOLEAN DEFAULT 1,          -- Enable/disable platform without deletion
    sort_order INTEGER DEFAULT 0,         -- Display order in UI
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

-- ============================================================================
-- Table: group_social_media_consents
-- Purpose: Track user consent for each group and social media platform
-- ============================================================================
CREATE TABLE IF NOT EXISTS group_social_media_consents (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    group_id TEXT NOT NULL,
    platform_id INTEGER NOT NULL,
    consented BOOLEAN NOT NULL DEFAULT 0,
    consent_timestamp DATETIME NOT NULL,
    revoked BOOLEAN DEFAULT 0,            -- For Phase 2: Consent revocation tracking
    revoked_timestamp DATETIME,           -- When consent was revoked (Phase 2)
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    
    -- Foreign key constraints
    FOREIGN KEY (group_id) REFERENCES groups(group_id) ON DELETE CASCADE,
    FOREIGN KEY (platform_id) REFERENCES social_media_platforms(id) ON DELETE CASCADE,
    
    -- Ensure each platform can only have one consent entry per group
    UNIQUE(group_id, platform_id)
);

-- ============================================================================
-- Indexes for query performance
-- ============================================================================
CREATE INDEX IF NOT EXISTS idx_consents_group_id ON group_social_media_consents(group_id);
CREATE INDEX IF NOT EXISTS idx_consents_platform_id ON group_social_media_consents(platform_id);
CREATE INDEX IF NOT EXISTS idx_consents_consented ON group_social_media_consents(consented);

-- ============================================================================
-- Seed data: Insert default social media platforms
-- ============================================================================
INSERT INTO social_media_platforms (platform_name, display_name, icon_name, sort_order) VALUES ('facebook', 'Facebook', 'Facebook', 1);
INSERT INTO social_media_platforms (platform_name, display_name, icon_name, sort_order) VALUES ('instagram', 'Instagram', 'Instagram', 2);
INSERT INTO social_media_platforms (platform_name, display_name, icon_name, sort_order) VALUES ('tiktok', 'TikTok', 'MusicNote', 3);
