-- Migration: reading_list and article_analytics tables
-- Timestamp: 20260312053000

-- ============================================================
-- 1. TABLES
-- ============================================================

-- Reading List: stores articles saved by users
CREATE TABLE IF NOT EXISTS public.reading_list (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    user_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
    article_slug TEXT NOT NULL,
    article_title TEXT NOT NULL,
    article_excerpt TEXT,
    article_image TEXT,
    article_image_alt TEXT,
    article_category TEXT,
    article_author TEXT,
    article_read_time TEXT,
    article_date TEXT,
    saved_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
);

-- Article Analytics: tracks views, read time, and engagement
CREATE TABLE IF NOT EXISTS public.article_analytics (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    article_slug TEXT NOT NULL,
    article_title TEXT NOT NULL,
    user_id UUID REFERENCES auth.users(id) ON DELETE SET NULL,
    session_id TEXT,
    event_type TEXT NOT NULL,
    read_time_seconds INTEGER DEFAULT 0,
    scroll_depth INTEGER DEFAULT 0,
    referrer TEXT,
    created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
);

-- ============================================================
-- 2. INDEXES
-- ============================================================

CREATE INDEX IF NOT EXISTS idx_reading_list_user_id ON public.reading_list(user_id);
CREATE INDEX IF NOT EXISTS idx_reading_list_article_slug ON public.reading_list(article_slug);
CREATE UNIQUE INDEX IF NOT EXISTS idx_reading_list_user_article ON public.reading_list(user_id, article_slug);

CREATE INDEX IF NOT EXISTS idx_article_analytics_slug ON public.article_analytics(article_slug);
CREATE INDEX IF NOT EXISTS idx_article_analytics_user_id ON public.article_analytics(user_id);
CREATE INDEX IF NOT EXISTS idx_article_analytics_event_type ON public.article_analytics(event_type);
CREATE INDEX IF NOT EXISTS idx_article_analytics_created_at ON public.article_analytics(created_at);

-- ============================================================
-- 3. ENABLE RLS
-- ============================================================

ALTER TABLE public.reading_list ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.article_analytics ENABLE ROW LEVEL SECURITY;

-- ============================================================
-- 4. RLS POLICIES
-- ============================================================

-- Reading List: users manage their own saved articles
DROP POLICY IF EXISTS "users_manage_own_reading_list" ON public.reading_list;
CREATE POLICY "users_manage_own_reading_list"
ON public.reading_list
FOR ALL
TO authenticated
USING (user_id = auth.uid())
WITH CHECK (user_id = auth.uid());

-- Article Analytics: anyone can insert (track views), users can read own data
DROP POLICY IF EXISTS "public_insert_article_analytics" ON public.article_analytics;
CREATE POLICY "public_insert_article_analytics"
ON public.article_analytics
FOR INSERT
TO public
WITH CHECK (true);

DROP POLICY IF EXISTS "users_read_own_article_analytics" ON public.article_analytics;
CREATE POLICY "users_read_own_article_analytics"
ON public.article_analytics
FOR SELECT
TO authenticated
USING (user_id = auth.uid() OR user_id IS NULL);
