-- 1. 为所有现有店铺创建优惠券模板
-- 这个脚本会查找数据库中现有的所有 Shop，并为每个 Shop 创建一系列优惠券

DO $$ 
DECLARE
    r_shop RECORD;
    v_template_id UUID;
BEGIN
    -- 遍历所有状态正常的店铺
    FOR r_shop IN SELECT id, merchant_id, shop_name FROM public.ml_shops WHERE status = 1 LOOP
        
        RAISE NOTICE '正在为店铺: % 创建优惠券模板...', r_shop.shop_name;
        
        -- A. 创建店铺满减券 (满100减10)
        -- 判断是否已存在同名券，避免重复创建
        IF NOT EXISTS (SELECT 1 FROM public.ml_coupon_templates WHERE merchant_id = r_shop.merchant_id AND name = '店铺新人礼 - 满100减10') THEN
            INSERT INTO public.ml_coupon_templates (
                merchant_id, 
                name, 
                coupon_type, 
                discount_type, 
                discount_value, 
                min_order_amount, 
                total_quantity, 
                start_time, 
                end_time, 
                status,
                applicable_products
            ) VALUES (
                r_shop.merchant_id, 
                '店铺新人礼 - 满100减10', 
                1, -- 满减
                1, -- 固定金额
                10.00, 
                100.00, 
                500, -- 发放500张
                now(), 
                now() + interval '1 year', 
                1,
                '[]'::jsonb
            );
        END IF;

        -- B. 创建店铺折扣券 (9.5折, 满200可用)
        IF NOT EXISTS (SELECT 1 FROM public.ml_coupon_templates WHERE merchant_id = r_shop.merchant_id AND name = '全店95折券') THEN
            INSERT INTO public.ml_coupon_templates (
                merchant_id, 
                name, 
                coupon_type, 
                discount_type, 
                discount_value, 
                min_order_amount, 
                total_quantity, 
                start_time, 
                end_time, 
                status,
                applicable_products
            ) VALUES (
                r_shop.merchant_id, 
                '全店95折券', 
                1, -- 满减/折扣
                2, -- 百分比
                0.95, -- 95折
                200.00, 
                1000, 
                now(), 
                now() + interval '1 year', 
                1,
                '[]'::jsonb
            );
        END IF;
        
        -- C. 创建大额满减券 (满500减60)
         IF NOT EXISTS (SELECT 1 FROM public.ml_coupon_templates WHERE merchant_id = r_shop.merchant_id AND name = '店庆大促 - 满500减60') THEN
            INSERT INTO public.ml_coupon_templates (
                merchant_id, 
                name, 
                coupon_type, 
                discount_type, 
                discount_value, 
                min_order_amount, 
                total_quantity, 
                start_time, 
                end_time, 
                status,
                applicable_products
            ) VALUES (
                r_shop.merchant_id, 
                '店庆大促 - 满500减60', 
                1, 
                1, 
                60.00, 
                500.00, 
                200, 
                now(), 
                now() + interval '30 days', 
                1,
                '[]'::jsonb
            );
        END IF;

    END LOOP;

    -- D. 创建几个平台通用券 (如果不存在)
    IF NOT EXISTS (SELECT 1 FROM public.ml_coupon_templates WHERE merchant_id IS NULL AND name = '平台春季大促红包') THEN
        INSERT INTO public.ml_coupon_templates (
            merchant_id, 
            name, 
            coupon_type, 
            discount_type, 
            discount_value, 
            min_order_amount, 
            total_quantity, 
            start_time, 
            end_time, 
            status,
            applicable_products
        ) VALUES (
            NULL, 
            '平台春季大促红包', 
            1, 
            1, 
            8.00, 
            0, 
            10000, 
            now(), 
            now() + interval '3 months', 
            1,
            '[]'::jsonb
        );
    END IF;

    RAISE NOTICE '所有现有店铺优惠券模板创建完成。';

END $$;
