-- ============================================================
-- 09_create_bookmarks.sql
-- Tabel bookmark ayat & hadits per user
-- ============================================================

CREATE TABLE IF NOT EXISTS bookmarks (
    id              SERIAL PRIMARY KEY,
    user_id         UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
    content_type    TEXT NOT NULL,                -- 'ayah', 'hadith', 'doa'
    content_key     TEXT NOT NULL,                -- "1:1" (ayah) atau "bukhari-5" (hadith)
    note            TEXT,                         -- Catatan user
    created_at      TIMESTAMPTZ DEFAULT NOW(),

    UNIQUE(user_id, content_type, content_key)
);

-- Index
CREATE INDEX IF NOT EXISTS idx_bookmarks_user ON bookmarks(user_id);
CREATE INDEX IF NOT EXISTS idx_bookmarks_type ON bookmarks(user_id, content_type);

-- RLS
ALTER TABLE bookmarks ENABLE ROW LEVEL SECURITY;

CREATE POLICY "bookmarks_read_own" ON bookmarks
    FOR SELECT USING (auth.uid() = user_id);

CREATE POLICY "bookmarks_insert_own" ON bookmarks
    FOR INSERT WITH CHECK (auth.uid() = user_id);

CREATE POLICY "bookmarks_delete_own" ON bookmarks
    FOR DELETE USING (auth.uid() = user_id);
