-- Migration: 000.604.sql
-- Description: Create table for tracking swapped products with cascade delete behavior
CREATE TABLE swapped_products (
    id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    original_product_id INTEGER NOT NULL REFERENCES "product-products"(productid),
    new_product_id INTEGER NOT NULL REFERENCES "product-products"(productid),
    swapped_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,

    -- Ensure unique combination of original and swapped product (prevent duplicates)
    UNIQUE(original_product_id),
    UNIQUE(new_product_id),
    CHECK (original_product_id IS DISTINCT FROM new_product_id)
);
