-- =============================================================================
-- Dabil v2 Migration — Run ONCE on your database
-- 1. Adds 'accepted' and 'served' to order_status enum
-- 2. Migrates old 'confirmed' orders to 'accepted'
-- 3. Ensures commission_rate = 10%
-- =============================================================================

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;

-- 1. Update order_status enum to include 'accepted' and 'served'
ALTER TABLE `orders`
  MODIFY COLUMN `order_status`
    ENUM('pending','accepted','preparing','ready','served','completed','cancelled')
    NOT NULL DEFAULT 'pending';

-- 2. Migrate any old 'confirmed' status to 'accepted'
UPDATE `orders` SET `order_status` = 'accepted' WHERE `order_status` = 'confirmed';

-- 3. Ensure commission rate is 10%
INSERT INTO `platform_settings` (`setting_key`, `setting_value`, `description`)
  VALUES ('commission_rate', '10', 'Percentage of every order credited to the Super Admin wallet')
ON DUPLICATE KEY UPDATE `setting_value` = '10';

COMMIT;

-- Verification:
-- SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='orders' AND COLUMN_NAME='order_status';
-- SELECT setting_value FROM platform_settings WHERE setting_key='commission_rate';
