-- Safe category cleanup that handles existing singular forms
-- This will consolidate similar categories and fix naming

-- First, update any products that reference plural categories to point to singular ones
-- Update any references to 'Fertilizers' to point to 'Fertilizer' (if it exists)
UPDATE products SET category_id = (
    SELECT id FROM product_categories WHERE name = 'Fertilizer' LIMIT 1
) WHERE category_id = (
    SELECT id FROM product_categories WHERE name = 'Fertilizers' LIMIT 1
) AND EXISTS (SELECT 1 FROM product_categories WHERE name = 'Fertilizer');

-- Update any references to 'Herbicides' to point to 'Herbicide' (if it exists)
UPDATE products SET category_id = (
    SELECT id FROM product_categories WHERE name = 'Herbicide' LIMIT 1
) WHERE category_id = (
    SELECT id FROM product_categories WHERE name = 'Herbicides' LIMIT 1
) AND EXISTS (SELECT 1 FROM product_categories WHERE name = 'Herbicide');

-- Update any references to 'Fungicides' to point to 'Fungicide' (if it exists)
UPDATE products SET category_id = (
    SELECT id FROM product_categories WHERE name = 'Fungicide' LIMIT 1
) WHERE category_id = (
    SELECT id FROM product_categories WHERE name = 'Fungicides' LIMIT 1
) AND EXISTS (SELECT 1 FROM product_categories WHERE name = 'Fungicide');

-- Update any references to 'Insecticides' to point to 'Insecticide' (if it exists)
UPDATE products SET category_id = (
    SELECT id FROM product_categories WHERE name = 'Insecticide' LIMIT 1
) WHERE category_id = (
    SELECT id FROM product_categories WHERE name = 'Insecticides' LIMIT 1
) AND EXISTS (SELECT 1 FROM product_categories WHERE name = 'Insecticide');

-- Delete plural categories that now have singular equivalents
DELETE FROM product_categories WHERE name IN ('Fertilizers', 'Herbicides', 'Fungicides', 'Insecticides') 
AND EXISTS (SELECT 1 FROM product_categories WHERE name IN ('Fertilizer', 'Herbicide', 'Fungicide', 'Insecticide'));

-- Update remaining plural categories to singular (only if singular doesn't exist)
UPDATE product_categories SET name = 'Surfactant' WHERE name = 'Surfactants' 
AND NOT EXISTS (SELECT 1 FROM product_categories WHERE name = 'Surfactant');

UPDATE product_categories SET name = 'Adjuvant' WHERE name = 'Adjuvants'
AND NOT EXISTS (SELECT 1 FROM product_categories WHERE name = 'Adjuvant');

UPDATE product_categories SET name = 'Growth Regulator' WHERE name = 'Growth Regulators'
AND NOT EXISTS (SELECT 1 FROM product_categories WHERE name = 'Growth Regulator');

-- Add any missing core categories (only if they don't exist)
INSERT INTO product_categories (name, description) VALUES
('Herbicide', 'Products for weed control and prevention'),
('Fertilizer', 'Nutrients for lawn growth and health'),
('Fungicide', 'Products for disease prevention and treatment'),
('Insecticide', 'Products for insect control'),
('Pre-emergent', 'Products that prevent weeds from germinating'),
('Post-emergent', 'Products that kill existing weeds'),
('Growth Regulator', 'Products that modify plant growth'),
('Surfactant', 'Products that improve spray coverage and penetration'),
('Adjuvant', 'Products that enhance pesticide performance'),
('Seed', 'Grass seeds and seed treatments'),
('Soil Amendment', 'Products that improve soil conditions')
ON CONFLICT (name) DO NOTHING;