-- Create the Chat History Table CREATE TABLE IF NOT EXISTS public.chat_history ( id UUID DEFAULT uuid_generate_v4() PRIMARY KEY, user_id UUID REFERENCES auth.users(id) ON DELETE CASCADE, query TEXT NOT NULL, answer TEXT NOT NULL, sources JSONB DEFAULT '[]'::jsonb, created_at TIMESTAMP WITH TIME ZONE DEFAULT timezone('utc'::text, now()) NOT NULL ); -- Turn on Row Level Security (RLS) ALTER TABLE public.chat_history ENABLE ROW LEVEL SECURITY; -- Allow users to insert their own chats CREATE POLICY "Users can insert their own chat history" ON public.chat_history FOR INSERT TO authenticated WITH CHECK (auth.uid() = user_id); -- Allow users to view their own chats CREATE POLICY "Users can view their own chat history" ON public.chat_history FOR SELECT TO authenticated USING (auth.uid() = user_id);