-- Create product_images table
CREATE TABLE product_images (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    product_id UUID NOT NULL,
    image_path VARCHAR(255) NOT NULL,
    display_order INT NOT NULL DEFAULT 0,
    is_primary BOOLEAN NOT NULL DEFAULT false,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    FOREIGN KEY (product_id) REFERENCES products(id) ON DELETE CASCADE
);

-- Create indexes for performance
CREATE INDEX idx_product_images_product_id ON product_images(product_id);

-- Modify existing products table to remove single image_url field
ALTER TABLE products DROP COLUMN IF EXISTS image_url;

-- Insert test images for existing products
INSERT INTO product_images (product_id, image_path, display_order, is_primary) 
SELECT id, '/images/amethyst-geode.jpg', 0, true FROM products WHERE name = 'Amethyst Geode';

INSERT INTO product_images (product_id, image_path, display_order, is_primary) 
SELECT id, '/images/amethyst-geode-closeup.jpg', 1, false FROM products WHERE name = 'Amethyst Geode';

INSERT INTO product_images (product_id, image_path, display_order, is_primary) 
SELECT id, '/images/labradorite.jpg', 0, true FROM products WHERE name = 'Polished Labradorite';

INSERT INTO product_images (product_id, image_path, display_order, is_primary) 
SELECT id, '/images/turquoise.jpg', 0, true FROM products WHERE name = 'Raw Turquoise';

INSERT INTO product_images (product_id, image_path, display_order, is_primary) 
SELECT id, '/images/deer-antler.jpg', 0, true FROM products WHERE name = 'Deer Antler';

INSERT INTO product_images (product_id, image_path, display_order, is_primary) 
SELECT id, '/images/fossil-fish.jpg', 0, true FROM products WHERE name = 'Fossil Fish';

INSERT INTO product_images (product_id, image_path, display_order, is_primary) 
SELECT id, '/images/driftwood.jpg', 0, true FROM products WHERE name = 'Driftwood Piece';

INSERT INTO product_images (product_id, image_path, display_order, is_primary) 
SELECT id, '/images/walking-stick.jpg', 0, true FROM products WHERE name = 'Walking Stick';

INSERT INTO product_images (product_id, image_path, display_order, is_primary) 
SELECT id, '/images/birch-branches.jpg', 0, true FROM products WHERE name = 'Decorative Branch Set';