-- v0.401 M1: Add BPM, musical_key, category to products
-- Prerequisite: products table exists (GORM or prior migration)

DO $$
BEGIN
  IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'products') THEN
    ALTER TABLE products ADD COLUMN IF NOT EXISTS bpm INTEGER;
    ALTER TABLE products ADD COLUMN IF NOT EXISTS musical_key VARCHAR(10);
    ALTER TABLE products ADD COLUMN IF NOT EXISTS category VARCHAR(50);
    CREATE INDEX IF NOT EXISTS idx_products_category ON products(category) WHERE category IS NOT NULL;
    CREATE INDEX IF NOT EXISTS idx_products_bpm ON products(bpm) WHERE bpm IS NOT NULL;
    CREATE INDEX IF NOT EXISTS idx_products_musical_key ON products(musical_key) WHERE musical_key IS NOT NULL AND musical_key != '';
  END IF;
END $$;
