-- =====================================================================================
-- RLS: 营销核心活动表安全策略
-- 位置：docs/sql/20_rls/marketing/ml_marketing_activities_rls_v1.sql
-- 对象类型：RLS 策略
-- 版本：v1
-- 说明：消费者端公开只读；管理端操作由 RPC (SECURITY DEFINER) 承载
-- =====================================================================================

-- 启用 RLS
ALTER TABLE public.ak_seckill_activities ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.ak_combination_activities ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.ak_marketing_bargains ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.ak_marketing_groupbuys ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.ak_marketing_lotteries ENABLE ROW LEVEL SECURITY;

-- 1. 秒杀活动：公开只读
DROP POLICY IF EXISTS seckill_select_policy ON public.ak_seckill_activities;
CREATE POLICY seckill_select_policy ON public.ak_seckill_activities FOR SELECT TO anon, authenticated USING (status = true);

-- 2. 拼团活动：公开只读
DROP POLICY IF EXISTS combination_select_policy ON public.ak_combination_activities;
CREATE POLICY combination_select_policy ON public.ak_combination_activities FOR SELECT TO anon, authenticated USING (status = 'ongoing');

-- 3. 砍价活动：公开只读
DROP POLICY IF EXISTS bargain_select_policy ON public.ak_marketing_bargains;
CREATE POLICY bargain_select_policy ON public.ak_marketing_bargains FOR SELECT TO anon, authenticated USING (status = true);

-- 4. 团购活动：公开只读
DROP POLICY IF EXISTS groupbuy_select_policy ON public.ak_marketing_groupbuys;
CREATE POLICY groupbuy_select_policy ON public.ak_marketing_groupbuys FOR SELECT TO anon, authenticated USING (status = true);

-- 5. 抽奖活动：公开只读
DROP POLICY IF EXISTS lottery_select_policy ON public.ak_marketing_lotteries;
CREATE POLICY lottery_select_policy ON public.ak_marketing_lotteries FOR SELECT TO anon, authenticated USING (is_open = true);

-- 管理端全量管理将由 SECURITY DEFINER 的 RPC 接口执行
