DROP POLICY IF EXISTS "Homeowners can view association documents" ON public.documents;

CREATE POLICY "Homeowners can view shared association documents"
ON public.documents
FOR SELECT
TO authenticated
USING (
  public.has_role(auth.uid(), 'homeowner'::public.app_role)
  AND association_id IN (
    SELECT o.association_id
    FROM public.owners o
    WHERE o.user_id = auth.uid()
  )
  AND (
    is_public = true
    OR visibility @> ARRAY['homeowner']::text[]
    OR visibility @> ARRAY['public']::text[]
  )
);