-- ============================================================================
-- INITIAL DATA SEEDING SCRIPT
-- ============================================================================
-- Purpose:
-- This script populates the database with essential starting data.
-- It should be run AFTER the schema has been created (e.g., after initial.sql).
-- It is idempotent, meaning it can be run multiple times without causing errors.

-- 1. Pre-populate the master grocery items dictionary.
-- This block links generic items to their respective categories.
DO $$
DECLARE
    fv_cat_id BIGINT; ms_cat_id BIGINT; de_cat_id BIGINT; bb_cat_id BIGINT; pdg_cat_id BIGINT;
    bev_cat_id BIGINT; ff_cat_id BIGINT; snk_cat_id BIGINT; hc_cat_id BIGINT; pch_cat_id BIGINT;
    bc_cat_id BIGINT; ps_cat_id BIGINT; dpf_cat_id BIGINT; cg_cat_id BIGINT; cs_cat_id BIGINT;
    bkc_cat_id BIGINT;
BEGIN
    SELECT category_id INTO fv_cat_id FROM public.categories WHERE name = 'Fruits & Vegetables';
    SELECT category_id INTO ms_cat_id FROM public.categories WHERE name = 'Meat & Seafood';
    SELECT category_id INTO de_cat_id FROM public.categories WHERE name = 'Dairy & Eggs';
    SELECT category_id INTO bb_cat_id FROM public.categories WHERE name = 'Bakery & Bread';
    SELECT category_id INTO pdg_cat_id FROM public.categories WHERE name = 'Pantry & Dry Goods';
    SELECT category_id INTO bev_cat_id FROM public.categories WHERE name = 'Beverages';
    SELECT category_id INTO ff_cat_id FROM public.categories WHERE name = 'Frozen Foods';
    SELECT category_id INTO snk_cat_id FROM public.categories WHERE name = 'Snacks';
    SELECT category_id INTO hc_cat_id FROM public.categories WHERE name = 'Household & Cleaning';
    SELECT category_id INTO pch_cat_id FROM public.categories WHERE name = 'Personal Care & Health';
    SELECT category_id INTO bc_cat_id FROM public.categories WHERE name = 'Baby & Child';
    SELECT category_id INTO ps_cat_id FROM public.categories WHERE name = 'Pet Supplies';
    SELECT category_id INTO dpf_cat_id FROM public.categories WHERE name = 'Deli & Prepared Foods';
    SELECT category_id INTO cg_cat_id FROM public.categories WHERE name = 'Canned Goods';
    SELECT category_id INTO cs_cat_id FROM public.categories WHERE name = 'Condiments & Spices';
    SELECT category_id INTO bkc_cat_id FROM public.categories WHERE name = 'Breakfast & Cereal';

    INSERT INTO public.master_grocery_items (name, category_id) VALUES
    ('apples', fv_cat_id), ('bananas', fv_cat_id), ('oranges', fv_cat_id), ('grapes', fv_cat_id), ('strawberries', fv_cat_id), ('blueberries', fv_cat_id), ('raspberries', fv_cat_id), ('avocados', fv_cat_id), ('tomatoes', fv_cat_id), ('potatoes', fv_cat_id), ('onions', fv_cat_id), ('garlic', fv_cat_id), ('carrots', fv_cat_id), ('broccoli', fv_cat_id), ('spinach', fv_cat_id), ('lettuce', fv_cat_id), ('bell peppers', fv_cat_id), ('cucumbers', fv_cat_id), ('mushrooms', fv_cat_id), ('lemons', fv_cat_id), ('limes', fv_cat_id), ('celery', fv_cat_id), ('corn', fv_cat_id), ('sweet potatoes', fv_cat_id), ('zucchini', fv_cat_id), ('cauliflower', fv_cat_id), ('green beans', fv_cat_id), ('peas', fv_cat_id), ('asparagus', fv_cat_id),
    ('chicken breast', ms_cat_id), ('chicken thighs', ms_cat_id), ('ground beef', ms_cat_id), ('steak', ms_cat_id), ('pork chops', ms_cat_id), ('bacon', ms_cat_id), ('sausage', ms_cat_id), ('salmon', ms_cat_id), ('shrimp', ms_cat_id), ('tilapia', ms_cat_id), ('cod', ms_cat_id), ('tuna', ms_cat_id), ('ham', ms_cat_id), ('turkey', ms_cat_id),
    ('milk', de_cat_id), ('cheese', de_cat_id), ('yogurt', de_cat_id), ('butter', de_cat_id), ('eggs', de_cat_id), ('cream cheese', de_cat_id), ('sour cream', de_cat_id), ('cottage cheese', de_cat_id),
    ('bread', bb_cat_id), ('bagels', bb_cat_id), ('tortillas', bb_cat_id), ('croissants', bb_cat_id), ('muffins', bb_cat_id), ('baguette', bb_cat_id), ('pita bread', bb_cat_id),
    ('rice', pdg_cat_id), ('pasta', pdg_cat_id), ('flour', pdg_cat_id), ('sugar', pdg_cat_id), ('salt', pdg_cat_id), ('pepper', pdg_cat_id), ('olive oil', pdg_cat_id), ('vegetable oil', pdg_cat_id), ('canned tomatoes', pdg_cat_id), ('canned beans', pdg_cat_id), ('peanut butter', pdg_cat_id), ('jam', pdg_cat_id), ('honey', pdg_cat_id), ('syrup', pdg_cat_id), ('nuts', pdg_cat_id), ('dried fruit', pdg_cat_id), ('crackers', pdg_cat_id), ('quinoa', pdg_cat_id), ('lentils', pdg_cat_id),
    ('water', bev_cat_id), ('juice', bev_cat_id), ('soda', bev_cat_id), ('coffee', bev_cat_id), ('tea', bev_cat_id), ('almond milk', bev_cat_id), ('soy milk', bev_cat_id), ('coconut water', bev_cat_id),
    ('frozen pizza', ff_cat_id), ('frozen vegetables', ff_cat_id), ('frozen fruit', ff_cat_id), ('ice cream', ff_cat_id), ('frozen dinners', ff_cat_id), ('french fries', ff_cat_id), ('frozen fish', ff_cat_id),
    ('chips', snk_cat_id), ('pretzels', snk_cat_id), ('popcorn', snk_cat_id), ('granola bars', snk_cat_id), ('cookies', snk_cat_id), ('chocolate', snk_cat_id), ('candy', snk_cat_id),
    ('paper towels', hc_cat_id), ('toilet paper', hc_cat_id), ('trash bags', hc_cat_id), ('dish soap', hc_cat_id), ('laundry detergent', hc_cat_id), ('all-purpose cleaner', hc_cat_id), ('sponges', hc_cat_id),
    ('soap', pch_cat_id), ('shampoo', pch_cat_id), ('conditioner', pch_cat_id), ('toothpaste', pch_cat_id), ('deodorant', pch_cat_id), ('vitamins', pch_cat_id), ('pain reliever', pch_cat_id),
    ('diapers', bc_cat_id), ('baby wipes', bc_cat_id), ('baby food', bc_cat_id), ('formula', bc_cat_id),
    ('dog food', ps_cat_id), ('cat food', ps_cat_id), ('cat litter', ps_cat_id),
    ('deli meat', dpf_cat_id), ('deli cheese', dpf_cat_id), ('rotisserie chicken', dpf_cat_id), ('prepared salads', dpf_cat_id),
    ('canned soup', cg_cat_id), ('canned corn', cg_cat_id), ('canned tuna', cg_cat_id), ('canned chicken', cg_cat_id),
    ('ketchup', cs_cat_id), ('mustard', cs_cat_id), ('mayonnaise', cs_cat_id), ('soy sauce', cs_cat_id), ('hot sauce', cs_cat_id), ('bbq sauce', cs_cat_id), ('salad dressing', cs_cat_id), ('cinnamon', cs_cat_id), ('oregano', cs_cat_id), ('paprika', cs_cat_id), ('garlic powder', cs_cat_id),
    ('cereal', bkc_cat_id), ('oatmeal', bkc_cat_id), ('granola', bkc_cat_id), ('pancake mix', bkc_cat_id)
    ON CONFLICT (name) DO NOTHING;
