CREATE TABLE `deliveries` (
	`id` int AUTO_INCREMENT NOT NULL,
	`reference` varchar(80) NOT NULL,
	`saleId` int NOT NULL,
	`status` enum('prepared','dispatched','delivered','cancelled') NOT NULL DEFAULT 'prepared',
	`deliveredAt` timestamp,
	`notes` text,
	`createdByUserId` int,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `deliveries_id` PRIMARY KEY(`id`),
	CONSTRAINT `deliveries_reference_unique` UNIQUE(`reference`)
);
--> statement-breakpoint
CREATE TABLE `delivery_lines` (
	`id` int AUTO_INCREMENT NOT NULL,
	`deliveryId` int NOT NULL,
	`saleItemId` int NOT NULL,
	`stockLotId` int,
	`quantity` int NOT NULL,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `delivery_lines_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE INDEX `deliveries_sale_index` ON `deliveries` (`saleId`);--> statement-breakpoint
CREATE INDEX `delivery_lines_delivery_index` ON `delivery_lines` (`deliveryId`);