-- Full Database Purge (Keep Users Only) - Disable Triggers
-- WARNING: This will delete ALL content data while preserving user accounts

-- Disable all triggers temporarily
SET session_replication_role = replica;

-- 1. Clear submission items first (depends on content_submissions)
DELETE FROM public.submission_items;

-- 2. Clear timeline events (has FK to content_submissions)
DELETE FROM public.entity_timeline_events;

-- 3. Clear specific submission type tables
DELETE FROM public.park_submissions;
DELETE FROM public.ride_submissions;
DELETE FROM public.company_submissions;
DELETE FROM public.ride_model_submissions;

-- 4. Clear main content submissions (now safe)
DELETE FROM public.content_submissions;

-- 5. Clear user-generated content
DELETE FROM public.reviews;
DELETE FROM public.reports;
DELETE FROM public.photos;

-- 6. Clear user lists
DELETE FROM public.list_items;
DELETE FROM public.user_top_lists;

-- 7. Clear user ride tracking
DELETE FROM public.user_ride_credits;

-- 8. Clear historical records
DELETE FROM public.historical_rides;
DELETE FROM public.historical_parks;

-- 9. Clear entity versions (history)
DELETE FROM public.ride_versions;
DELETE FROM public.park_versions;
DELETE FROM public.company_versions;
DELETE FROM public.ride_model_versions;
DELETE FROM public.entity_versions_archive;

-- 10. Clear main entity tables (no dependencies left)
DELETE FROM public.rides;
DELETE FROM public.parks;
DELETE FROM public.companies;
DELETE FROM public.ride_models;

-- 11. Clear park-related tables
DELETE FROM public.park_location_history;
DELETE FROM public.park_operating_hours;

-- 12. Clear analytics/tracking
DELETE FROM public.entity_page_views;

-- 13. Clear contact/support
DELETE FROM public.contact_email_threads;
DELETE FROM public.contact_submissions;

-- 14. Clear notification logs
DELETE FROM public.notification_logs;

-- Re-enable all triggers
SET session_replication_role = DEFAULT;

-- Verify user data remains intact
DO $$
DECLARE
  user_count INTEGER;
  profile_count INTEGER;
BEGIN
  SELECT COUNT(*) INTO user_count FROM auth.users;
  SELECT COUNT(*) INTO profile_count FROM public.profiles;
  
  RAISE NOTICE 'Database purge complete. Users preserved: %, Profiles: %', user_count, profile_count;
END $$;