-- Add helper function for cleanup edge function

CREATE OR REPLACE FUNCTION public.get_orphaned_edit_history()
RETURNS TABLE (id UUID) AS $$
BEGIN
  RETURN QUERY
  SELECT ieh.id
  FROM item_edit_history ieh
  WHERE NOT EXISTS (
    SELECT 1 FROM submission_items si
    WHERE si.id = ieh.item_id
  );
END;
$$ LANGUAGE plpgsql SECURITY DEFINER SET search_path = public;