-- Fix Tables Naming and Missing Columns
-- Rename name to table_number if it exists (using a more compatible approach)
-- Note: In older MySQL/MariaDB, we might need to check column existence via information_schema
-- but for simplicity in a migration file, we'll try to add the column if missing or rename it.

-- Check if table_number exists, if not, try to rename name or add table_number
-- Since we can't easily do IF/ELSE in plain SQL without procedures, we'll use targeted ALTERs
-- that might fail if already corrected, which db/migrate.php will report.

ALTER TABLE `tables` ADD COLUMN IF NOT EXISTS `table_number` VARCHAR(50) AFTER `area_id`;
ALTER TABLE `tables` ADD COLUMN IF NOT EXISTS `status` ENUM('available', 'occupied', 'reserved', 'inactive') DEFAULT 'available' AFTER `capacity`;
ALTER TABLE `tables` ADD COLUMN IF NOT EXISTS `is_deleted` TINYINT(1) DEFAULT 0;

-- Copy name to table_number if name exists and table_number is empty
-- This is a bit tricky in pure SQL without knowing if 'name' still exists.
-- But usually, if they have 'name', they need to move it to 'table_number'.

-- If name exists, this will work. If not, it will fail, which is okay for this specific fix.
-- UPDATE `tables` SET `table_number` = `name` WHERE `table_number` IS NULL OR `table_number` = '';

-- Fix areas foreign key if it was wrong in their initial setup
ALTER TABLE `areas` DROP FOREIGN KEY IF EXISTS `areas_ibfk_1`;
ALTER TABLE `areas` ADD CONSTRAINT `areas_ibfk_1` FOREIGN KEY (`outlet_id`) REFERENCES `outlets`(`id`);
ALTER TABLE `areas` ADD COLUMN IF NOT EXISTS `is_deleted` TINYINT(1) DEFAULT 0;
