-- ============================================
-- 此文件由 Meoo Cloud 自动生成，请勿修改
-- This file is auto-generated by Meoo Cloud, DO NOT MODIFY
-- ============================================


-- 创建短链映射表
CREATE TABLE IF NOT EXISTS share_links (
 id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
 short_code TEXT UNIQUE NOT NULL,
 plan_id UUID NOT NULL,
 factory_type TEXT NOT NULL,
 created_by UUID NOT NULL,
 created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
 expires_at TIMESTAMPTZ,
 click_count INTEGER DEFAULT 0
);

-- 创建索引
CREATE INDEX IF NOT EXISTS idx_share_links_short_code ON share_links(short_code);
CREATE INDEX IF NOT EXISTS idx_share_links_plan_id ON share_links(plan_id);

-- 启用 RLS
ALTER TABLE share_links ENABLE ROW LEVEL SECURITY;

-- 添加 RLS 策略
CREATE POLICY users_select_share_links ON share_links
 FOR SELECT
 USING (true);

CREATE POLICY users_insert_share_links ON share_links
 FOR INSERT
 WITH CHECK (created_by = auth.uid());

CREATE POLICY users_delete_own_share_links ON share_links
 FOR DELETE
 USING (created_by = auth.uid());

