{% from "govuk_frontend_jinja/components/summary-list/macro.html" import govukSummaryList %}
{% from "govuk_frontend_jinja/components/tag/macro.html" import govukTag %}
{% from "govuk_frontend_jinja/components/details/macro.html" import govukDetails %}

{% macro submission_answers(helper, should_show_answers_diff=False) %}
  {% for form in helper.get_ordered_visible_forms() %}
    <h2 id="form-{{ form.id }}" class="govuk-heading-m govuk-!-margin-top-9">{{ form.title }}</h2>
    {% if helper.get_status_for_form(form) == enum.tasklist_section_status.NOT_NEEDED %}
      <p class="govuk-body">Section was not needed.</p>
    {% else %}
      {{ answers_summary_list(helper, form, should_show_answers_diff=should_show_answers_diff) }}
    {% endif %}
  {% endfor %}
{% endmacro %}

{# todo: relies on top level template having `interpolate` and `format_thousands` (for example) #}
{#       we should set all formatters as globally registered filters and set the appropriate interpolator here #}
{% macro answers_summary_list(helper, container, add_another_index=none, summary_title=none, should_show_answers_diff=False) %}
  {# each container context will have its own summary list which we'll populate at the end of this macro #}
  {% set add_another_contexts = [] %}
  {% set rows = [] %}

  {% set is_add_another_group = add_another_index is not none %}
  {% set context = helper.cached_evaluation_context if not is_add_another_group else helper.cached_evaluation_context.with_add_another_context(container, helper.submission.data_manager, add_another_index=add_another_index) %}

  {% for question in helper.cached_get_ordered_visible_questions(container, override_context=context if is_add_another_group else none) %}

    {% if question.add_another_container and not is_add_another_group %}
      {# for the add another context we'll just include one row with a link to the full answers below #}
      {% if question.add_another_container not in (add_another_contexts | map(attribute="container")) %}
        {% set count = helper.get_count_for_add_another(question.add_another_container) %}
        {% set summaries = [] %}
        {% set status = namespace(all_answered = true) %}
        {% for i in range(count) %}
          {% set (summary, is_answered) = helper.get_answer_summary_for_add_another(question.add_another_container, add_another_index=i) %}
          {%
            do summaries.append({
              "summary_text_line": summary,
              "is_answered": is_answered
            })
          %}
          {% if not is_answered %}{% set status.all_answered = false %}{% endif %}
        {% endfor %}
        {%
          do add_another_contexts.append({
            "container": question.add_another_container,
            "count": count,
            "summaries": summaries,
            "all_answered": status.all_answered
          })
        %}

        {% set key_text = interpolate(question.add_another_container.text) %}


        {% set value_html %}
          <p class="govuk-body">There {% trans count=count %}is {{ count }} answer{% pluralize %}are {{ count }} answers{% endtrans %}</p>
          {% if count %}
            <p class="govuk-body">
              <a href="#{{ question.add_another_container.id }}-answers" class="govuk-link govuk-link--no-visited-state">Answers for “{{ question.add_another_container.name }}”</a>
            </p>
          {% endif %}
        {% endset %}
        {%
          do rows.append({
              "key": {"text": key_text },
              "value": {"html": value_html },
          })
        %}
      {% endif %}
    {% else %}
      {% set answer = helper.cached_get_answer_for_question(question.id, add_another_index=add_another_index) %}
      {% set answer_changed = should_show_answers_diff and helper.has_answer_changed_since_previous(question.id, add_another_index=add_another_index) %}
      {% set value_html %}
        {% if answer_changed %}
          <p class="govuk-body">{{ govukTag({"text": "Changed", "classes": "govuk-tag--blue govuk-!-margin-bottom-2"}) }}</p>
        {% endif %}
        {% if answer %}
          {% include answer._render_answer_template %}
        {% else %}
          (Not answered)
        {% endif %}
        {% if answer_changed %}
          {% set original_answer = helper.get_previous_answer_for_question(question.id, add_another_index=add_another_index) %}
          {% set original_answer_html %}
            {% if original_answer %}
              {% set answer = original_answer %}
              {% include answer._render_answer_template %}
            {% else %}
              (Not answered)
            {% endif %}
          {% endset %}
          {{ govukDetails({"summaryText": "Original response", "classes": "govuk-!-margin-top-4","html": original_answer_html}) }}
        {% endif %}
      {% endset %}

      {% if is_add_another_group %}
        {% set context = helper.cached_interpolation_context.with_add_another_context(container, helper.submission.data_manager, add_another_index=add_another_index, mode="interpolation") %}
        {% set key_text = interpolate(question.text, context=context) %}
      {% else %}
        {% set key_text = interpolate(question.text) %}
      {% endif %}
      {%
        do rows.append({
          "key": {
            "text": key_text,
          },
          "value": {
            "html": value_html
          }
        })
      %}
    {% endif %}
  {% endfor %}

  {{
    govukSummaryList({
      "rows": rows,
      "attributes":{"data-testid":container.title if container.title else container.form.title},
      "card": {
        "title": {
          "text": summary_title if summary_title else "Answer " ~ ((add_another_index + 1) if is_add_another_group else "")
        }
      } if is_add_another_group else none
    })
  }}

  {% for add_another_context in add_another_contexts %}
    {% if add_another_context.count %}
      <div class="govuk-!-margin-bottom-9">
        <h3 class="govuk-heading-s" id="{{ add_another_context.container.id }}-answers">{{ add_another_context.container.name }}</h3>

        {% for i in range(add_another_context.count) %}
          {{ answers_summary_list(helper, add_another_context.container, add_another_index=i, summary_title=add_another_context.summaries[i].summary_text_line, should_show_answers_diff=should_show_answers_diff) }}
        {% endfor %}
      </div>
    {% endif %}
  {% endfor %}
{% endmacro %}
