-- Script to drop old foreign key constraints
-- This is the third part of the final cleanup

-- Drop foreign key constraints that reference the key tables first
DO $$ 
BEGIN
    -- Drop foreign key constraints in market_items table
    IF EXISTS (SELECT constraint_name FROM information_schema.table_constraints WHERE table_name = 'market_items' AND constraint_name = 'market_items_market_id_fkey') THEN
        ALTER TABLE market_items DROP CONSTRAINT market_items_market_id_fkey;
    END IF;
    
    -- Drop foreign key constraints in bookings table
    IF EXISTS (SELECT constraint_name FROM information_schema.table_constraints WHERE table_name = 'bookings' AND constraint_name = 'bookings_user_id_fkey') THEN
        ALTER TABLE bookings DROP CONSTRAINT bookings_user_id_fkey;
    END IF;
    IF EXISTS (SELECT constraint_name FROM information_schema.table_constraints WHERE table_name = 'bookings' AND constraint_name = 'bookings_beach_place_id_fkey') THEN
        ALTER TABLE bookings DROP CONSTRAINT bookings_beach_place_id_fkey;
    END IF;
    
    -- Drop foreign key constraints in reviews table
    IF EXISTS (SELECT constraint_name FROM information_schema.table_constraints WHERE table_name = 'reviews' AND constraint_name = 'reviews_user_id_fkey') THEN
        ALTER TABLE reviews DROP CONSTRAINT reviews_user_id_fkey;
    END IF;
    IF EXISTS (SELECT constraint_name FROM information_schema.table_constraints WHERE table_name = 'reviews' AND constraint_name = 'reviews_beach_place_id_fkey') THEN
        ALTER TABLE reviews DROP CONSTRAINT reviews_beach_place_id_fkey;
    END IF;
    IF EXISTS (SELECT constraint_name FROM information_schema.table_constraints WHERE table_name = 'reviews' AND constraint_name = 'reviews_booking_id_fkey') THEN
        ALTER TABLE reviews DROP CONSTRAINT reviews_booking_id_fkey;
    END IF;
END $$;