-- AlterTable
ALTER TABLE `products`
    ADD COLUMN `customization_mode` ENUM('NONE', 'RECIPE', 'CONFIGURATOR') NOT NULL DEFAULT 'NONE',
    ADD COLUMN `configurator_id` VARCHAR(191) NULL;

-- Backfill legacy recipe-based products
UPDATE `products`
SET `customization_mode` = 'RECIPE'
WHERE `recipe_id` IS NOT NULL;

-- AlterTable
ALTER TABLE `order_items`
    ADD COLUMN `configuration_label` VARCHAR(191) NULL,
    ADD COLUMN `configuration_snapshot` JSON NULL;

-- CreateTable
CREATE TABLE `product_configurators` (
    `id` VARCHAR(191) NOT NULL,
    `shop_id` VARCHAR(191) NOT NULL,
    `name` VARCHAR(191) NOT NULL,
    `description` VARCHAR(191) NULL,
    `active` BOOLEAN NOT NULL DEFAULT true,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updated_at` DATETIME(3) NOT NULL,

    INDEX `product_configurators_shop_id_active_idx`(`shop_id`, `active`),
    UNIQUE INDEX `product_configurators_shop_id_name_key`(`shop_id`, `name`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `product_option_groups` (
    `id` VARCHAR(191) NOT NULL,
    `configurator_id` VARCHAR(191) NOT NULL,
    `code` VARCHAR(191) NOT NULL,
    `label` VARCHAR(191) NOT NULL,
    `selection_mode` ENUM('SINGLE', 'MULTIPLE') NOT NULL DEFAULT 'SINGLE',
    `min_choices` INTEGER NOT NULL DEFAULT 1,
    `max_choices` INTEGER NOT NULL DEFAULT 1,
    `display_order` INTEGER NOT NULL DEFAULT 0,
    `active` BOOLEAN NOT NULL DEFAULT true,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updated_at` DATETIME(3) NOT NULL,

    INDEX `product_option_groups_configurator_id_display_order_idx`(`configurator_id`, `display_order`),
    UNIQUE INDEX `product_option_groups_configurator_id_code_key`(`configurator_id`, `code`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `product_configurator_steps` (
    `id` VARCHAR(191) NOT NULL,
    `configurator_id` VARCHAR(191) NOT NULL,
    `code` VARCHAR(191) NOT NULL,
    `label` VARCHAR(191) NOT NULL,
    `display_order` INTEGER NOT NULL DEFAULT 0,
    `min_selections` INTEGER NOT NULL DEFAULT 1,
    `max_selections` INTEGER NOT NULL DEFAULT 1,
    `repeat_count` INTEGER NOT NULL DEFAULT 1,
    `entry_group_id` VARCHAR(191) NULL,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updated_at` DATETIME(3) NOT NULL,

    INDEX `product_configurator_steps_configurator_id_display_order_idx`(`configurator_id`, `display_order`),
    INDEX `product_configurator_steps_entry_group_id_idx`(`entry_group_id`),
    UNIQUE INDEX `product_configurator_steps_configurator_id_code_key`(`configurator_id`, `code`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `product_option_choices` (
    `id` VARCHAR(191) NOT NULL,
    `group_id` VARCHAR(191) NOT NULL,
    `code` VARCHAR(191) NOT NULL,
    `label` VARCHAR(191) NOT NULL,
    `description` VARCHAR(191) NULL,
    `price_delta` INTEGER NOT NULL DEFAULT 0,
    `display_order` INTEGER NOT NULL DEFAULT 0,
    `is_default` BOOLEAN NOT NULL DEFAULT false,
    `active` BOOLEAN NOT NULL DEFAULT true,
    `next_group_id` VARCHAR(191) NULL,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updated_at` DATETIME(3) NOT NULL,

    INDEX `product_option_choices_group_id_display_order_idx`(`group_id`, `display_order`),
    INDEX `product_option_choices_next_group_id_idx`(`next_group_id`),
    UNIQUE INDEX `product_option_choices_group_id_code_key`(`group_id`, `code`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- AddForeignKey
ALTER TABLE `products`
    ADD CONSTRAINT `products_configurator_id_fkey`
    FOREIGN KEY (`configurator_id`) REFERENCES `product_configurators`(`id`)
    ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `product_configurators`
    ADD CONSTRAINT `product_configurators_shop_id_fkey`
    FOREIGN KEY (`shop_id`) REFERENCES `shops`(`id`)
    ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `product_option_groups`
    ADD CONSTRAINT `product_option_groups_configurator_id_fkey`
    FOREIGN KEY (`configurator_id`) REFERENCES `product_configurators`(`id`)
    ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `product_configurator_steps`
    ADD CONSTRAINT `product_configurator_steps_configurator_id_fkey`
    FOREIGN KEY (`configurator_id`) REFERENCES `product_configurators`(`id`)
    ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `product_configurator_steps`
    ADD CONSTRAINT `product_configurator_steps_entry_group_id_fkey`
    FOREIGN KEY (`entry_group_id`) REFERENCES `product_option_groups`(`id`)
    ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `product_option_choices`
    ADD CONSTRAINT `product_option_choices_group_id_fkey`
    FOREIGN KEY (`group_id`) REFERENCES `product_option_groups`(`id`)
    ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `product_option_choices`
    ADD CONSTRAINT `product_option_choices_next_group_id_fkey`
    FOREIGN KEY (`next_group_id`) REFERENCES `product_option_groups`(`id`)
    ON DELETE SET NULL ON UPDATE CASCADE;

-- RedefineIndex
CREATE INDEX `products_configurator_id_idx` ON `products`(`configurator_id`);

