-- =====================================================================================
-- Schema: 付费会员管理相关表
-- 位置：docs/sql/10_schema/marketing/ak_member_management_v1.sql
-- 说明：管理会员卡类型、权益内容及基础配置，按商家隔离。
-- =====================================================================================

-- 1. 会员卡类型表
CREATE TABLE IF NOT EXISTS public.ak_marketing_member_types (
    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,                          -- 会员名 (如: 月卡, 年卡)
    duration_days INTEGER DEFAULT 30,            -- 有效期（天），0表示永久
    price DECIMAL(12,2) NOT NULL DEFAULT 0,      -- 原价
    discount_price DECIMAL(12,2) NOT NULL DEFAULT 0, -- 优惠价/实际支付价
    
    is_open BOOLEAN DEFAULT true,                -- 是否开启
    sort_order INTEGER DEFAULT 0,                -- 排序
    
    created_at TIMESTAMPTZ DEFAULT now(),
    updated_at TIMESTAMPTZ DEFAULT now()
);

-- 2. 会员权益表
CREATE TABLE IF NOT EXISTS public.ak_marketing_member_rights (
    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,                          -- 权益名称
    description TEXT,                            -- 权益简介
    icon_url TEXT,                               -- 权益图标
    
    is_show BOOLEAN DEFAULT true,                -- 是否展示
    sort_order INTEGER DEFAULT 0,                -- 排序
    
    created_at TIMESTAMPTZ DEFAULT now(),
    updated_at TIMESTAMPTZ DEFAULT now()
);

-- 3. 会员基础配置表 (每个商家一条记录)
CREATE TABLE IF NOT EXISTS public.ak_marketing_member_config (
    id TEXT PRIMARY KEY DEFAULT 'member_config',
    merchant_id UUID NOT NULL UNIQUE REFERENCES public.ak_users(id) ON DELETE CASCADE,
    
    is_enabled BOOLEAN DEFAULT true,             -- 是否开启付费会员功能
    bg_img_url TEXT,                             -- 会员期内背景图
    expire_bg_img_url TEXT,                      -- 会员到期背景图
    rules_description TEXT,                      -- 会员规则说明文本
    
    updated_at TIMESTAMPTZ DEFAULT now()
);

-- 4. 启用 RLS
ALTER TABLE public.ak_marketing_member_types ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.ak_marketing_member_rights ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.ak_marketing_member_config ENABLE ROW LEVEL SECURITY;

-- 5. 创建权限策略 (按 merchant_id 隔离)
-- 商家管理自己的数据
CREATE POLICY "Merchants manage their own member types" ON public.ak_marketing_member_types FOR ALL TO authenticated USING (merchant_id = auth.uid()) WITH CHECK (merchant_id = auth.uid());
CREATE POLICY "Merchants manage their own member rights" ON public.ak_marketing_member_rights FOR ALL TO authenticated USING (merchant_id = auth.uid()) WITH CHECK (merchant_id = auth.uid());
CREATE POLICY "Merchants manage their own member config" ON public.ak_marketing_member_config FOR ALL TO authenticated USING (merchant_id = auth.uid()) WITH CHECK (merchant_id = auth.uid());

-- 允许所有认证用户查看 (移动端展示)
CREATE POLICY "Users can view active member types" ON public.ak_marketing_member_types FOR SELECT TO authenticated USING (is_open = true);
CREATE POLICY "Users can view active member rights" ON public.ak_marketing_member_rights FOR SELECT TO authenticated USING (is_show = true);
CREATE POLICY "Users can view member config" ON public.ak_marketing_member_config FOR SELECT TO authenticated USING (true);

-- 6. 索引
CREATE INDEX IF NOT EXISTS idx_member_types_merchant ON public.ak_marketing_member_types(merchant_id);
CREATE INDEX IF NOT EXISTS idx_member_rights_merchant ON public.ak_marketing_member_rights(merchant_id);
