-- Social data import and confirmed profile insights.

CREATE TABLE IF NOT EXISTS t_social_content_item (
    id VARCHAR(64) PRIMARY KEY COMMENT 'UUID primary key',
    user_id VARCHAR(64) NOT NULL COMMENT 'Owner user id',
    platform VARCHAR(32) NOT NULL COMMENT 'Source platform: xiaohongshu/weibo/wechat/other',
    source_type VARCHAR(32) NOT NULL COMMENT 'manual/link/screenshot',
    source_url VARCHAR(1000) COMMENT 'Original source url',
    title VARCHAR(200) COMMENT 'Imported content title',
    content MEDIUMTEXT COMMENT 'User supplied or extracted text',
    image_urls JSON COMMENT 'Related image urls',
    published_at DATETIME COMMENT 'Original publish time',
    import_status VARCHAR(32) DEFAULT 'imported' COMMENT 'imported/ocr_pending/ocr_failed',
    approved_for_ai TINYINT DEFAULT 0 COMMENT 'Whether user allows AI analysis',
    content_hash VARCHAR(64) COMMENT 'Content dedupe hash',
    raw_metadata JSON COMMENT 'Import metadata',
    deleted_at DATETIME COMMENT 'Soft delete time',
    create_by VARCHAR(64) COMMENT 'Creator id',
    create_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT 'Create time',
    update_by VARCHAR(64) COMMENT 'Updater id',
    update_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Update time',
    is_deleted TINYINT DEFAULT 0 COMMENT 'Soft delete flag',
    remarks VARCHAR(500) COMMENT 'Remarks',
    INDEX idx_social_content_user_time (user_id, create_time),
    INDEX idx_social_content_platform (platform),
    INDEX idx_social_content_hash (user_id, content_hash),
    INDEX idx_social_content_deleted (is_deleted)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = 'Social content imported by users';

CREATE TABLE IF NOT EXISTS t_social_profile_insight (
    id VARCHAR(64) PRIMARY KEY COMMENT 'UUID primary key',
    user_id VARCHAR(64) NOT NULL COMMENT 'Owner user id',
    source_item_id VARCHAR(64) COMMENT 'Source social content id',
    insight_type VARCHAR(32) NOT NULL COMMENT 'value/interest/emotion_pattern/script_theme',
    label VARCHAR(100) NOT NULL COMMENT 'Insight label',
    summary VARCHAR(1000) COMMENT 'Insight summary',
    evidence_excerpt VARCHAR(500) COMMENT 'Short supporting excerpt',
    confidence DECIMAL(5,4) DEFAULT 0 COMMENT 'Extraction confidence',
    status VARCHAR(32) DEFAULT 'suggested' COMMENT 'suggested/confirmed/rejected/deleted',
    user_edited TINYINT DEFAULT 0 COMMENT 'Whether user edited label or summary',
    confirmed_at DATETIME COMMENT 'Confirmed time',
    deleted_at DATETIME COMMENT 'Soft delete time',
    create_by VARCHAR(64) COMMENT 'Creator id',
    create_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT 'Create time',
    update_by VARCHAR(64) COMMENT 'Updater id',
    update_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Update time',
    is_deleted TINYINT DEFAULT 0 COMMENT 'Soft delete flag',
    remarks VARCHAR(500) COMMENT 'Remarks',
    INDEX idx_social_insight_user_status (user_id, status),
    INDEX idx_social_insight_source (source_item_id),
    INDEX idx_social_insight_deleted (is_deleted)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = 'User-reviewable social profile insights';

CREATE TABLE IF NOT EXISTS t_user_consent_log (
    id VARCHAR(64) PRIMARY KEY COMMENT 'UUID primary key',
    user_id VARCHAR(64) NOT NULL COMMENT 'Owner user id',
    platform VARCHAR(32) COMMENT 'Related social platform',
    consent_type VARCHAR(64) NOT NULL COMMENT 'Consent type',
    consent_version VARCHAR(32) COMMENT 'Consent copy version',
    scope VARCHAR(200) COMMENT 'Authorized data scope',
    purpose VARCHAR(500) COMMENT 'Authorized usage purpose',
    status VARCHAR(32) COMMENT 'granted/revoked',
    granted_at DATETIME COMMENT 'Granted time',
    revoked_at DATETIME COMMENT 'Revoked time',
    client_ip VARCHAR(64) COMMENT 'Client ip',
    device_info JSON COMMENT 'Device metadata',
    create_by VARCHAR(64) COMMENT 'Creator id',
    create_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT 'Create time',
    update_by VARCHAR(64) COMMENT 'Updater id',
    update_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Update time',
    is_deleted TINYINT DEFAULT 0 COMMENT 'Soft delete flag',
    remarks VARCHAR(500) COMMENT 'Remarks',
    INDEX idx_user_consent_user_time (user_id, create_time),
    INDEX idx_user_consent_type (consent_type)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = 'User consent audit log';
