-- ============================================
-- MULTI-VENDOR ROLLBACK SCRIPT
-- Date: 2025-09-01
-- Description: Rollback multi-vendor changes if needed
-- WARNING: This will permanently delete restaurant data!
-- ============================================

-- STEP 1: Remove foreign key constraints first
ALTER TABLE `users` DROP FOREIGN KEY `users_restaurant_id_foreign`;
ALTER TABLE `menu_items` DROP FOREIGN KEY `menu_items_restaurant_id_foreign`;
ALTER TABLE `orders` DROP FOREIGN KEY `orders_restaurant_id_foreign`;
ALTER TABLE `staff` DROP FOREIGN KEY `staff_restaurant_id_foreign`;
ALTER TABLE `inventories` DROP FOREIGN KEY `inventories_restaurant_id_foreign`;
ALTER TABLE `coupons` DROP FOREIGN KEY `coupons_restaurant_id_foreign`;
ALTER TABLE `menu_categories` DROP FOREIGN KEY `menu_categories_restaurant_id_foreign`;
ALTER TABLE `restaurant_settings` DROP FOREIGN KEY `restaurant_settings_restaurant_id_foreign`;

-- STEP 2: Remove indexes
ALTER TABLE `users` DROP INDEX `users_restaurant_id_index`;
ALTER TABLE `menu_items` DROP INDEX `menu_items_restaurant_id_index`;
ALTER TABLE `orders` DROP INDEX `orders_restaurant_id_index`;
ALTER TABLE `staff` DROP INDEX `staff_restaurant_id_index`;
ALTER TABLE `inventories` DROP INDEX `inventories_restaurant_id_index`;
ALTER TABLE `coupons` DROP INDEX `coupons_restaurant_id_index`;
ALTER TABLE `menu_categories` DROP INDEX `menu_categories_restaurant_id_index`;
ALTER TABLE `restaurant_settings` DROP INDEX `restaurant_settings_restaurant_id_index`;

-- STEP 3: Remove columns from existing tables
ALTER TABLE `users` DROP COLUMN `restaurant_id`;
ALTER TABLE `menu_items` DROP COLUMN `restaurant_id`;
ALTER TABLE `orders` DROP COLUMN `restaurant_id`;
ALTER TABLE `staff` DROP COLUMN `restaurant_id`;
ALTER TABLE `inventories` DROP COLUMN `restaurant_id`;
ALTER TABLE `coupons` DROP COLUMN `restaurant_id`;
ALTER TABLE `menu_categories` DROP COLUMN `restaurant_id`;
ALTER TABLE `restaurant_settings` DROP COLUMN `restaurant_id`;
ALTER TABLE `restaurant_settings` DROP INDEX `restaurant_settings_restaurant_unique`;

-- STEP 4: Revert users role enum
ALTER TABLE `users` 
MODIFY COLUMN `role` enum('admin','staff','manager') COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'staff';

-- STEP 5: Drop restaurants table
DROP TABLE `restaurants`;

-- STEP 6: Update migration table to remove executed migrations
DELETE FROM `migrations` WHERE `migration` IN (
    '2025_09_01_000001_create_restaurants_table',
    '2025_09_01_000002_add_restaurant_fields_to_users_table'
);

-- ============================================
-- END OF ROLLBACK SCRIPT
-- ============================================
