-- CreateTable
CREATE TABLE `station_product_links` (
    `id` VARCHAR(191) NOT NULL,
    `station_id` VARCHAR(191) NOT NULL,
    `product_id` VARCHAR(191) NOT NULL,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    UNIQUE INDEX `station_product_links_station_id_product_id_key`(`station_id`, `product_id`),
    INDEX `station_product_links_product_id_idx`(`product_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- Backfill existing production assignments
INSERT INTO `station_product_links` (`id`, `station_id`, `product_id`, `created_at`)
SELECT UUID(), `station_id`, `id`, CURRENT_TIMESTAMP(3)
FROM `products`
WHERE `station_id` IS NOT NULL;

-- AddForeignKey
ALTER TABLE `station_product_links`
ADD CONSTRAINT `station_product_links_station_id_fkey`
FOREIGN KEY (`station_id`) REFERENCES `stations`(`id`)
ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `station_product_links`
ADD CONSTRAINT `station_product_links_product_id_fkey`
FOREIGN KEY (`product_id`) REFERENCES `products`(`id`)
ON DELETE CASCADE ON UPDATE CASCADE;

