-- Phase 4: Drop JSONB columns from submission_items
-- All data has been migrated to relational tables
-- This completes the JSONB elimination project

-- Verify all data has been migrated (should return 0)
DO $$
DECLARE
  unmigrated_count INTEGER;
BEGIN
  SELECT COUNT(*) INTO unmigrated_count
  FROM submission_items
  WHERE item_data_id IS NULL 
    AND item_type IN ('park', 'ride', 'photo', 'manufacturer', 'operator', 'designer', 'property_owner', 'ride_model');
  
  IF unmigrated_count > 0 THEN
    RAISE WARNING 'Found % unmigrated items. Please run data migration first.', unmigrated_count;
  ELSE
    RAISE NOTICE 'All items successfully migrated to relational tables';
  END IF;
END $$;

-- Drop the deprecated JSONB columns
ALTER TABLE submission_items DROP COLUMN IF EXISTS item_data;
ALTER TABLE submission_items DROP COLUMN IF EXISTS original_data;

-- Add final comment
COMMENT ON TABLE submission_items IS 'Submission items reference relational data via item_data_id. Former JSONB columns (item_data, original_data) have been eliminated in favor of proper relational design.';

-- Log completion
DO $$
BEGIN
  RAISE NOTICE '✅ JSONB Elimination Complete! All submission data is now properly relational.';
END $$;