-- =====================================================================================
-- Schema: 抽奖与直播管理表
-- 位置：docs/sql/10_schema/marketing/ak_lottery_live_v1.sql
-- 说明：管理抽奖活动、奖品、主播及直播间，按商家隔离。
-- =====================================================================================

-- 1. 抽奖活动表
CREATE TABLE IF NOT EXISTS public.ak_marketing_lotteries (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    merchant_id UUID NOT NULL REFERENCES public.ak_users(id) ON DELETE CASCADE,
    
    name TEXT NOT NULL,                          -- 活动名称
    type INTEGER DEFAULT 1,                      -- 活动类型: 1积分抽奖, 2订单评价, 3订单支付
    
    start_time TIMESTAMPTZ NOT NULL,             -- 开始时间
    end_time TIMESTAMPTZ NOT NULL,               -- 结束时间
    
    is_open BOOLEAN DEFAULT true,                -- 是否开启
    
    created_at TIMESTAMPTZ DEFAULT now(),
    updated_at TIMESTAMPTZ DEFAULT now()
);

-- 2. 抽奖奖品表
CREATE TABLE IF NOT EXISTS public.ak_marketing_lottery_prizes (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    lottery_id UUID NOT NULL REFERENCES public.ak_marketing_lotteries(id) ON DELETE CASCADE,
    
    name TEXT NOT NULL,                          -- 奖品名称
    prize_type TEXT NOT NULL,                    -- 奖品类型: points, balance, coupon, physical
    amount DECIMAL(12,2) DEFAULT 0,              -- 奖励面值/数量
    stock INTEGER DEFAULT 0,                     -- 奖品库存
    probability DECIMAL(5,2) DEFAULT 0,          -- 中奖概率 (0-100)
    
    sort_order INTEGER DEFAULT 0,
    created_at TIMESTAMPTZ DEFAULT now()
);

-- 3. 直播主播表
CREATE TABLE IF NOT EXISTS public.ak_marketing_live_anchors (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    merchant_id UUID NOT NULL REFERENCES public.ak_users(id) ON DELETE CASCADE,
    
    nickname TEXT NOT NULL,                      -- 主播昵称
    wechat TEXT,                                 -- 微信号
    phone TEXT,                                  -- 联系电话
    avatar_url TEXT,                             -- 头像
    
    status BOOLEAN DEFAULT true,                 -- 状态
    created_at TIMESTAMPTZ DEFAULT now()
);

-- 4. 直播间管理表
CREATE TABLE IF NOT EXISTS public.ak_marketing_live_rooms (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    merchant_id UUID NOT NULL REFERENCES public.ak_users(id) ON DELETE CASCADE,
    anchor_id UUID REFERENCES public.ak_marketing_live_anchors(id) ON DELETE SET NULL,
    
    name TEXT NOT NULL,                          -- 直播间名称
    background_url TEXT,                         -- 背景图
    share_img_url TEXT,                          -- 分享图
    
    start_time TIMESTAMPTZ NOT NULL,             -- 开始时间
    end_time TIMESTAMPTZ NOT NULL,               -- 计划结束时间
    
    sort INTEGER DEFAULT 0,                      -- 排序
    type TEXT DEFAULT 'phone',                   -- 类型: phone手机直播等
    
    like_enabled BOOLEAN DEFAULT true,           -- 开启点赞
    sale_enabled BOOLEAN DEFAULT true,           -- 开启卖货
    comment_enabled BOOLEAN DEFAULT true,        -- 开启评论
    
    is_show BOOLEAN DEFAULT true,                -- 是否显示
    live_status INTEGER DEFAULT 1,               -- 1未开始, 2直播中, 3暂停, 4已结束
    
    created_at TIMESTAMPTZ DEFAULT now(),
    updated_at TIMESTAMPTZ DEFAULT now()
);

-- 5. 启用 RLS
ALTER TABLE public.ak_marketing_lotteries ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.ak_marketing_lottery_prizes ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.ak_marketing_live_anchors ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.ak_marketing_live_rooms ENABLE ROW LEVEL SECURITY;

-- 6. 创建权限策略 (按 merchant_id 隔离)
CREATE POLICY "Merchants manage their own lotteries" ON public.ak_marketing_lotteries FOR ALL TO authenticated USING (merchant_id = auth.uid()) WITH CHECK (merchant_id = auth.uid());
CREATE POLICY "Merchants manage their own anchors" ON public.ak_marketing_live_anchors FOR ALL TO authenticated USING (merchant_id = auth.uid()) WITH CHECK (merchant_id = auth.uid());
CREATE POLICY "Merchants manage their own rooms" ON public.ak_marketing_live_rooms FOR ALL TO authenticated USING (merchant_id = auth.uid()) WITH CHECK (merchant_id = auth.uid());

-- 允许查看
CREATE POLICY "Users can view lotteries" ON public.ak_marketing_lotteries FOR SELECT TO authenticated USING (is_open = true);
CREATE POLICY "Users can view active rooms" ON public.ak_marketing_live_rooms FOR SELECT TO authenticated USING (is_show = true);
