CREATE TABLE `FlyerSession` (
  `id` INTEGER NOT NULL AUTO_INCREMENT,
  `userId` INTEGER NOT NULL,
  `retailer` VARCHAR(191) NOT NULL,
  `weekKey` VARCHAR(191) NOT NULL,
  `status` VARCHAR(191) NOT NULL DEFAULT 'draft',
  `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  `updatedAt` DATETIME(3) NOT NULL,
  `expiresAt` DATETIME(3) NULL,

  INDEX `FlyerSession_userId_idx`(`userId`),
  INDEX `FlyerSession_weekKey_idx`(`weekKey`),
  INDEX `FlyerSession_status_idx`(`status`),
  PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

CREATE TABLE `FlyerItem` (
  `id` INTEGER NOT NULL AUTO_INCREMENT,
  `sessionId` INTEGER NOT NULL,
  `rawName` VARCHAR(191) NOT NULL,
  `normalizedName` VARCHAR(191) NOT NULL,
  `categoryHint` VARCHAR(191) NULL,
  `price` DECIMAL(10, 2) NULL,
  `priceUnit` VARCHAR(191) NULL,
  `comparisonPrice` DECIMAL(10, 2) NULL,
  `comparisonUnit` VARCHAR(191) NULL,
  `offerText` VARCHAR(191) NULL,
  `parseConfidence` DOUBLE NOT NULL,
  `parseReasons` JSON NULL,
  `matchedProductId` INTEGER NULL,
  `matchedProductName` VARCHAR(191) NULL,
  `matchedVia` VARCHAR(191) NULL,
  `matchConfidence` DOUBLE NULL,
  `matchReasons` JSON NULL,
  `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  `updatedAt` DATETIME(3) NOT NULL,

  INDEX `FlyerItem_sessionId_idx`(`sessionId`),
  INDEX `FlyerItem_normalizedName_idx`(`normalizedName`),
  PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

CREATE TABLE `FlyerSelection` (
  `id` INTEGER NOT NULL AUTO_INCREMENT,
  `sessionId` INTEGER NOT NULL,
  `itemId` INTEGER NOT NULL,
  `userId` INTEGER NOT NULL,
  `plannedQuantity` DECIMAL(10, 2) NULL,
  `plannedUnit` VARCHAR(191) NULL,
  `priority` VARCHAR(191) NOT NULL DEFAULT 'normal',
  `note` VARCHAR(191) NULL,
  `status` VARCHAR(191) NOT NULL DEFAULT 'planned',
  `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  `updatedAt` DATETIME(3) NOT NULL,

  UNIQUE INDEX `FlyerSelection_sessionId_itemId_key`(`sessionId`, `itemId`),
  INDEX `FlyerSelection_sessionId_idx`(`sessionId`),
  INDEX `FlyerSelection_userId_status_idx`(`userId`, `status`),
  PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

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

ALTER TABLE `FlyerItem`
  ADD CONSTRAINT `FlyerItem_sessionId_fkey`
  FOREIGN KEY (`sessionId`) REFERENCES `FlyerSession`(`id`)
  ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE `FlyerSelection`
  ADD CONSTRAINT `FlyerSelection_sessionId_fkey`
  FOREIGN KEY (`sessionId`) REFERENCES `FlyerSession`(`id`)
  ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE `FlyerSelection`
  ADD CONSTRAINT `FlyerSelection_itemId_fkey`
  FOREIGN KEY (`itemId`) REFERENCES `FlyerItem`(`id`)
  ON DELETE CASCADE ON UPDATE CASCADE;

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