END $$;

-- 2. Pre-populate the categories table from a predefined list.
DO $$
BEGIN
    INSERT INTO public.categories (name) VALUES
    ('Fruits & Vegetables'), ('Meat & Seafood'), ('Dairy & Eggs'), ('Bakery & Bread'), 
    ('Pantry & Dry Goods'), ('Beverages'), ('Frozen Foods'), ('Snacks'), ('Household & Cleaning'),
    ('Personal Care & Health'), ('Baby & Child'), ('Pet Supplies'), ('Deli & Prepared Foods'),
    ('Canned Goods'), ('Condiments & Spices'), ('Breakfast & Cereal'), ('Organic'), 
    ('International Foods'), ('Other/Miscellaneous')
    ON CONFLICT (name) DO NOTHING;
END $$;

-- 3. Pre-populate the brands and products tables.
-- This block adds common brands and links them to specific products.
DO $$
DECLARE
    -- Store & Brand IDs
    loblaws_id BIGINT; coke_id BIGINT; kraft_id BIGINT; maple_leaf_id BIGINT; dempsters_id BIGINT; no_name_id BIGINT; pc_id BIGINT;
    -- Master Item IDs
    soda_item_id BIGINT; pasta_item_id BIGINT; turkey_item_id BIGINT; bread_item_id BIGINT; cheese_item_id BIGINT;
