<%
  wrestlers_by_id = @tournament.wrestlers.index_by(&:id)
  weights_by_id = @tournament.weights.index_by(&:id)
  mats_by_id = @tournament.mats.index_by(&:id)
  sorted_matches = @tournament.matches.sort_by(&:bout_number)
%>
{
  "tournament": {
    "attributes": <%= @tournament.attributes.to_json %>,
    "schools": <%= @tournament.schools.map(&:attributes).to_json %>,
    "weights": <%= @tournament.weights.map(&:attributes).to_json %>,
    "mats": <%= @tournament.mats.map { |mat| mat.attributes.merge(
      {
        "queue_bout_numbers": mat.queue_matches.map { |match| match&.bout_number }
      }
    ) }.to_json %>,
    "wrestlers": <%= @tournament.wrestlers.map { |wrestler| wrestler.attributes.merge(
      {
        "school": wrestler.school&.attributes,
        "weight": wrestler.weight&.attributes
      }
    ) }.to_json %>,
    "matches": <%= sorted_matches.map { |match| match.attributes.merge(
      {
        "w1_name": wrestlers_by_id[match.w1]&.name,
        "w2_name": wrestlers_by_id[match.w2]&.name,
        "winner_name": wrestlers_by_id[match.winner_id]&.name,
        "weight": weights_by_id[match.weight_id]&.attributes,
        "mat": mats_by_id[match.mat_id]&.attributes
      }
    ) }.to_json %>
  }
}
