{% from "govuk_frontend_jinja/components/tag/macro.html" import govukTag %}

{% macro tasklistSectionStatusTag(status) %}
  {%
    set classMap = {
        enum.tasklist_section_status.CANNOT_START_YET: "govuk-task-list__status--cannot-start-yet",
        enum.tasklist_section_status.NOT_STARTED: "govuk-tag--grey",
        enum.tasklist_section_status.IN_PROGRESS: "govuk-tag--light-blue",
        enum.tasklist_section_status.NO_QUESTIONS: "govuk-tag--red",
        enum.tasklist_section_status.CHANGES_REQUESTED: "govuk-tag--yellow",
    }
  %}
  {% if status == enum.tasklist_section_status.COMPLETED or status == enum.tasklist_section_status.NOT_NEEDED %}
    {{ status }}
  {% elif status == enum.tasklist_section_status.CANNOT_START_YET %}
    <div class="{{ classMap[status] }}">{{ status }}</div>
  {% else %}
    {{ govukTag({"text": status, "classes": classMap[status] }) }}
  {% endif %}
{% endmacro %}

{% macro grantStatusTag(status) %}
  {%
    set classMap = {
        enum.grant_status.DRAFT: "govuk-tag--yellow",
        enum.grant_status.ONBOARDING: "govuk-tag--yellow",
        enum.grant_status.LIVE: "govuk-tag--green",
    }
  %}
  {{ govukTag({"text": status | title, "classes": classMap[status] }) }}
{% endmacro %}

{% macro grantStatusDescription(grant, collection_type) %}
  {% set collections = grant.reports if collection_type == enum.collection_type_enum.MONITORING_REPORT else grant.pre_award_forms %}
  {% if grant.status == enum.grant_status.DRAFT %}
    <span data-testid="grant-status-description"> {{ collection_type.constants.plural | capitalize }} cannot be published until the grant is live. </span>
  {% elif grant.status == enum.grant_status.LIVE and collections | selectattr('status', 'equalto', enum.collection_status.OPEN) | list | length == 0 %}
    <span data-testid="grant-status-description"> There are no {{ collection_type.constants.plural }} live. </span>
  {% endif %}
{% endmacro %}

{% macro reportStatusTag(status) %}
  {%
    set classMap = {
        enum.collection_status.DRAFT: "govuk-tag--yellow",
        enum.collection_status.SCHEDULED: "govuk-tag--blue",
        enum.collection_status.OPEN: "govuk-tag--green",
        enum.collection_status.CLOSED: "govuk-tag--grey",
    }
  %}
  {{ govukTag({"text": status, "classes": classMap[status] }) }}
{% endmacro %}

{% macro multiSubmissionStatusTag(submissions, collection_helper, is_overdue) %}
  {% if collection_helper.is_closed %}
    {% if submissions | selectattr("is_submitted") | list | length == submissions | length %}
      {% set multi_status = enum.submission_status.SUBMITTED %}
    {% elif submissions | selectattr("is_submitted") | list | length > 0 %}
      {% set multi_status = enum.submission_status.PARTIALLY_SUBMITTED %}
    {% else %}
      {% set multi_status = enum.submission_status.NOT_SUBMITTED %}
    {% endif %}
  {% else %}
    {% if submissions | length == 0 %}
      {% set multi_status = enum.submission_status.NOT_STARTED %}
    {% elif submissions | selectattr("status", "equalto", enum.submission_status.NOT_STARTED) | list | length == submissions | length %}
      {% set multi_status = enum.submission_status.NOT_STARTED %}
    {% elif submissions | selectattr("is_submitted") | list | length == submissions | length %}
      {% set multi_status = enum.submission_status.SUBMITTED %}
    {% else %}
      {% set multi_status = enum.submission_status.IN_PROGRESS %}
    {% endif %}
  {% endif %}
  {% if multi_status == enum.submission_status.SUBMITTED %}
    <span class="govuk-body govuk-!-margin-bottom-0">{{ submissions | length }} submitted</span>
  {% else %}
    {{ submissionStatusTag(multi_status, is_overdue) }}
  {% endif %}
{% endmacro %}

{% macro assessmentStatusTag(status) %}
  {%
    set classMap = {
      enum.submission_assessment_status.MARKED_AS_APPROVED: "govuk-tag--green",
      enum.submission_assessment_status.MARKED_AS_REJECTED: "govuk-tag--red",
      enum.submission_assessment_status.NOT_STARTED: "govuk-tag--grey",
    }
  %}
  {{ govukTag({"text": status, "classes": "app-status-tag " ~ classMap[status]}) }}
{% endmacro %}

{% macro submissionStatusTag(status, is_overdue) %}
  {% set show_without_tag = [ enum.submission_status.SUBMITTED, enum.submission_status.SUBMITTED_WITH_CHANGES, enum.submission_status.PARTIALLY_SUBMITTED ] %}
  {%
    set classMap = {
        enum.submission_status.NOT_STARTED: "govuk-tag--grey",
        enum.submission_status.IN_PROGRESS: "govuk-tag--light-blue",
        enum.submission_status.READY_TO_SUBMIT: "govuk-tag--blue",
        enum.submission_status.AWAITING_SIGN_OFF: "govuk-tag--purple",
        enum.submission_status.NOT_SUBMITTED: "govuk-tag--orange",
        enum.submission_status.CHANGES_REQUESTED: "govuk-tag--yellow",
    }
  %}
  {% set status_text = status ~ (" (Overdue)" if is_overdue and status != enum.submission_status.NOT_SUBMITTED else "") %}
  {% if status in show_without_tag %}
    <span class="govuk-body govuk-!-margin-bottom-0">{{ status_text }}</span>
  {% else %}
    {{ govukTag({"text": status_text, "classes": "app-status-tag " ~ (classMap[status] if not is_overdue or status == enum.submission_status.NOT_SUBMITTED else "govuk-tag--yellow") }) }}
  {% endif %}
{% endmacro %}
