-- sql/drop_tables.sql
/*
-- This script is used to completely reset the public schema by dropping all tables.
-- It should be run before re-running the master schema script to ensure a clean state.
-- The CASCADE option is used to automatically handle dependent objects like foreign keys.
-- The order is grouped logically for readability, though CASCADE makes the exact order less critical.
*/

-- ============================================================================
-- Drop All Application Tables
-- ============================================================================

-- Drop linking and leaf tables first
DROP TABLE IF EXISTS public.user_achievements CASCADE;
DROP TABLE IF EXISTS public.user_reactions CASCADE;
DROP TABLE IF EXISTS public.user_follows CASCADE;
DROP TABLE IF EXISTS public.shared_recipe_collections CASCADE;
DROP TABLE IF EXISTS public.user_appliances CASCADE;
DROP TABLE IF EXISTS public.recipe_collection_items CASCADE;
DROP TABLE IF EXISTS public.favorite_stores CASCADE;
DROP TABLE IF EXISTS public.favorite_recipes CASCADE;
DROP TABLE IF EXISTS public.user_item_aliases CASCADE;
DROP TABLE IF EXISTS public.recipe_appliances CASCADE;
DROP TABLE IF EXISTS public.shared_menu_plans CASCADE;
DROP TABLE IF EXISTS public.recipe_ingredient_substitutions CASCADE;
DROP TABLE IF EXISTS public.recipe_comments CASCADE;
DROP TABLE IF EXISTS public.planned_meals CASCADE;
DROP TABLE IF EXISTS public.recipe_ratings CASCADE;
DROP TABLE IF EXISTS public.recipe_tags CASCADE;
DROP TABLE IF EXISTS public.shared_shopping_lists CASCADE;
DROP TABLE IF EXISTS public.recipe_ingredients CASCADE;
DROP TABLE IF EXISTS public.flyer_locations CASCADE;
DROP TABLE IF EXISTS public.receipt_items CASCADE;
DROP TABLE IF EXISTS public.shopping_trip_items CASCADE;
DROP TABLE IF EXISTS public.pantry_items CASCADE;
DROP TABLE IF EXISTS public.suggested_corrections CASCADE;
DROP TABLE IF EXISTS public.shopping_list_items CASCADE;
DROP TABLE IF EXISTS public.user_alerts CASCADE;
DROP TABLE IF EXISTS public.user_watched_items CASCADE;
DROP TABLE IF EXISTS public.master_item_aliases CASCADE;
DROP TABLE IF EXISTS public.item_price_history CASCADE;
DROP TABLE IF EXISTS public.flyer_items CASCADE;
DROP TABLE IF EXISTS public.user_dietary_restrictions CASCADE;

-- Drop primary data tables
DROP TABLE IF EXISTS public.activity_log CASCADE;
DROP TABLE IF EXISTS public.notifications CASCADE;
DROP TABLE IF EXISTS public.password_reset_tokens CASCADE;
DROP TABLE IF EXISTS public.search_queries CASCADE;
DROP TABLE IF EXISTS public.user_submitted_prices CASCADE;
DROP TABLE IF EXISTS public.unmatched_flyer_items CASCADE;
DROP TABLE IF EXISTS public.unit_conversions CASCADE;
DROP TABLE IF EXISTS public.shopping_trips CASCADE;
DROP TABLE IF EXISTS public.receipts CASCADE;
DROP TABLE IF EXISTS public.pantry_locations CASCADE;
DROP TABLE IF EXISTS public.shopping_lists CASCADE;
DROP TABLE IF EXISTS public.menu_plans CASCADE;
DROP TABLE IF EXISTS public.recipe_collections CASCADE;
DROP TABLE IF EXISTS public.recipes CASCADE;
DROP TABLE IF EXISTS public.products CASCADE;
DROP TABLE IF EXISTS public.brands CASCADE;
DROP TABLE IF EXISTS public.flyers CASCADE;
DROP TABLE IF EXISTS public.store_locations CASCADE;
DROP TABLE IF EXISTS public.stores CASCADE;
DROP TABLE IF EXISTS public.master_grocery_items CASCADE;
DROP TABLE IF EXISTS public.tags CASCADE;
DROP TABLE IF EXISTS public.appliances CASCADE;
DROP TABLE IF EXISTS public.dietary_restrictions CASCADE;
DROP TABLE IF EXISTS public.categories CASCADE;
DROP TABLE IF EXISTS public.achievements CASCADE;
DROP TABLE IF EXISTS public.budgets CASCADE;
DROP TABLE IF EXISTS public.profiles CASCADE;
DROP TABLE IF EXISTS public.users CASCADE;

-- Drop metadata and utility tables last
DROP TABLE IF EXISTS public.schema_info CASCADE;

-- ============================================================================
-- Drop PostGIS System Tables (Optional)
-- ============================================================================
-- These tables are created by the PostGIS extension. Dropping them ensures
-- a completely clean public schema if the extension were to be re-installed.
-- It's generally safe to include these in a full reset script.

-- These tables are owned by the 'postgis' extension and cannot be dropped directly.
-- Doing so would require dropping the extension itself, which is not the goal of this script.
-- We only want to reset the application's data, not the database's extensions.
-- DROP TABLE IF EXISTS public.spatial_ref_sys CASCADE;
-- DROP TABLE IF EXISTS public.geometry_columns CASCADE;
-- DROP TABLE IF EXISTS public.geography_columns CASCADE;
