-- Staff (Manage Operations) migration. Run once on the Dabil database.
-- Adds a flag so newly-created staff are forced to set their own password
-- on first login. Safe on MySQL 8+ (IF NOT EXISTS).

ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `must_reset_pw` TINYINT(1) NOT NULL DEFAULT 0;

-- Rush/escalation flag on orders: set when the kitchen rushes an overdue
-- order so it's promoted (badge + red accent + escalation banner) on the
-- kitchen and waiter screens.
ALTER TABLE `orders` ADD COLUMN IF NOT EXISTS `is_rushed` TINYINT(1) NOT NULL DEFAULT 0;
--   ALTER TABLE `orders` ADD COLUMN `is_rushed` TINYINT(1) NOT NULL DEFAULT 0;  -- older MySQL

-- "Call Server" assistance requests raised by customers from the Order Status
-- screen. The waiter dashboard shows open calls as a live alert banner.
CREATE TABLE IF NOT EXISTS `service_calls` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `restaurant_id` INT NOT NULL,
  `order_id` INT DEFAULT NULL,
  `user_id` INT DEFAULT NULL,
  `table_number` VARCHAR(20) DEFAULT NULL,
  `table_label` VARCHAR(120) DEFAULT NULL,
  `customer_name` VARCHAR(150) DEFAULT NULL,
  `status` ENUM('open','ack') NOT NULL DEFAULT 'open',
  `acked_by` INT DEFAULT NULL,
  `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `acked_at` TIMESTAMP NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_rest_status` (`restaurant_id`, `status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Older MySQL: run without IF NOT EXISTS and ignore "duplicate column":
--   ALTER TABLE `users` ADD COLUMN `must_reset_pw` TINYINT(1) NOT NULL DEFAULT 0;

-- Optional: when the restaurant admin creates a staff account with a
-- temporary password, set must_reset_pw = 1 so the staff member is prompted
-- to choose their own password the first time they sign in. e.g.:
--   UPDATE `users` SET must_reset_pw = 1 WHERE id = <new_staff_id>;