BEGIN
    -- Insert a store for the store brands
    INSERT INTO public.stores (name) VALUES ('Loblaws') ON CONFLICT (name) DO NOTHING;
    SELECT store_id INTO loblaws_id FROM public.stores WHERE name = 'Loblaws';

    -- Insert brands and get their IDs
    INSERT INTO public.brands (name) VALUES ('Coca-Cola'), ('Kraft'), ('Maple Leaf'), ('Dempster''s'), ('No Name'), ('President''s Choice')
    ON CONFLICT (name) DO NOTHING;

    SELECT brand_id INTO coke_id FROM public.brands WHERE name = 'Coca-Cola';
    SELECT brand_id INTO kraft_id FROM public.brands WHERE name = 'Kraft';
    SELECT brand_id INTO maple_leaf_id FROM public.brands WHERE name = 'Maple Leaf';
    SELECT brand_id INTO dempsters_id FROM public.brands WHERE name = 'Dempster''s';
    SELECT brand_id INTO no_name_id FROM public.brands WHERE name = 'No Name';
    SELECT brand_id INTO pc_id FROM public.brands WHERE name = 'President''s Choice';

    -- Link store brands to their store
    UPDATE public.brands SET store_id = loblaws_id WHERE name = 'No Name';
    UPDATE public.brands SET store_id = loblaws_id WHERE name = 'President''s Choice';

    -- Get master item IDs
    SELECT mgi.master_grocery_item_id INTO soda_item_id FROM public.master_grocery_items mgi WHERE mgi.name = 'soda';
    SELECT mgi.master_grocery_item_id INTO pasta_item_id FROM public.master_grocery_items mgi WHERE mgi.name = 'pasta';
    SELECT mgi.master_grocery_item_id INTO turkey_item_id FROM public.master_grocery_items mgi WHERE mgi.name = 'turkey';
    SELECT mgi.master_grocery_item_id INTO bread_item_id FROM public.master_grocery_items mgi WHERE mgi.name = 'bread';
    SELECT mgi.master_grocery_item_id INTO cheese_item_id FROM public.master_grocery_items mgi WHERE mgi.name = 'cheese';

    -- Insert specific products, linking master items and brands
    INSERT INTO public.products (master_item_id, brand_id, name, size, upc_code) VALUES
    (soda_item_id, coke_id, 'Coca-Cola Classic', '2L Bottle', '067000004114'),
    (pasta_item_id, kraft_id, 'Kraft Dinner Original Macaroni & Cheese', '225g Box', '068100058918'),
    (turkey_item_id, maple_leaf_id, 'Maple Leaf Natural Selections Sliced Turkey Breast', '175g', '063100123456'),
    (bread_item_id, dempsters_id, 'Dempster''s 100% Whole Wheat Bread', '675g Loaf', '068721001005'),
    (cheese_item_id, no_name_id, 'No Name Cheddar Cheese Block', '400g', '060383037575'),
    (cheese_item_id, pc_id, 'PC Old Cheddar Cheese', '400g', '060383000005')
    ON CONFLICT (upc_code) DO NOTHING;
END $$;

