-- ============================================================
-- HAYFALA QURAN — Tracker: Habits & Daily Logs
-- ============================================================

-- 1. Default habits (template untuk semua user)
CREATE TABLE IF NOT EXISTS default_habits (
    id          SERIAL PRIMARY KEY,
    name        TEXT NOT NULL,
    subtitle    TEXT,
    category    TEXT NOT NULL DEFAULT 'sunnah', -- sholat, quran, sunnah
    icon        TEXT DEFAULT 'pray',
    sort_order  INTEGER DEFAULT 0,
    is_active   BOOLEAN DEFAULT true,
    created_at  TIMESTAMPTZ DEFAULT NOW()
);

ALTER TABLE default_habits ENABLE ROW LEVEL SECURITY;
DROP POLICY IF EXISTS "default_habits_read_all" ON default_habits;
CREATE POLICY "default_habits_read_all" ON default_habits FOR SELECT USING (true);

-- Seed default habits
INSERT INTO default_habits (name, subtitle, category, icon, sort_order) VALUES
('Sholat Subuh', 'Waktu Subuh', 'sholat', 'mosque', 1),
('Sholat Dzuhur', 'Waktu Dzuhur', 'sholat', 'mosque', 2),
('Sholat Ashar', 'Waktu Ashar', 'sholat', 'mosque', 3),
('Sholat Maghrib', 'Waktu Maghrib', 'sholat', 'mosque', 4),
('Sholat Isya', 'Waktu Isya', 'sholat', 'mosque', 5),
('Baca Al-Quran', 'Min. 1 halaman', 'quran', 'book', 6),
('Dzikir Pagi', 'Setelah Subuh', 'quran', 'sun', 7),
('Dzikir Petang', 'Setelah Ashar', 'quran', 'moon', 8),
('Baca Hadits Harian', 'Sunnah Rasulullah', 'sunnah', 'msg', 9),
('Sedekah Hari Ini', 'Amal jariyah', 'sunnah', 'heart', 10),
('Doa Harian', 'Doa pagi & malam', 'sunnah', 'pray', 11),
('Muroja''ah Hafalan', 'Juz 30', 'sunnah', 'repeat', 12)
ON CONFLICT DO NOTHING;

-- 2. Custom habits per user (tambahan dari user)
CREATE TABLE IF NOT EXISTS user_habits (
    id          SERIAL PRIMARY KEY,
    user_id     UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
    name        TEXT NOT NULL,
    subtitle    TEXT,
    category    TEXT NOT NULL DEFAULT 'custom',
    icon        TEXT DEFAULT 'pray',
    sort_order  INTEGER DEFAULT 100,
    is_active   BOOLEAN DEFAULT true,
    created_at  TIMESTAMPTZ DEFAULT NOW(),
    updated_at  TIMESTAMPTZ DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_user_habits_user ON user_habits(user_id);

ALTER TABLE user_habits ENABLE ROW LEVEL SECURITY;
DROP POLICY IF EXISTS "user_habits_read_own" ON user_habits;
DROP POLICY IF EXISTS "user_habits_insert_own" ON user_habits;
DROP POLICY IF EXISTS "user_habits_update_own" ON user_habits;
DROP POLICY IF EXISTS "user_habits_delete_own" ON user_habits;
CREATE POLICY "user_habits_read_own" ON user_habits FOR SELECT USING (auth.uid() = user_id);
CREATE POLICY "user_habits_insert_own" ON user_habits FOR INSERT WITH CHECK (auth.uid() = user_id);
CREATE POLICY "user_habits_update_own" ON user_habits FOR UPDATE USING (auth.uid() = user_id);
CREATE POLICY "user_habits_delete_own" ON user_habits FOR DELETE USING (auth.uid() = user_id);

-- 3. Daily habit logs (checklist harian)
CREATE TABLE IF NOT EXISTS habit_logs (
    id              SERIAL PRIMARY KEY,
    user_id         UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
    log_date        DATE NOT NULL DEFAULT CURRENT_DATE,
    habit_type      TEXT NOT NULL, -- 'default' atau 'custom'
    habit_ref_id    INTEGER NOT NULL, -- ID dari default_habits atau user_habits
    is_done         BOOLEAN DEFAULT false,
    created_at      TIMESTAMPTZ DEFAULT NOW(),
    UNIQUE(user_id, log_date, habit_type, habit_ref_id)
);

CREATE INDEX IF NOT EXISTS idx_habit_logs_user_date ON habit_logs(user_id, log_date);

ALTER TABLE habit_logs ENABLE ROW LEVEL SECURITY;
DROP POLICY IF EXISTS "habit_logs_read_own" ON habit_logs;
DROP POLICY IF EXISTS "habit_logs_upsert_own" ON habit_logs;
DROP POLICY IF EXISTS "habit_logs_update_own" ON habit_logs;
CREATE POLICY "habit_logs_read_own" ON habit_logs FOR SELECT USING (auth.uid() = user_id);
CREATE POLICY "habit_logs_upsert_own" ON habit_logs FOR INSERT WITH CHECK (auth.uid() = user_id);
CREATE POLICY "habit_logs_update_own" ON habit_logs FOR UPDATE USING (auth.uid() = user_id);

-- ============================================================
-- ✅ Done! Tracker tables ready.
-- ============================================================
