-- The guest menu always had two chips the restaurant could not touch: "Sve" and
-- "Preporučujemo". They can now be renamed or hidden, the same way every other
-- category can.
ALTER TABLE `restaurant_settings` ADD `show_all_chip` integer DEFAULT 1 NOT NULL;
--> statement-breakpoint
ALTER TABLE `restaurant_settings` ADD `all_chip_label` text DEFAULT 'Sve' NOT NULL;
--> statement-breakpoint
ALTER TABLE `restaurant_settings` ADD `show_featured_chip` integer DEFAULT 1 NOT NULL;
--> statement-breakpoint
ALTER TABLE `restaurant_settings` ADD `featured_chip_label` text DEFAULT 'Preporučujemo' NOT NULL;
--> statement-breakpoint

-- Shifts. A shift is a named span on one day ("Jutarnja 08:00-16:00"), the
-- waiters working it, and which table each of them covers. Planning is separate
-- from the floor: `dining_tables.assigned_email` is only rewritten when the
-- manager applies a shift, so a roster drafted for tomorrow cannot reshuffle
-- tonight's service.
CREATE TABLE `shifts` (
	`id` text PRIMARY KEY NOT NULL,
	`restaurant_id` text NOT NULL,
	`name` text NOT NULL,
	`shift_date` text NOT NULL,
	`starts_at` text NOT NULL,
	`ends_at` text NOT NULL,
	`note` text DEFAULT '' NOT NULL,
	`applied_at` text,
	`created_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
	`updated_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
	FOREIGN KEY (`restaurant_id`) REFERENCES `restaurants`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE UNIQUE INDEX `shifts_restaurant_date_name_unique` ON `shifts` (`restaurant_id`,`shift_date`,`name`);
--> statement-breakpoint
CREATE INDEX `idx_shifts_restaurant_date` ON `shifts` (`restaurant_id`,`shift_date`);
--> statement-breakpoint

CREATE TABLE `shift_waiters` (
	`shift_id` text NOT NULL,
	`email` text NOT NULL,
	`created_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
	PRIMARY KEY(`shift_id`, `email`),
	FOREIGN KEY (`shift_id`) REFERENCES `shifts`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE INDEX `idx_shift_waiters_email` ON `shift_waiters` (`email`);
--> statement-breakpoint

-- One waiter per table per shift: two people on the same table is the confusion
-- this board exists to prevent.
CREATE TABLE `shift_tables` (
	`shift_id` text NOT NULL,
	`table_id` text NOT NULL,
	`email` text NOT NULL,
	`created_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
	PRIMARY KEY(`shift_id`, `table_id`),
	FOREIGN KEY (`shift_id`) REFERENCES `shifts`(`id`) ON UPDATE no action ON DELETE cascade,
	FOREIGN KEY (`table_id`) REFERENCES `dining_tables`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE INDEX `idx_shift_tables_shift_email` ON `shift_tables` (`shift_id`,`email`);
--> statement-breakpoint
PRAGMA optimize;
