CREATE OR REPLACE FUNCTION public.get_cross_association_blocked_dates(_association_ids uuid[])
RETURNS TABLE(id uuid, start_date timestamptz, end_date timestamptz, all_day boolean)
LANGUAGE sql
STABLE
SECURITY DEFINER
SET search_path = public
AS $$
  SELECT ce.id, ce.start_date, ce.end_date, ce.all_day
  FROM public.calendar_events ce
  WHERE ce.association_id IS NOT NULL
    AND NOT (ce.association_id = ANY(COALESCE(_association_ids, ARRAY[]::uuid[])))
    AND ce.start_date >= (now() - interval '60 days')
$$;

GRANT EXECUTE ON FUNCTION public.get_cross_association_blocked_dates(uuid[]) TO authenticated;