-- Test: simplest possible policies for organizations + org_members
-- Remove is_platform_admin() to isolate the issue

-- Fix organizations SELECT
DROP POLICY IF EXISTS "Members can view their organizations" ON public.organizations;
CREATE POLICY "Members can view their organizations" ON public.organizations
  FOR SELECT TO authenticated
  USING (
    EXISTS (
      SELECT 1 FROM public.org_members
      WHERE org_id = organizations.id
        AND user_id = (select auth.uid())
    )
  );

-- Fix org_members SELECT - use simplest self-referencing pattern
DROP POLICY IF EXISTS "Members can view org members" ON public.org_members;
CREATE POLICY "Members can view org members" ON public.org_members
  FOR SELECT TO authenticated
  USING (
    org_id IN (
      SELECT om.org_id FROM public.org_members om
      WHERE om.user_id = (select auth.uid())
    )
  );