-- 4. Pre-populate the master_item_aliases table.
-- This block adds common alternative names for master items to improve fuzzy matching.
DO $$
DECLARE
    ground_beef_id BIGINT;
    chicken_breast_id BIGINT;
    chicken_thighs_id BIGINT;
    bell_peppers_id BIGINT;
    soda_id BIGINT;
    paper_towels_id BIGINT;
    toilet_paper_id BIGINT;
BEGIN
    -- Get master item IDs
    SELECT mgi.master_grocery_item_id INTO ground_beef_id FROM public.master_grocery_items mgi WHERE mgi.name = 'ground beef';
    SELECT mgi.master_grocery_item_id INTO chicken_breast_id FROM public.master_grocery_items mgi WHERE mgi.name = 'chicken breast';
    SELECT mgi.master_grocery_item_id INTO chicken_thighs_id FROM public.master_grocery_items mgi WHERE mgi.name = 'chicken thighs';
    SELECT mgi.master_grocery_item_id INTO bell_peppers_id FROM public.master_grocery_items mgi WHERE mgi.name = 'bell peppers';
    SELECT mgi.master_grocery_item_id INTO soda_id FROM public.master_grocery_items mgi WHERE mgi.name = 'soda';
    SELECT mgi.master_grocery_item_id INTO paper_towels_id FROM public.master_grocery_items mgi WHERE mgi.name = 'paper towels';
    SELECT mgi.master_grocery_item_id INTO toilet_paper_id FROM public.master_grocery_items mgi WHERE mgi.name = 'toilet paper';

    -- Insert aliases, ignoring any that might already exist
    INSERT INTO public.master_item_aliases (master_item_id, alias) VALUES
    (ground_beef_id, 'ground chuck'), (ground_beef_id, 'lean ground beef'), (ground_beef_id, 'extra lean ground beef'), (ground_beef_id, 'hamburger meat'),
    (chicken_breast_id, 'boneless skinless chicken breast'), (chicken_breast_id, 'chicken cutlets'),
    (chicken_thighs_id, 'boneless skinless chicken thighs'), (chicken_thighs_id, 'bone-in chicken thighs'),
    (bell_peppers_id, 'red pepper'), (bell_peppers_id, 'green pepper'), (bell_peppers_id, 'yellow pepper'), (bell_peppers_id, 'orange pepper'),
    (soda_id, 'pop'), (soda_id, 'soft drink'), (soda_id, 'coke'), (soda_id, 'pepsi'),
    (paper_towels_id, 'paper towel'),
    (toilet_paper_id, 'bathroom tissue'), (toilet_paper_id, 'toilet tissue')
    ON CONFLICT (alias) DO NOTHING;
END $$;

-- 5. Pre-populate recipes, ingredients, and tags to make the recommendation engine effective.
DO $$
DECLARE
    -- Recipe IDs
    chicken_recipe_id BIGINT;
    bolognese_recipe_id BIGINT;
    stir_fry_recipe_id BIGINT;

    -- Ingredient (Master Item) IDs
    chicken_breast_id BIGINT; rice_id BIGINT; broccoli_id BIGINT;
    ground_beef_id BIGINT; pasta_item_id BIGINT; tomatoes_id BIGINT; onions_id BIGINT; garlic_id BIGINT;
    bell_peppers_id BIGINT; carrots_id BIGINT; soy_sauce_id BIGINT;

    -- Tag IDs
    quick_easy_tag BIGINT; healthy_tag BIGINT; chicken_tag BIGINT;
    family_tag BIGINT; beef_tag BIGINT; weeknight_tag BIGINT; vegetarian_tag BIGINT;
