-- =====================================================================================
-- 生成特定分类的测试商品数据
-- 说明：为主要分类各生成6个真实感的商品数据
-- =====================================================================================

DO $$
DECLARE
    -- 商家ID
    v_merchant_id UUID;
    
    -- 分类ID
    cat_mobile UUID;
    cat_computer UUID;
    cat_mens UUID;
    cat_womens UUID;
    cat_snacks UUID;
    cat_fruits UUID;
    cat_appliances UUID;
    cat_beauty UUID;
    
    -- 临时变量
    v_product_id UUID;
BEGIN
    -- 1. 获取一个商家ID (如果没有商家，就获取任意一个用户)
    SELECT id INTO v_merchant_id FROM public.ak_users WHERE role = 'merchant' LIMIT 1;
    IF v_merchant_id IS NULL THEN
        SELECT id INTO v_merchant_id FROM public.ak_users LIMIT 1;
    END IF;
    
    IF v_merchant_id IS NULL THEN
        RAISE EXCEPTION '未找到可用用户作为商家';
    END IF;

    -- 2. 获取分类ID
    SELECT id INTO cat_mobile FROM public.ml_categories WHERE name = '手机通讯' LIMIT 1;
    SELECT id INTO cat_computer FROM public.ml_categories WHERE name = '电脑办公' LIMIT 1;
    SELECT id INTO cat_mens FROM public.ml_categories WHERE name = '男装' LIMIT 1;
    SELECT id INTO cat_womens FROM public.ml_categories WHERE name = '女装' LIMIT 1;
    SELECT id INTO cat_snacks FROM public.ml_categories WHERE name = '零食坚果' LIMIT 1;
    SELECT id INTO cat_fruits FROM public.ml_categories WHERE name = '新鲜水果' LIMIT 1;
    SELECT id INTO cat_appliances FROM public.ml_categories WHERE name = '家用电器' LIMIT 1;
    SELECT id INTO cat_beauty FROM public.ml_categories WHERE name = '美妆护肤' LIMIT 1;

    -- =================================================================================
    -- 3. 插入数据
    -- =================================================================================

    -- ---------------------------------------------------------------------------------
    -- 手机通讯 (6个)
    -- ---------------------------------------------------------------------------------
    IF cat_mobile IS NOT NULL THEN
        -- 1. iPhone 15
        INSERT INTO public.ml_products (merchant_id, category_id, product_code, name, description, main_image_url, base_price, total_stock, available_stock, status, sale_count)
        VALUES (v_merchant_id, cat_mobile, 'MOB001', 'Apple iPhone 15 Pro Max 256GB', '钛金属边框，A17 Pro芯片', '/static/images/demo/p1.jpg', 9999.00, 100, 100, 1, 1205);

        -- 2. Huawei Mate 60
        INSERT INTO public.ml_products (merchant_id, category_id, product_code, name, description, main_image_url, base_price, total_stock, available_stock, status, sale_count)
        VALUES (v_merchant_id, cat_mobile, 'MOB002', 'Huawei Mate 60 Pro 12GB+512GB', '雅川青，超光变XMAGE影像', '/static/images/demo/p2.jpg', 6999.00, 50, 50, 1, 5600);

        -- 3. Xiaomi 14
        INSERT INTO public.ml_products (merchant_id, category_id, product_code, name, description, main_image_url, base_price, total_stock, available_stock, status, sale_count)
        VALUES (v_merchant_id, cat_mobile, 'MOB003', 'Xiaomi 14 Pro 徕卡可变光圈', '骁龙8Gen3，光影猎人900', '/static/images/demo/p3.jpg', 4999.00, 200, 200, 1, 3200);

        -- 4. Samsung S24
        INSERT INTO public.ml_products (merchant_id, category_id, product_code, name, description, main_image_url, base_price, total_stock, available_stock, status, sale_count)
        VALUES (v_merchant_id, cat_mobile, 'MOB004', 'Samsung Galaxy S24 Ultra', 'AI手机，第二代动态AMOLED', '/static/images/demo/p4.jpg', 8899.00, 80, 80, 1, 890);

        -- 5. OPPO Find X7
        INSERT INTO public.ml_products (merchant_id, category_id, product_code, name, description, main_image_url, base_price, total_stock, available_stock, status, sale_count)
        VALUES (v_merchant_id, cat_mobile, 'MOB005', 'OPPO Find X7 Ultra', '双潜望四主摄，哈苏大师影像', '/static/images/demo/p5.jpg', 5999.00, 120, 120, 1, 1500);

        -- 6. Vivo X100
        INSERT INTO public.ml_products (merchant_id, category_id, product_code, name, description, main_image_url, base_price, total_stock, available_stock, status, sale_count)
        VALUES (v_merchant_id, cat_mobile, 'MOB006', 'vivo X100 Pro 蔡司APO超级长焦', '蓝晶x天玑9300芯片', '/static/images/demo/p6.jpg', 5499.00, 150, 150, 1, 2100);
    END IF;

    -- ---------------------------------------------------------------------------------
    -- 电脑办公 (6个)
    -- ---------------------------------------------------------------------------------
    IF cat_computer IS NOT NULL THEN
        INSERT INTO public.ml_products (merchant_id, category_id, product_code, name, description, main_image_url, base_price, total_stock, available_stock, status) VALUES 
        (v_merchant_id, cat_computer, 'COMP001', 'MacBook Pro 14英寸 M3芯片', '太空黑，18GB+512GB', '/static/images/demo/p1.jpg', 12999.00, 50, 50, 1),
        (v_merchant_id, cat_computer, 'COMP002', 'Lenovo ThinkPad X1 Carbon 2024', '商务轻薄本，酷睿Ultra7', '/static/images/demo/p2.jpg', 14999.00, 30, 30, 1),
        (v_merchant_id, cat_computer, 'COMP003', 'Dell XPS 15 创作本', '4K OLED触控屏，i9处理器', '/static/images/demo/p3.jpg', 18999.00, 20, 20, 1),
        (v_merchant_id, cat_computer, 'COMP004', 'Logitech MX Master 3S', '无线蓝牙鼠标，静音滚轮', '/static/images/demo/p4.jpg', 899.00, 500, 500, 1),
        (v_merchant_id, cat_computer, 'COMP005', 'Keychron K3 Pro 机械键盘', '矮轴双模，QMK/VIA开源', '/static/images/demo/p5.jpg', 468.00, 300, 300, 1),
        (v_merchant_id, cat_computer, 'COMP006', 'ASUS ROG 玩家国度 显示器', '4K 144Hz HDR电竞显示器', '/static/images/demo/p6.jpg', 4999.00, 60, 60, 1);
    END IF;

    -- ---------------------------------------------------------------------------------
    -- 男装 (6个)
    -- ---------------------------------------------------------------------------------
    IF cat_mens IS NOT NULL THEN
        INSERT INTO public.ml_products (merchant_id, category_id, product_code, name, description, main_image_url, base_price, total_stock, available_stock, status) VALUES 
        (v_merchant_id, cat_mens, 'MEN001', '纯棉重磅T恤男', '宽松美式复古，多色可选', '/static/images/demo/p1.jpg', 89.00, 1000, 1000, 1),
        (v_merchant_id, cat_mens, 'MEN002', '商务休闲修身西装外套', '高弹力抗皱，四季款', '/static/images/demo/p2.jpg', 399.00, 200, 200, 1),
        (v_merchant_id, cat_mens, 'MEN003', '直筒水洗牛仔裤', '经典五袋款，YKK拉链', '/static/images/demo/p3.jpg', 199.00, 500, 500, 1),
        (v_merchant_id, cat_mens, 'MEN004', '速干运动短裤', '透气网眼，跑步健身', '/static/images/demo/p4.jpg', 69.00, 800, 800, 1),
        (v_merchant_id, cat_mens, 'MEN005', '美利奴羊毛衫', 'V领针织，保暖舒适', '/static/images/demo/p5.jpg', 299.00, 150, 150, 1),
        (v_merchant_id, cat_mens, 'MEN006', '工装连帽夹克', '防风防水，多口袋设计', '/static/images/demo/p6.jpg', 459.00, 120, 120, 1);
    END IF;

    -- ---------------------------------------------------------------------------------
    -- 女装 (6个)
    -- ---------------------------------------------------------------------------------
    IF cat_womens IS NOT NULL THEN
        INSERT INTO public.ml_products (merchant_id, category_id, product_code, name, description, main_image_url, base_price, total_stock, available_stock, status) VALUES 
        (v_merchant_id, cat_womens, 'WOMEN001', '法式碎花连衣裙', '收腰显瘦，雪纺面料', '/static/images/demo/p1.jpg', 269.00, 300, 300, 1),
        (v_merchant_id, cat_womens, 'WOMEN002', '高腰A字半身裙', '百搭款，防走光内衬', '/static/images/demo/p2.jpg', 159.00, 400, 400, 1),
        (v_merchant_id, cat_womens, 'WOMEN003', '真丝缎面衬衫', '高级感通勤，垂坠感好', '/static/images/demo/p3.jpg', 399.00, 100, 100, 1),
        (v_merchant_id, cat_womens, 'WOMEN004', '羊绒双面呢大衣', '赫本风中长款，纯手工缝制', '/static/images/demo/p4.jpg', 1299.00, 80, 80, 1),
        (v_merchant_id, cat_womens, 'WOMEN005', '紧身弹力小脚裤', '魔术黑裤，修饰腿型', '/static/images/demo/p5.jpg', 129.00, 600, 600, 1),
        (v_merchant_id, cat_womens, 'WOMEN006', '宽松慵懒风毛衣', '马海毛混纺，温柔系', '/static/images/demo/p6.jpg', 189.00, 250, 250, 1);
    END IF;

    -- ---------------------------------------------------------------------------------
    -- 零食坚果 (6个)
    -- ---------------------------------------------------------------------------------
    IF cat_snacks IS NOT NULL THEN
        INSERT INTO public.ml_products (merchant_id, category_id, product_code, name, description, main_image_url, base_price, total_stock, available_stock, status) VALUES 
        (v_merchant_id, cat_snacks, 'SNACK001', '混合每日坚果 30包', '腰果核桃蔓越莓，营养均衡', '/static/images/demo/p1.jpg', 129.00, 1000, 1000, 1),
        (v_merchant_id, cat_snacks, 'SNACK002', '手撕风干牛肉干', '原味/香辣，高蛋白', '/static/images/demo/p2.jpg', 89.00, 500, 500, 1),
        (v_merchant_id, cat_snacks, 'SNACK003', '原切薯片 组合装', '黄瓜味/番茄味/原味', '/static/images/demo/p3.jpg', 29.90, 2000, 2000, 1),
        (v_merchant_id, cat_snacks, 'SNACK004', '72%黑巧克力', '纯可可脂，入口即化', '/static/images/demo/p4.jpg', 45.00, 300, 300, 1),
        (v_merchant_id, cat_snacks, 'SNACK005', '芒果干 500g', '菲律宾进口，酸甜适中', '/static/images/demo/p5.jpg', 39.90, 800, 800, 1),
        (v_merchant_id, cat_snacks, 'SNACK006', '网红辣条大礼包', '怀旧零食，麻辣鲜香', '/static/images/demo/p6.jpg', 19.90, 3000, 3000, 1);
    END IF;

    -- ---------------------------------------------------------------------------------
    -- 新鲜水果 (6个)
    -- ---------------------------------------------------------------------------------
    IF cat_fruits IS NOT NULL THEN
        INSERT INTO public.ml_products (merchant_id, category_id, product_code, name, description, main_image_url, base_price, total_stock, available_stock, status) VALUES 
        (v_merchant_id, cat_fruits, 'FRUIT001', '智利进口车厘子 JJJ级', '5kg礼盒装，个大脆甜', '/static/images/demo/p1.jpg', 299.00, 200, 200, 1),
        (v_merchant_id, cat_fruits, 'FRUIT002', '泰国金枕榴莲', '整个约3-4kg，包熟包甜', '/static/images/demo/p2.jpg', 189.00, 100, 100, 1),
        (v_merchant_id, cat_fruits, 'FRUIT003', '阳光玫瑰青提', '香印葡萄，无籽脆甜', '/static/images/demo/p3.jpg', 49.90, 300, 300, 1),
        (v_merchant_id, cat_fruits, 'FRUIT004', '新疆阿克苏冰糖心苹果', '5kg家庭装，带糖心', '/static/images/demo/p4.jpg', 39.90, 500, 500, 1),
        (v_merchant_id, cat_fruits, 'FRUIT005', '海南红心火龙果', '大果包邮，花青素满满', '/static/images/demo/p5.jpg', 29.90, 600, 600, 1),
        (v_merchant_id, cat_fruits, 'FRUIT006', '四川不知火丑橘', '果肉饱满，汁多肉嫩', '/static/images/demo/p6.jpg', 35.00, 400, 400, 1);
    END IF;

    -- ---------------------------------------------------------------------------------
    -- 家用电器 (6个)
    -- ---------------------------------------------------------------------------------
    IF cat_appliances IS NOT NULL THEN
        INSERT INTO public.ml_products (merchant_id, category_id, product_code, name, description, main_image_url, base_price, total_stock, available_stock, status) VALUES 
        (v_merchant_id, cat_appliances, 'HOME001', '小米米家扫地机器人', '扫拖一体，激光导航', '/static/images/demo/p1.jpg', 1499.00, 200, 200, 1),
        (v_merchant_id, cat_appliances, 'HOME002', '戴森无叶风扇空气净化器', '去除甲醛，凉风扇', '/static/images/demo/p2.jpg', 4999.00, 50, 50, 1),
        (v_merchant_id, cat_appliances, 'HOME003', '飞利浦空气炸锅', '大容量可视，低脂健康', '/static/images/demo/p3.jpg', 399.00, 300, 300, 1),
        (v_merchant_id, cat_appliances, 'HOME004', '九阳破壁机', '静音免手洗，豆浆果汁', '/static/images/demo/p4.jpg', 499.00, 250, 250, 1),
        (v_merchant_id, cat_appliances, 'HOME005', '索尼75寸4K液晶电视', 'X90L系列，XR认知芯片', '/static/images/demo/p5.jpg', 8999.00, 40, 40, 1),
        (v_merchant_id, cat_appliances, 'HOME006', '海尔双开门冰箱', '500L大容量，一级能效', '/static/images/demo/p6.jpg', 3299.00, 60, 60, 1);
    END IF;
    
    RAISE NOTICE '数据生成完成：已为各分类插入测试商品';
END $$;
