{% from "common/macros/status.html" import reportStatusTag with context %}
{% from "govuk_frontend_jinja/components/summary-list/macro.html" import govukSummaryList %}
{% from "govuk_frontend_jinja/components/tag/macro.html" import govukTag %}

{% macro collection_card(collection) %}
  {% set can_edit = authorisation_helper.can_edit_collection(current_user, collection.id) %}
  {% set grant = collection.grant %}
  {% set formSectionsText %}
    {% if can_edit %}
      {% if collection.forms | length == 0 %}
        {# TODO: in the next commit the collection type will be included to any url for collection pages like add section, change name, etc. #}
        {# appropriate language for each context will then be swapped out as needed #}
        <a class="govuk-link govuk-link--no-visited-state" href="{{ url_for('deliver_grant_funding.add_section', grant_id=grant.id, collection_type=collection.type, collection_id=collection.id) }}"
          >Add sections<span class="govuk-visually-hidden"> to {{ collection.name }}</span></a
        >
        to create your {{ collection.type.constants.singular }}
        <br />
        Sections are groups of questions with a common theme
      {% else %}
        <a class="govuk-link govuk-link--no-visited-state" href="{{ url_for('deliver_grant_funding.list_collection_sections', grant_id=grant.id, collection_type=collection.type, collection_id=collection.id) }}">
          {% trans count = collection.forms | length %}{{ count }} section{% pluralize %}{{ count }} sections{% endtrans %}
          <span class="govuk-visually-hidden"> in form for {{ collection.name }}</span>
        </a>
      {% endif %}
    {% else %}
      <a class="govuk-link govuk-link--no-visited-state" href="{{ url_for('deliver_grant_funding.list_collection_sections', grant_id=grant.id, collection_type=collection.type, collection_id=collection.id) }}">
        {% trans count = collection.forms | length %}{{ count }} section{% pluralize %}{{ count }} sections{% endtrans %}
        <span class="govuk-visually-hidden"> in form for {{ collection.name }}</span>
      </a>
    {% endif %}
  {% endset %}


  {% set dataSetsHtml %}
    <a class="govuk-link govuk-link--no-visited-state" href="{{ url_for('deliver_grant_funding.list_collection_data_sets', grant_id=grant.id, collection_type=collection.type, collection_id=collection.id) }}">
      {% trans count = collection.data_sources | length %}{{ count }} data set{% pluralize %}{{ count }} data sets{% endtrans %}
      <span class="govuk-visually-hidden"> for {{ collection.name }}</span>
    </a>
    {% if collections_with_missing_data_data_sets and collection.id in collections_with_missing_data_data_sets %}
      {{ govukTag({"text": "Data missing", "classes": "govuk-tag--orange govuk-!-margin-left-1"}) }}
    {% endif %}
  {% endset %}


  {% set liveSubmissionsText %}
    {% trans count = collection.live_submissions | length %}{{ count }} live submission{% pluralize %}{{ count }} live submissions{% endtrans %}
    <span class="govuk-visually-hidden">for {{ collection.name }}</span>
  {% endset %}
  {% set testSubmissionsText %}
    {% trans count = collection.test_submissions | length %}{{ count }} test submission{% pluralize %}{{ count }} test submissions{% endtrans %}
    <span class="govuk-visually-hidden">for {{ collection.name }}</span>
  {% endset %}


  {% set submissionsHtml %}
    <p class="govuk-body">
      <a
        class="govuk-link govuk-link--no-visited-state"
        href="{{ url_for('deliver_grant_funding.list_submissions', grant_id=grant.id, collection_type=collection.type, collection_id=collection.id, submission_mode=enum.submission_mode.LIVE) }}">
        {{ liveSubmissionsText }}
      </a>
    </p>

    {# TODO: hide test submissions link when the reporting round status is live #}
    <p class="govuk-body">
      <a
        class="govuk-link govuk-link--no-visited-state"
        href="{{ url_for('deliver_grant_funding.list_submissions', grant_id=grant.id, collection_type=collection.type, collection_id=collection.id, submission_mode=enum.submission_mode.TEST) }}">
        {{ testSubmissionsText }}
      </a>
    </p>
  {% endset %}


  {% set reportingPeriodText %}
    {% if collection.reporting_period_start_date and collection.reporting_period_end_date %}
      {{ format_date_range(collection.reporting_period_start_date, collection.reporting_period_end_date) }}
    {% else %}
      Not set
    {% endif %}
  {% endset %}


  {% set submissionPeriodText %}
    {% if collection.submission_period_start_date and collection.submission_period_end_date %}
      {{ format_date_range(collection.submission_period_start_date, collection.submission_period_end_date) }}
    {% else %}
      Not set
    {% endif %}
  {% endset %}

  {% set actions = [] %}
  {% do actions.append({"text": "Manage settings" if can_edit else "View settings", "visuallyHiddenText": " for " ~ collection.name, "href": url_for("deliver_grant_funding.collection_settings", grant_id=grant.id, collection_type=collection.type, collection_id=collection.id), "classes": "govuk-link--no-visited-state" }) %}

  {# TODO: see if this logic can be moved out to a macro that can factor in collection type if it needs to (for things like labels) #}
  {{
    govukSummaryList({
        "card": {"title": {"text": collection.name}, "actions": {"items": actions} },
        "rows": [
            {"key": {"text": "Status"}, "value": {"html": reportStatusTag(collection.status)} },
            {"key": {"text": "Form"}, "value": {"text": formSectionsText} },
            {"key": {"text": "Uploaded data sets"}, "value": {"html": dataSetsHtml} },
            {"key": {"text": "Reporting period"}, "value": {"text": reportingPeriodText} } if collection.is_monitoring_collection else {},
            {"key": {"text": "Submission period"}, "value": {"text": submissionPeriodText} },
            {"key": {"text": "Submissions"}, "value": {"html": submissionsHtml} },
            {"key": {"text": "Created by"}, "value": {"text": collection.created_by.email} },
        ],
    })
  }}
{% endmacro %}
