<div class="govuk-summary-card govuk-!-margin-bottom-6">
  <div class="govuk-summary-card__title-wrapper">
    <% if lettings? %>
      <h3 class="govuk-summary-card__title govuk-!-font-weight-regular"><strong>Row <%= row %></strong> <%= tenant_code_html %> <%= property_ref_html %></h3>
    <% else %>
      <h3 class="govuk-summary-card__title govuk-!-font-weight-regular"><strong>Row <%= row %></strong> <%= purchaser_code_html %></h3>
    <% end %>
  </div>

  <div class="govuk-summary-card__content">
    <% potential_errors, critical_errors = bulk_upload_errors.partition { |error| error.category == "soft_validation" } %>

    <% if critical_errors.any? %>
      <h2 class="govuk-heading-m">Critical errors</h2>
      <p class="govuk-body">These errors must be fixed to complete your logs.</p>
      <%= govuk_table(html_attributes: { class: potential_errors.any? ? "" : "no-bottom-border" })  do |table| %>
        <%= table.with_head do |head| %>
          <% head.with_row do |row| %>
            <% row.with_cell(header: true, text: "Cell") %>
            <% row.with_cell(header: true, text: "Question") %>
            <% row.with_cell(header: true, text: "Error") %>
            <% row.with_cell(header: true, text: "Specification") %>
          <% end %>

          <%= table.with_body do |body| %>
            <% critical_errors.each do |error| %>
              <% body.with_row do |row| %>
                  <% row.with_cell(text: error.cell) %>
                  <% row.with_cell(text: question_for_field(error.field), html_attributes: { class: "govuk-!-width-one-half" }) %>
                  <% row.with_cell(text: error.error.html_safe, html_attributes: { class: "govuk-!-font-weight-bold govuk-!-width-one-half" }) %>
                  <% row.with_cell(text: error.field.humanize) %>
              <% end %>
            <% end %>
          <% end %>
        <% end %>
      <% end %>
    <% end %>

    <% if potential_errors.any? %>
      <h2 class="govuk-heading-m">Potential errors</h2>
      <p class="govuk-body">The following groups of cells might have conflicting data. Check the answers and fix any incorrect data.<br><br>If the answers are correct, fix the critical errors and upload the file again. You'll need to confirm that the following data is correct when the file only contains potential errors.</p>
        <%= govuk_table(html_attributes: { class: "no-bottom-border" })  do |table| %>
          <%= table.with_head do |head| %>
            <% head.with_row do |row| %>
              <% row.with_cell(header: true, text: "Cell") %>
              <% row.with_cell(header: true, text: "Question") %>
              <% row.with_cell(header: true, text: "Potential error") %>
              <% row.with_cell(header: true, text: "Specification") %>
            <% end %>
          <% end %>

        <%= table.with_body do |body| %>
          <% potential_errors.group_by(&:error).each_with_index do |(error_message, errors), group_index| %>
            <% total_groups = potential_errors.group_by(&:error).size %>
            <% errors.each_with_index do |error, index| %>
              <% row_class = row_classes(index, errors.size) %>
              <% body.with_row(html_attributes: { class: row_class }) do |row| %>
                <% row.with_cell(text: error.cell) %>
                <% row.with_cell(text: question_for_field(error.field), html_attributes: { class: "govuk-!-width-one-half" }) %>
                <% if index == 0 %>
                  <% cell_class = cell_classes(group_index, total_groups) %>
                  <% row.with_cell(text: error_message.html_safe, rowspan: errors.size, html_attributes: { class: cell_class }) %>
                <% end %>
                <% row.with_cell(text: error.field.humanize) %>
              <% end %>
            <% end %>
          <% end %>
        <% end %>
        <% end %>
    <% end %>
  </div>
</div>