class Playbook::PbAdvancedTable::TableBody

def render_row_and_children(row, column_definitions, current_depth, first_parent_child, ancestor_ids = [], top_parent_id = nil, additional_classes: "", table_data_attributes: {})

def render_row_and_children(row, column_definitions, current_depth, first_parent_child, ancestor_ids = [], top_parent_id = nil, additional_classes: "", table_data_attributes: {})
  top_parent_id ||= row.object_id
  new_ancestor_ids = ancestor_ids + [row.object_id]
  leaf_columns = flatten_columns(column_definitions)
  output = ActiveSupport::SafeBuffer.new
  is_first_child_of_subrow = current_depth.positive? && first_parent_child && subrow_headers[current_depth - 1].present?
  subrow_ancestor_ids = ancestor_ids + ["#{row.object_id}sr"]
  subrow_data_attributes = {
    advanced_table_content: subrow_ancestor_ids.join("-"),
    row_depth: current_depth,
    row_parent: "#{id}_#{ancestor_ids.last}",
  }
  # Subrow header if applicable
  output << pb_rails("advanced_table/table_subrow_header", props: { row: row, column_definitions: leaf_columns, depth: current_depth, subrow_header: subrow_headers[current_depth - 1], collapsible_trail: collapsible_trail, classname: "toggle-content", responsive: responsive, subrow_data_attributes: subrow_data_attributes }) if is_first_child_of_subrow && enable_toggle_expansion == "all"
  current_data_attributes = if current_depth.zero?
                              {
                                row_depth: 0,
                                advanced_table_content: row.object_id.to_s,
                                row_parent: nil,
                              }
                            else
                              table_data_attributes
                            end
  # Additional class and data attributes needed for toggle logic
  output << pb_rails("advanced_table/table_row", props: { id: id, row: row, column_definitions: leaf_columns, depth: current_depth, collapsible_trail: collapsible_trail, classname: additional_classes, table_data_attributes: current_data_attributes, responsive: responsive, loading: loading, selectable_rows: selectable_rows, row_id: row[:id], enable_toggle_expansion: enable_toggle_expansion })
  if row[:children].present?
    row[:children].each do |child_row|
      is_first_child = row[:children].first == child_row
      immediate_parent_id = row.object_id
      data_content = new_ancestor_ids.join("-") + "-#{child_row.object_id}"
      child_data_attributes = {
        top_parent: "#{id}_#{top_parent_id}",
        row_depth: current_depth + 1,
        row_parent: "#{id}_#{immediate_parent_id}",
        advanced_table_content: data_content,
      }
      output << render_row_and_children(child_row, column_definitions, current_depth + 1, is_first_child, new_ancestor_ids, top_parent_id, additional_classes: "toggle-content", table_data_attributes: child_data_attributes)
    end
  end
  output
end