ALTER TABLE `FlyerSession`
  ADD COLUMN `sourceFileName` VARCHAR(191) NULL,
  ADD COLUMN `sourceMimeType` VARCHAR(191) NULL,
  ADD COLUMN `sourceFileSize` INTEGER NULL,
  ADD COLUMN `sourceStorageKey` VARCHAR(191) NULL,
  ADD COLUMN `sourceData` LONGBLOB NULL;

ALTER TABLE `FlyerItem`
  ADD COLUMN `categoryId` INTEGER NULL;

ALTER TABLE `FlyerItem`
  ADD CONSTRAINT `FlyerItem_categoryId_fkey`
  FOREIGN KEY (`categoryId`) REFERENCES `Category`(`id`)
  ON DELETE SET NULL ON UPDATE CASCADE;

CREATE INDEX `FlyerItem_categoryId_idx` ON `FlyerItem`(`categoryId`);

CREATE TABLE `ShoppingListItem` (
  `id` INTEGER NOT NULL AUTO_INCREMENT,
  `userId` INTEGER NOT NULL,
  `name` VARCHAR(191) NOT NULL,
  `productId` INTEGER NULL,
  `categoryId` INTEGER NULL,
  `quantity` DECIMAL(10, 2) NULL,
  `unit` VARCHAR(191) NULL,
  `source` VARCHAR(191) NOT NULL DEFAULT 'manual',
  `status` VARCHAR(191) NOT NULL DEFAULT 'open',
  `checkedAt` DATETIME(3) NULL,
  `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  `updatedAt` DATETIME(3) NOT NULL,

  INDEX `ShoppingListItem_userId_status_idx`(`userId`, `status`),
  INDEX `ShoppingListItem_productId_unit_status_idx`(`productId`, `unit`, `status`),
  INDEX `ShoppingListItem_categoryId_idx`(`categoryId`),
  PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

ALTER TABLE `ShoppingListItem`
  ADD CONSTRAINT `ShoppingListItem_userId_fkey`
  FOREIGN KEY (`userId`) REFERENCES `User`(`id`)
  ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE `ShoppingListItem`
  ADD CONSTRAINT `ShoppingListItem_productId_fkey`
  FOREIGN KEY (`productId`) REFERENCES `Product`(`id`)
  ON DELETE SET NULL ON UPDATE CASCADE;

ALTER TABLE `ShoppingListItem`
  ADD CONSTRAINT `ShoppingListItem_categoryId_fkey`
  FOREIGN KEY (`categoryId`) REFERENCES `Category`(`id`)
  ON DELETE SET NULL ON UPDATE CASCADE;
