-- Migration: Add main_image_url and rating fields to product table, change default gender to Unisex

-- Add main_image_url field (optional, falls back to images[0] if not set)
ALTER TABLE product ADD COLUMN IF NOT EXISTS main_image_url TEXT DEFAULT NULL;

-- Add rating fields for popularity sorting
ALTER TABLE product ADD COLUMN IF NOT EXISTS rating_average DECIMAL(2,1) DEFAULT 0.0;
ALTER TABLE product ADD COLUMN IF NOT EXISTS rating_count INTEGER DEFAULT 0;

-- Update existing products with NULL or empty gender to 'Unisex'
UPDATE product SET gender = 'Unisex' WHERE gender IS NULL OR gender = '';

-- Create index for better query performance on ratings
CREATE INDEX IF NOT EXISTS idx_product_rating ON product(rating_average DESC);
