{"-- Break policy cycle: make chat_threads policies org-based

-- Remove participant-based policies on chat_threads
DROP POLICY IF EXISTS \"Participants can view their threads\" ON public.chat_threads","DROP POLICY IF EXISTS \"Participants can update their threads\" ON public.chat_threads","-- Org members can view threads in their org
CREATE POLICY \"Org members can view org threads\"
ON public.chat_threads
FOR SELECT
USING (
  EXISTS (
    SELECT 1 FROM public.user_roles ur
    WHERE ur.user_id = auth.uid()
      AND ur.organization_id = chat_threads.organization_id
  )
)","-- Org members can update threads in their org
CREATE POLICY \"Org members can update org threads\"
ON public.chat_threads
FOR UPDATE
USING (
  EXISTS (
    SELECT 1 FROM public.user_roles ur
    WHERE ur.user_id = auth.uid()
      AND ur.organization_id = chat_threads.organization_id
  )
)"}