-- =====================================================================================
-- Schema Update: ml_products 扩展字段 (物流、营销、高级设置)
-- 位置：docs/sql/10_schema/product/ml_products_ext_v1.sql
-- 说明：补齐商品编辑页 Step 3-6 所需的持久化字段。
-- =====================================================================================

DO $$ 
BEGIN
    -- 1. 物流设置：关联运费模板
    IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'ml_products' AND column_name = 'shipping_template_id') THEN
        ALTER TABLE public.ml_products ADD COLUMN shipping_template_id UUID REFERENCES public.ak_shipping_templates(id) ON DELETE SET NULL;
        COMMENT ON COLUMN public.ml_products.shipping_template_id IS '关联运费模板ID';
    END IF;

    -- 2. 营销设置：赠送积分
    IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'ml_products' AND column_name = 'give_integral') THEN
        ALTER TABLE public.ml_products ADD COLUMN give_integral INTEGER DEFAULT 0;
        COMMENT ON COLUMN public.ml_products.give_integral IS '购买赠送积分';
    END IF;

    -- 3. 高级设置：警戒库存
    IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'ml_products' AND column_name = 'stock_warning') THEN
        ALTER TABLE public.ml_products ADD COLUMN stock_warning INTEGER DEFAULT 10;
        COMMENT ON COLUMN public.ml_products.stock_warning IS '库存报警数值';
    END IF;

    -- 4. 高级设置：虚拟销量
    IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'ml_products' AND column_name = 'virtual_sales') THEN
        ALTER TABLE public.ml_products ADD COLUMN virtual_sales INTEGER DEFAULT 0;
        COMMENT ON COLUMN public.ml_products.virtual_sales IS '虚拟销量（展示用）';
    END IF;

    -- 5. 高级设置：排序
    IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'ml_products' AND column_name = 'sort_order') THEN
        ALTER TABLE public.ml_products ADD COLUMN sort_order INTEGER DEFAULT 0;
        COMMENT ON COLUMN public.ml_products.sort_order IS '商品排序权重';
    END IF;

END $$;
