ALTER TABLE public.messages REPLICA IDENTITY FULL;


-- 2. Drop existing policies

DROP
POLICY IF EXISTS "messages_access" ON public.messages;



-- 3. Create one simple policy for messages

CREATE
POLICY "messages_realtime" ON public.messages

FOR ALL

USING (

    sender_uuid = auth.uid() OR  -- Either you sent it

    thread_id IN (               -- Or you're in the thread

        SELECT thread_id 

        FROM thread_participants 

        WHERE user_uuid = auth.uid()

    )

);

