-- Verification script to check HTML entity cleaning migration results
-- Run this after the migration to verify it worked correctly

-- Check if the cleaning function exists
SELECT 
    'clean_html_entities function: ' || 
    CASE WHEN EXISTS (
        SELECT 1 FROM pg_proc p 
        JOIN pg_namespace n ON p.pronamespace = n.oid 
        WHERE p.proname = 'clean_html_entities' AND n.nspname = 'public'
    ) THEN '✓ EXISTS' ELSE '✗ MISSING' END as function_status;

-- Count records that still have HTML entities (should be 0 after migration)
SELECT 
    'Records with HTML tags in bulletins: ' || COUNT(*) as bulletin_html_tags
FROM bulletins 
WHERE 
    title ~ '<[^>]*>' OR 
    sabbath_school ~ '<[^>]*>' OR 
    divine_worship ~ '<[^>]*>' OR 
    scripture_reading ~ '<[^>]*>' OR 
    sunset ~ '<[^>]*>';

SELECT 
    'Records with HTML entities in bulletins: ' || COUNT(*) as bulletin_html_entities  
FROM bulletins 
WHERE 
    title ~ '&(nbsp|amp|lt|gt|quot|#39);' OR 
    sabbath_school ~ '&(nbsp|amp|lt|gt|quot|#39);' OR 
    divine_worship ~ '&(nbsp|amp|lt|gt|quot|#39);' OR 
    scripture_reading ~ '&(nbsp|amp|lt|gt|quot|#39);' OR 
    sunset ~ '&(nbsp|amp|lt|gt|quot|#39);';

SELECT 
    'Records with HTML tags in events: ' || COUNT(*) as event_html_tags
FROM events 
WHERE 
    title ~ '<[^>]*>' OR 
    description ~ '<[^>]*>' OR 
    location ~ '<[^>]*>' OR 
    location_url ~ '<[^>]*>' OR 
    approved_from ~ '<[^>]*>';

SELECT 
    'Records with HTML entities in events: ' || COUNT(*) as event_html_entities
FROM events 
WHERE 
    title ~ '&(nbsp|amp|lt|gt|quot|#39);' OR 
    description ~ '&(nbsp|amp|lt|gt|quot|#39);' OR 
    location ~ '&(nbsp|amp|lt|gt|quot|#39);' OR 
    location_url ~ '&(nbsp|amp|lt|gt|quot|#39);' OR 
    approved_from ~ '&(nbsp|amp|lt|gt|quot|#39);';

-- Show some sample cleaned data
SELECT 
    'Sample bulletin titles after cleaning:' as sample_data;
    
SELECT 
    SUBSTRING(title, 1, 50) || (CASE WHEN LENGTH(title) > 50 THEN '...' ELSE '' END) as cleaned_titles
FROM bulletins 
WHERE title IS NOT NULL 
ORDER BY updated_at DESC 
LIMIT 5;

SELECT 
    'Sample event descriptions after cleaning:' as sample_data;

SELECT 
    SUBSTRING(description, 1, 80) || (CASE WHEN LENGTH(description) > 80 THEN '...' ELSE '' END) as cleaned_descriptions
FROM events 
WHERE description IS NOT NULL 
ORDER BY updated_at DESC 
LIMIT 3;

-- Check when records were last updated (should show recent timestamps if migration ran)
SELECT 
    'Recently updated bulletins: ' || COUNT(*) || ' (updated in last hour)' as recent_bulletins
FROM bulletins 
WHERE updated_at > NOW() - INTERVAL '1 hour';

SELECT 
    'Recently updated events: ' || COUNT(*) || ' (updated in last hour)' as recent_events  
FROM events 
WHERE updated_at > NOW() - INTERVAL '1 hour';

-- Summary
SELECT '===============================================' as summary;
SELECT 'MIGRATION VERIFICATION COMPLETE' as summary;
SELECT 'If all HTML tag/entity counts above are 0, the migration was successful!' as summary;
SELECT '===============================================' as summary;