-- Migration 00005: Seed Demo Data Function

CREATE OR REPLACE FUNCTION public.seed_household_demo_data(p_household_id UUID)
RETURNS VOID
LANGUAGE plpgsql
SECURITY DEFINER
SET search_path = public
AS $$
DECLARE
    v_user_id UUID;
    v_cat_gewuerze UUID;
    v_cat_kuehl UUID;
    v_cat_tiefkuehl UUID;
    v_cat_getraenke UUID;
    v_cat_konserven UUID;
    v_cat_back UUID;
    v_cat_grund UUID;
    v_cat_obst_gemuese UUID;
    v_cat_haushalt UUID;

    v_loc_kuehlschrank UUID;
    v_loc_gefrierschrank UUID;
    v_loc_gewuerzschrank UUID;
    v_loc_vorratsschrank UUID;
    v_loc_getraenkelager UUID;

    v_unit_stueck UUID;
    v_unit_packung UUID;
    v_unit_flasche UUID;
    v_unit_dose UUID;
    v_unit_g UUID;
    v_unit_kg UUID;
    v_unit_l UUID;
BEGIN
    v_user_id := auth.uid();
    IF NOT public.has_household_role(p_household_id, ARRAY['MEMBER', 'ADMIN', 'OWNER']::public.household_role[]) THEN
        RAISE EXCEPTION 'PERMISSION_DENIED: User is not authorized to seed household %', p_household_id;
    END IF;

    -- Categories
    INSERT INTO public.categories (household_id, name, sort_order) VALUES
    (p_household_id, 'Gewürze', 1) RETURNING id INTO v_cat_gewuerze;
    INSERT INTO public.categories (household_id, name, sort_order) VALUES
    (p_household_id, 'Kühlwaren', 2) RETURNING id INTO v_cat_kuehl;
    INSERT INTO public.categories (household_id, name, sort_order) VALUES
    (p_household_id, 'Tiefkühlprodukte', 3) RETURNING id INTO v_cat_tiefkuehl;
    INSERT INTO public.categories (household_id, name, sort_order) VALUES
    (p_household_id, 'Getränke', 4) RETURNING id INTO v_cat_getraenke;
    INSERT INTO public.categories (household_id, name, sort_order) VALUES
    (p_household_id, 'Konserven', 5) RETURNING id INTO v_cat_konserven;
    INSERT INTO public.categories (household_id, name, sort_order) VALUES
    (p_household_id, 'Backzutaten', 6) RETURNING id INTO v_cat_back;
    INSERT INTO public.categories (household_id, name, sort_order) VALUES
    (p_household_id, 'Grundnahrungsmittel', 7) RETURNING id INTO v_cat_grund;
    INSERT INTO public.categories (household_id, name, sort_order) VALUES
    (p_household_id, 'Obst und Gemüse', 8) RETURNING id INTO v_cat_obst_gemuese;
    INSERT INTO public.categories (household_id, name, sort_order) VALUES
    (p_household_id, 'Haushalt', 9) RETURNING id INTO v_cat_haushalt;

    -- Locations
    INSERT INTO public.locations (household_id, name, sort_order) VALUES
    (p_household_id, 'Kühlschrank', 1) RETURNING id INTO v_loc_kuehlschrank;
    INSERT INTO public.locations (household_id, name, sort_order) VALUES
    (p_household_id, 'Gefrierschrank', 2) RETURNING id INTO v_loc_gefrierschrank;
    INSERT INTO public.locations (household_id, name, sort_order) VALUES
    (p_household_id, 'Gewürzschrank', 3) RETURNING id INTO v_loc_gewuerzschrank;
    INSERT INTO public.locations (household_id, name, sort_order) VALUES
    (p_household_id, 'Vorratsschrank', 4) RETURNING id INTO v_loc_vorratsschrank;
    INSERT INTO public.locations (household_id, name, sort_order) VALUES
    (p_household_id, 'Getränkelager', 5) RETURNING id INTO v_loc_getraenkelager;

    -- Units
    INSERT INTO public.units (household_id, name, abbreviation, unit_type, allows_decimals) VALUES
    (p_household_id, 'Stück', 'Stk.', 'COUNT', false) RETURNING id INTO v_unit_stueck;
    INSERT INTO public.units (household_id, name, abbreviation, unit_type, allows_decimals) VALUES
    (p_household_id, 'Packung', 'Pk.', 'COUNT', false) RETURNING id INTO v_unit_packung;
    INSERT INTO public.units (household_id, name, abbreviation, unit_type, allows_decimals) VALUES
    (p_household_id, 'Flasche', 'Fl.', 'COUNT', false) RETURNING id INTO v_unit_flasche;
    INSERT INTO public.units (household_id, name, abbreviation, unit_type, allows_decimals) VALUES
    (p_household_id, 'Dose', 'Ds.', 'COUNT', false) RETURNING id INTO v_unit_dose;
    INSERT INTO public.units (household_id, name, abbreviation, unit_type, allows_decimals) VALUES
    (p_household_id, 'Gramm', 'g', 'WEIGHT', true) RETURNING id INTO v_unit_g;
    INSERT INTO public.units (household_id, name, abbreviation, unit_type, allows_decimals) VALUES
    (p_household_id, 'Kilogramm', 'kg', 'WEIGHT', true) RETURNING id INTO v_unit_kg;
    INSERT INTO public.units (household_id, name, abbreviation, unit_type, allows_decimals) VALUES
    (p_household_id, 'Liter', 'l', 'VOLUME', true) RETURNING id INTO v_unit_l;

    -- Standard Products with Batches
    -- Paprikapulver
    INSERT INTO public.products (household_id, category_id, default_location_id, default_unit_id, name, brand, package_size, package_unit, minimum_quantity, estimated_price_cents, favorite, created_by)
    VALUES (p_household_id, v_cat_gewuerze, v_loc_gewuerzschrank, v_unit_dose, 'Paprikapulver', 'Ostmann', 1, 'Dose', 1, 199, true, v_user_id);
    
    -- Salz
    INSERT INTO public.products (household_id, category_id, default_location_id, default_unit_id, name, brand, package_size, package_unit, minimum_quantity, estimated_price_cents, favorite, created_by)
    VALUES (p_household_id, v_cat_gewuerze, v_loc_gewuerzschrank, v_unit_packung, 'Salz', 'Bad Reichenhaller', 500, 'g', 1, 99, false, v_user_id);

    -- Pfeffer
    INSERT INTO public.products (household_id, category_id, default_location_id, default_unit_id, name, brand, package_size, package_unit, minimum_quantity, estimated_price_cents, favorite, created_by)
    VALUES (p_household_id, v_cat_gewuerze, v_loc_gewuerzschrank, v_unit_dose, 'Pfeffer schwarz', 'Ostmann', 1, 'Dose', 1, 249, false, v_user_id);

    -- Mehl
    INSERT INTO public.products (household_id, category_id, default_location_id, default_unit_id, name, brand, package_size, package_unit, minimum_quantity, estimated_price_cents, favorite, created_by)
    VALUES (p_household_id, v_cat_back, v_loc_vorratsschrank, v_unit_kg, 'Weizenmehl Type 405', 'Diamant', 1000, 'g', 2, 129, false, v_user_id);

    -- Nudeln
    INSERT INTO public.products (household_id, category_id, default_location_id, default_unit_id, name, brand, package_size, package_unit, minimum_quantity, estimated_price_cents, favorite, created_by)
    VALUES (p_household_id, v_cat_grund, v_loc_vorratsschrank, v_unit_packung, 'Spaghetti', 'Barilla', 500, 'g', 2, 189, true, v_user_id);

    -- Reis
    INSERT INTO public.products (household_id, category_id, default_location_id, default_unit_id, name, brand, package_size, package_unit, minimum_quantity, estimated_price_cents, favorite, created_by)
    VALUES (p_household_id, v_cat_grund, v_loc_vorratsschrank, v_unit_packung, 'Basmati Reis', 'Uncle Ben''s', 1000, 'g', 1, 349, false, v_user_id);

    -- Milch
    INSERT INTO public.products (household_id, category_id, default_location_id, default_unit_id, name, brand, package_size, package_unit, minimum_quantity, estimated_price_cents, favorite, created_by)
    VALUES (p_household_id, v_cat_kuehl, v_loc_kuehlschrank, v_unit_l, 'Vollmilch 3.5%', 'Weihenstephan', 1, 'l', 2, 119, true, v_user_id);

    -- Cola Zero
    INSERT INTO public.products (household_id, category_id, default_location_id, default_unit_id, name, brand, package_size, package_unit, minimum_quantity, estimated_price_cents, favorite, created_by)
    VALUES (p_household_id, v_cat_getraenke, v_loc_getraenkelager, v_unit_flasche, 'Coca-Cola Zero', 'Coca-Cola', 1.5, 'l', 4, 129, false, v_user_id);

    -- Tomaten
    INSERT INTO public.products (household_id, category_id, default_location_id, default_unit_id, name, brand, package_size, package_unit, minimum_quantity, estimated_price_cents, favorite, created_by)
    VALUES (p_household_id, v_cat_obst_gemuese, v_loc_kuehlschrank, v_unit_packung, 'Strauchtomaten', 'Bio', 500, 'g', 1, 229, true, v_user_id);

    -- Kartoffeln
    INSERT INTO public.products (household_id, category_id, default_location_id, default_unit_id, name, brand, package_size, package_unit, minimum_quantity, estimated_price_cents, favorite, created_by)
    VALUES (p_household_id, v_cat_obst_gemuese, v_loc_vorratsschrank, v_unit_kg, 'Speisekartoffeln festkochend', 'Regional', 2.5, 'kg', 1, 299, false, v_user_id);

    -- Zwiebeln
    INSERT INTO public.products (household_id, category_id, default_location_id, default_unit_id, name, brand, package_size, package_unit, minimum_quantity, estimated_price_cents, favorite, created_by)
    VALUES (p_household_id, v_cat_obst_gemuese, v_loc_vorratsschrank, v_unit_packung, 'Speisezwiebeln', 'Regional', 1, 'kg', 1, 149, true, v_user_id);
END;
$$;