BEGIN
    -- Insert sample recipes and get their IDs
    INSERT INTO public.recipes (name, description, instructions, prep_time_minutes, cook_time_minutes, servings) VALUES
    ('Simple Chicken and Rice', 'A quick and healthy weeknight meal with chicken, rice, and broccoli.', '1. Cook rice according to package directions. 2. Steam broccoli. 3. Pan-sear chicken breast until cooked through. 4. Combine and serve.', 10, 20, 4),
    ('Classic Spaghetti Bolognese', 'A rich and hearty meat sauce served over spaghetti, perfect for the whole family.', '1. Brown ground beef with onions and garlic. 2. Add tomatoes and simmer for 30 minutes. 3. Cook pasta. 4. Serve sauce over pasta.', 15, 45, 6),
    ('Vegetable Stir-fry', 'A fast, flavorful, and vegetarian stir-fry loaded with fresh vegetables.', '1. Chop all vegetables. 2. Heat oil in a wok or large pan. 3. Stir-fry vegetables for 5-7 minutes until tender-crisp. 4. Add soy sauce and serve immediately.', 10, 10, 3)
    ON CONFLICT (name) WHERE user_id IS NULL DO NOTHING;

    SELECT recipe_id INTO chicken_recipe_id FROM public.recipes WHERE name = 'Simple Chicken and Rice';
    SELECT recipe_id INTO bolognese_recipe_id FROM public.recipes WHERE name = 'Classic Spaghetti Bolognese';
    SELECT recipe_id INTO stir_fry_recipe_id FROM public.recipes WHERE name = 'Vegetable Stir-fry';

    -- Get ingredient IDs from master_grocery_items
    SELECT mgi.master_grocery_item_id INTO chicken_breast_id FROM public.master_grocery_items mgi WHERE mgi.name = 'chicken breast';
    SELECT mgi.master_grocery_item_id INTO rice_id FROM public.master_grocery_items mgi WHERE mgi.name = 'rice';
    SELECT mgi.master_grocery_item_id INTO broccoli_id FROM public.master_grocery_items mgi WHERE mgi.name = 'broccoli';
    SELECT mgi.master_grocery_item_id INTO ground_beef_id FROM public.master_grocery_items mgi WHERE mgi.name = 'ground beef';
    SELECT mgi.master_grocery_item_id INTO pasta_item_id FROM public.master_grocery_items mgi WHERE mgi.name = 'pasta';
    SELECT mgi.master_grocery_item_id INTO tomatoes_id FROM public.master_grocery_items mgi WHERE mgi.name = 'tomatoes';
    SELECT mgi.master_grocery_item_id INTO onions_id FROM public.master_grocery_items mgi WHERE mgi.name = 'onions';
    SELECT mgi.master_grocery_item_id INTO garlic_id FROM public.master_grocery_items mgi WHERE mgi.name = 'garlic';
    SELECT mgi.master_grocery_item_id INTO bell_peppers_id FROM public.master_grocery_items mgi WHERE mgi.name = 'bell peppers';
    SELECT mgi.master_grocery_item_id INTO carrots_id FROM public.master_grocery_items mgi WHERE mgi.name = 'carrots';
    SELECT mgi.master_grocery_item_id INTO soy_sauce_id FROM public.master_grocery_items mgi WHERE mgi.name = 'soy sauce';

    -- Insert ingredients for each recipe
    INSERT INTO public.recipe_ingredients (recipe_id, master_item_id, quantity, unit) VALUES
    (chicken_recipe_id, chicken_breast_id, 2, 'items'), (chicken_recipe_id, rice_id, 200, 'g'), (chicken_recipe_id, broccoli_id, 300, 'g'),
    (bolognese_recipe_id, ground_beef_id, 500, 'g'), (bolognese_recipe_id, pasta_item_id, 400, 'g'), (bolognese_recipe_id, tomatoes_id, 800, 'g'), (bolognese_recipe_id, onions_id, 1, 'items'), (bolognese_recipe_id, garlic_id, 2, 'cloves'),
    (stir_fry_recipe_id, broccoli_id, 200, 'g'), (stir_fry_recipe_id, bell_peppers_id, 1, 'items'), (stir_fry_recipe_id, carrots_id, 2, 'items'), (stir_fry_recipe_id, onions_id, 1, 'items'), (stir_fry_recipe_id, soy_sauce_id, 50, 'ml')
    ON CONFLICT (recipe_ingredient_id) DO NOTHING;

    -- Insert tags and get their IDs
    INSERT INTO public.tags (name) VALUES ('Quick & Easy'), ('Healthy'), ('Chicken'), ('Family Friendly'), ('Beef'), ('Weeknight Dinner'), ('Vegetarian')
    ON CONFLICT (name) DO NOTHING;

    SELECT tag_id INTO quick_easy_tag FROM public.tags WHERE name = 'Quick & Easy';
    SELECT tag_id INTO healthy_tag FROM public.tags WHERE name = 'Healthy';
    SELECT tag_id INTO chicken_tag FROM public.tags WHERE name = 'Chicken';
    SELECT tag_id INTO family_tag FROM public.tags WHERE name = 'Family Friendly';
    SELECT tag_id INTO beef_tag FROM public.tags WHERE name = 'Beef';
    SELECT tag_id INTO weeknight_tag FROM public.tags WHERE name = 'Weeknight Dinner';
    SELECT tag_id INTO vegetarian_tag FROM public.tags WHERE name = 'Vegetarian';

    -- Link tags to recipes
    INSERT INTO public.recipe_tags (recipe_id, tag_id) VALUES
    (chicken_recipe_id, quick_easy_tag), (chicken_recipe_id, healthy_tag), (chicken_recipe_id, chicken_tag), (chicken_recipe_id, weeknight_tag),
    (bolognese_recipe_id, family_tag), (bolognese_recipe_id, beef_tag), (bolognese_recipe_id, weeknight_tag),
    (stir_fry_recipe_id, quick_easy_tag), (stir_fry_recipe_id, healthy_tag), (stir_fry_recipe_id, vegetarian_tag)
    ON CONFLICT (recipe_id, tag_id) DO NOTHING;
