-- Launch Checklist Progress Table
-- Track user's launch preparation progress

CREATE TABLE IF NOT EXISTS launch_checklist_progress (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    user_id TEXT NOT NULL,
    item_id TEXT NOT NULL,
    completed BOOLEAN DEFAULT FALSE,
    completed_at TIMESTAMPTZ,
    notes TEXT,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW(),
    UNIQUE(user_id, item_id)
);

-- Indexes
CREATE INDEX IF NOT EXISTS idx_checklist_user ON launch_checklist_progress(user_id);
CREATE INDEX IF NOT EXISTS idx_checklist_completed ON launch_checklist_progress(completed) WHERE completed = TRUE;

-- Grant permissions
GRANT SELECT, INSERT, UPDATE ON launch_checklist_progress TO authenticated;
GRANT ALL ON launch_checklist_progress TO service_role;
-- ============================================================
-- RLS for launch_checklist_progress (added by scripts/fix-archived-rls.js)
-- Columns: id, user_id, item_id, completed, completed_at, notes, created_at, updated_at, item_id
-- ============================================================
ALTER TABLE launch_checklist_progress ENABLE ROW LEVEL SECURITY;
ALTER TABLE launch_checklist_progress FORCE ROW LEVEL SECURITY;
DROP POLICY IF EXISTS self_access ON launch_checklist_progress;
CREATE POLICY self_access ON launch_checklist_progress FOR ALL USING (user_id = auth.uid()::text OR is_platform_admin()) WITH CHECK (user_id = auth.uid()::text OR is_platform_admin());
