-- Script to clean existing HTML tags from database content
-- Run this script to sanitize existing data in your database

-- Clean bulletins table
UPDATE bulletins SET 
    title = REGEXP_REPLACE(title, '<[^>]*>', '', 'g'),
    sabbath_school = REGEXP_REPLACE(COALESCE(sabbath_school, ''), '<[^>]*>', '', 'g'),
    divine_worship = REGEXP_REPLACE(COALESCE(divine_worship, ''), '<[^>]*>', '', 'g'),
    scripture_reading = REGEXP_REPLACE(COALESCE(scripture_reading, ''), '<[^>]*>', '', 'g'),
    sunset = REGEXP_REPLACE(COALESCE(sunset, ''), '<[^>]*>', '', 'g')
WHERE 
    title LIKE '%<%' OR 
    sabbath_school LIKE '%<%' OR 
    divine_worship LIKE '%<%' OR 
    scripture_reading LIKE '%<%' OR 
    sunset LIKE '%<%';

-- Clean events table
UPDATE events SET 
    title = REGEXP_REPLACE(title, '<[^>]*>', '', 'g'),
    description = REGEXP_REPLACE(description, '<[^>]*>', '', 'g'),
    location = REGEXP_REPLACE(location, '<[^>]*>', '', 'g'),
    location_url = REGEXP_REPLACE(COALESCE(location_url, ''), '<[^>]*>', '', 'g'),
    category = REGEXP_REPLACE(category, '<[^>]*>', '', 'g'),
    recurring_type = REGEXP_REPLACE(COALESCE(recurring_type, ''), '<[^>]*>', '', 'g')
WHERE 
    title LIKE '%<%' OR 
    description LIKE '%<%' OR 
    location LIKE '%<%' OR 
    location_url LIKE '%<%' OR 
    category LIKE '%<%' OR 
    recurring_type LIKE '%<%';

-- Clean pending_events table
UPDATE pending_events SET 
    title = REGEXP_REPLACE(title, '<[^>]*>', '', 'g'),
    description = REGEXP_REPLACE(description, '<[^>]*>', '', 'g'),
    location = REGEXP_REPLACE(location, '<[^>]*>', '', 'g'),
    location_url = REGEXP_REPLACE(COALESCE(location_url, ''), '<[^>]*>', '', 'g'),
    category = REGEXP_REPLACE(category, '<[^>]*>', '', 'g'),
    recurring_type = REGEXP_REPLACE(COALESCE(recurring_type, ''), '<[^>]*>', '', 'g'),
    bulletin_week = REGEXP_REPLACE(bulletin_week, '<[^>]*>', '', 'g'),
    submitter_email = REGEXP_REPLACE(COALESCE(submitter_email, ''), '<[^>]*>', '', 'g'),
    admin_notes = REGEXP_REPLACE(COALESCE(admin_notes, ''), '<[^>]*>', '', 'g')
WHERE 
    title LIKE '%<%' OR 
    description LIKE '%<%' OR 
    location LIKE '%<%' OR 
    location_url LIKE '%<%' OR 
    category LIKE '%<%' OR 
    recurring_type LIKE '%<%' OR 
    bulletin_week LIKE '%<%' OR 
    submitter_email LIKE '%<%' OR 
    admin_notes LIKE '%<%';

-- Clean contact_submissions table
UPDATE contact_submissions SET 
    first_name = REGEXP_REPLACE(first_name, '<[^>]*>', '', 'g'),
    last_name = REGEXP_REPLACE(last_name, '<[^>]*>', '', 'g'),
    email = REGEXP_REPLACE(email, '<[^>]*>', '', 'g'),
    phone = REGEXP_REPLACE(COALESCE(phone, ''), '<[^>]*>', '', 'g'),
    message = REGEXP_REPLACE(message, '<[^>]*>', '', 'g')
WHERE 
    first_name LIKE '%<%' OR 
    last_name LIKE '%<%' OR 
    email LIKE '%<%' OR 
    phone LIKE '%<%' OR 
    message LIKE '%<%';

-- Clean church_config table
UPDATE church_config SET 
    church_name = REGEXP_REPLACE(church_name, '<[^>]*>', '', 'g'),
    contact_email = REGEXP_REPLACE(contact_email, '<[^>]*>', '', 'g'),
    contact_phone = REGEXP_REPLACE(COALESCE(contact_phone, ''), '<[^>]*>', '', 'g'),
    church_address = REGEXP_REPLACE(church_address, '<[^>]*>', '', 'g'),
    po_box = REGEXP_REPLACE(COALESCE(po_box, ''), '<[^>]*>', '', 'g'),
    google_maps_url = REGEXP_REPLACE(COALESCE(google_maps_url, ''), '<[^>]*>', '', 'g'),
    about_text = REGEXP_REPLACE(about_text, '<[^>]*>', '', 'g')
WHERE 
    church_name LIKE '%<%' OR 
    contact_email LIKE '%<%' OR 
    contact_phone LIKE '%<%' OR 
    church_address LIKE '%<%' OR 
    po_box LIKE '%<%' OR 
    google_maps_url LIKE '%<%' OR 
    about_text LIKE '%<%';

-- Also clean HTML entities
UPDATE bulletins SET 
    title = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(title, '&amp;', '&'), '&lt;', '<'), '&gt;', '>'), '&quot;', '"'), '&#39;', ''''),
    sabbath_school = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(COALESCE(sabbath_school, ''), '&amp;', '&'), '&lt;', '<'), '&gt;', '>'), '&quot;', '"'), '&#39;', ''''),
    divine_worship = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(COALESCE(divine_worship, ''), '&amp;', '&'), '&lt;', '<'), '&gt;', '>'), '&quot;', '"'), '&#39;', ''''),
    scripture_reading = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(COALESCE(scripture_reading, ''), '&amp;', '&'), '&lt;', '<'), '&gt;', '>'), '&quot;', '"'), '&#39;', ''''),
    sunset = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(COALESCE(sunset, ''), '&amp;', '&'), '&lt;', '<'), '&gt;', '>'), '&quot;', '"'), '&#39;', '''');

SELECT 'Database cleaning completed. All HTML tags and entities have been removed from existing content.' as result;