END $$;

-- 6. Pre-populate the unit_conversions table with common cooking conversions.
-- Factors are for converting 1 unit of `from_unit` to the `to_unit`.
DO $$
DECLARE
    flour_id BIGINT; sugar_id BIGINT; butter_id BIGINT; milk_id BIGINT; water_id BIGINT; rice_id BIGINT;
BEGIN
    -- Get master item IDs
    SELECT mgi.master_grocery_item_id INTO flour_id FROM public.master_grocery_items mgi WHERE mgi.name = 'flour';
    SELECT mgi.master_grocery_item_id INTO sugar_id FROM public.master_grocery_items mgi WHERE mgi.name = 'sugar';
    SELECT mgi.master_grocery_item_id INTO butter_id FROM public.master_grocery_items mgi WHERE mgi.name = 'butter';
    SELECT mgi.master_grocery_item_id INTO milk_id FROM public.master_grocery_items mgi WHERE mgi.name = 'milk';
    SELECT mgi.master_grocery_item_id INTO water_id FROM public.master_grocery_items mgi WHERE mgi.name = 'water';
    SELECT mgi.master_grocery_item_id INTO rice_id FROM public.master_grocery_items mgi WHERE mgi.name = 'rice';

    -- Insert conversion factors
    INSERT INTO public.unit_conversions (master_item_id, from_unit, to_unit, factor) VALUES
    -- 1 gram of flour is approx 0.00833 cups
    (flour_id, 'g', 'cup', 0.00833), (flour_id, 'cup', 'g', 120),
    -- 1 gram of sugar is approx 0.005 cups
    (sugar_id, 'g', 'cup', 0.005), (sugar_id, 'cup', 'g', 200),
    -- 1 gram of butter is approx 0.0044 cups
    (butter_id, 'g', 'cup', 0.0044), (butter_id, 'cup', 'g', 227),
    -- 1 ml of water/milk is approx 0.0042 cups
    (water_id, 'ml', 'cup', 0.0042), (water_id, 'cup', 'ml', 240),
    (milk_id, 'ml', 'cup', 0.0042), (milk_id, 'cup', 'ml', 240)
    ON CONFLICT (master_item_id, from_unit, to_unit) DO NOTHING;
END $$;

-- 7. Pre-populate the dietary_restrictions table.
INSERT INTO public.dietary_restrictions (name, type) VALUES
('Vegetarian', 'diet'), ('Vegan', 'diet'), ('Gluten-Free', 'diet'), ('Keto', 'diet'),
('Dairy', 'allergy'), ('Eggs', 'allergy'), ('Fish', 'allergy'), ('Shellfish', 'allergy'),
('Tree Nuts', 'allergy'), ('Peanuts', 'allergy'), ('Soy', 'allergy'), ('Wheat', 'allergy')
ON CONFLICT (name) DO NOTHING;

-- 8. Pre-populate the appliances table.
INSERT INTO public.appliances (name) VALUES
('Oven'), ('Microwave'), ('Stovetop'), ('Blender'), ('Food Processor'),
('Stand Mixer'), ('Hand Mixer'), ('Air Fryer'), ('Instant Pot'), ('Slow Cooker'),
('Grill'), ('Toaster')
ON CONFLICT (name) DO NOTHING;
