-- ============================================================
-- 04_create_hadiths.sql
-- Tabel hadits dari 9 kitab
-- Sumber: Hadith API (api.hadith.gading.dev)
-- ============================================================

CREATE TABLE IF NOT EXISTS hadiths (
    id              SERIAL PRIMARY KEY,
    book            TEXT NOT NULL,                -- bukhari, muslim, abu-daud, dll
    hadith_number   INTEGER NOT NULL,            -- Nomor hadits dalam kitab
    arabic          TEXT,                         -- Teks Arab
    translation     TEXT NOT NULL,                -- Terjemahan Indonesia
    narrator        TEXT,                         -- Perawi (jika ada)
    created_at      TIMESTAMPTZ DEFAULT NOW(),
    
    UNIQUE(book, hadith_number)
);

-- Index
CREATE INDEX IF NOT EXISTS idx_hadiths_book ON hadiths(book);
CREATE INDEX IF NOT EXISTS idx_hadiths_book_number ON hadiths(book, hadith_number);

-- Full-text search untuk pencarian hadits
CREATE INDEX IF NOT EXISTS idx_hadiths_translation_search 
    ON hadiths USING GIN (to_tsvector('indonesian', translation));

-- RLS: semua boleh baca
ALTER TABLE hadiths ENABLE ROW LEVEL SECURITY;

CREATE POLICY "hadiths_read_all" ON hadiths
    FOR SELECT USING (true);
