{% extends "admin/master.html" %}
{% import 'admin/layout.html' as layout with context %}
{% from "govuk_frontend_jinja/components/back-link/macro.html" import govukBackLink %}
{% from "govuk_frontend_jinja/components/task-list/macro.html" import govukTaskList %}
{% from "govuk_frontend_jinja/components/tag/macro.html" import govukTag %}

{% block beforeContent %}
  {% if grant.collections|length == 1 %}
    {{ govukBackLink({"text": "Back", "href": url_for('collection_lifecycle.index') }) }}
  {% else %}
    {{ govukBackLink({"text": "Back", "href": url_for('collection_lifecycle.select_collection', grant_id=grant.id) }) }}
  {% endif %}
{% endblock %}

{% block action_panel %}
  <div class="govuk-grid-row">
    <div class="govuk-grid-column-two-thirds">
      {{ layout.messages() }}

      <h1 class="govuk-heading-l">
        <span class="govuk-caption-l">{{ grant.name }} - {{ collection.name }}</span>
        Collection lifecycle
      </h1>

      <p class="govuk-body">Prepare for a {{ collection.type.constants.singular }} to go live by completing these tasks.</p>

      <h2 class="govuk-heading-m">Platform tasks</h2>
      {% set rows=[] %}
      {%
        do rows.append({
          "title": {"text": "Set up organisations", "classes": "govuk-link--no-visited-state"},
          "status": {"html": govukTag({"text": organisation_count ~ (" organisation" if organisation_count == 1 else " organisations"), "classes": "govuk-tag--blue"})},
          "href": url_for('collection_lifecycle.set_up_organisations', grant_id=grant.id, collection_id=collection.id),
        })
      %}
      {%
        do rows.append({
          "title": {"text": "Set up global certifiers", "classes": "govuk-link--no-visited-state"},
          "status": {"html": govukTag({"text": certifiers_count ~ (" certifier" if certifiers_count == 1 else " certifiers"), "classes": "govuk-tag--blue"})},
          "href": url_for('collection_lifecycle.set_up_global_certifiers', grant_id=grant.id, collection_id=collection.id),
        })
      %}
      {{ govukTaskList({ "idPrefix": "platform-tasks", "items": rows, "attributes": {"id": "platform-tasks"} }) }}

      <h2 class="govuk-heading-m">Grant tasks</h2>
      {% set rows=[] %}

      {% if grant.status == enum.grant_status.DRAFT %}
        {% set status_tag = govukTag({"text": "To do", "classes": "govuk-tag--grey"}) %}
        {% set link_href = url_for('collection_lifecycle.mark_as_onboarding', grant_id=grant.id, collection_id=collection.id) %}
      {% else %}
        {% set status_tag = govukTag({"text": "Completed", "classes": "govuk-tag--green"}) %}
        {% set link_href = "" %}
      {% endif %}
      {%
        do rows.append({
          "title": {"text": "Mark as onboarding with Funding Service", "classes": "govuk-link--no-visited-state"},
          "status": {"html": status_tag},
          "href": link_href,
        })
      %}

      {% if grant.privacy_policy_markdown %}
        {% set privacy_policy_status_tag = govukTag({"text": "Completed", "classes": "govuk-tag--green"}) %}
      {% else %}
        {% set privacy_policy_status_tag = govukTag({"text": "To do", "classes": "govuk-tag--grey"}) %}
      {% endif %}
      {%
        do rows.append({
          "title": {"text": "Set privacy policy", "classes": "govuk-link--no-visited-state"},
          "status": {"html": privacy_policy_status_tag},
          "href": url_for('collection_lifecycle.set_privacy_policy', grant_id=grant.id, collection_id=collection.id),
        })
      %}

      {% if grant.status in [ enum.grant_status.DRAFT, enum.grant_status.ONBOARDING ] %}
        {% if not grant.can_go_live %}
          {% set status_tag = govukTag({"text": "Cannot start yet", "classes": "govuk-tag--cannot-start-yet"}) %}
          {% set link_href = "" %}
        {% else %}
          {% set status_tag = govukTag({"text": "To do", "classes": "govuk-tag--grey"}) %}
          {% set link_href = url_for('collection_lifecycle.make_live', grant_id=grant.id, collection_id=collection.id) %}
        {% endif %}
      {% else %}
        {% set status_tag = govukTag({"text": "Completed", "classes": "govuk-tag--green"}) %}
        {% set link_href = "" %}
      {% endif %}
      {%
        do rows.append({
          "title": {"text": "Make the grant live", "classes": "govuk-link--no-visited-state"},
          "status": {"html": status_tag},
          "href": link_href,
        })
      %}
      {%
        do rows.append({
          "title": {"text": "Set up grant recipients", "classes": "govuk-link--no-visited-state"},
          "status": {"html": govukTag({"text": grant_recipients_count ~ (" grant recipient" if grant_recipients_count == 1 else " grant recipients"), "classes": "govuk-tag--blue"})},
          "href": url_for('collection_lifecycle.set_up_grant_recipients', grant_id=grant.id, collection_id=collection.id),
        })
      %}
      {%
        do rows.append({
          "title": {"text": "Set up grant recipient data providers", "classes": "govuk-link--no-visited-state"},
          "status": {"html": govukTag({"text": grant_recipient_users_count ~ (" data provider" if grant_recipient_users_count == 1 else " data providers"), "classes": "govuk-tag--blue"})},
          "href": url_for('collection_lifecycle.add_individual_data_providers' if collection.is_open else 'collection_lifecycle.add_bulk_data_providers', grant_id=grant.id, collection_id=collection.id),
        })
      %}
      {%
        do rows.append({
          "title": {"text": "Override certifiers for this grant", "classes": "govuk-link--no-visited-state"},
          "status": {"html": govukTag({"text": grant_override_certifiers_count ~ (" override" if grant_override_certifiers_count == 1 else " overrides"), "classes": "govuk-tag--blue"})},
          "href": url_for('collection_lifecycle.override_grant_certifiers', grant_id=grant.id, collection_id=collection.id),
        })
      %}
      {{ govukTaskList({ "idPrefix": "grant-tasks", "items": rows, "attributes": {"id": "grant-tasks"} }) }}

      <h2 class="govuk-heading-m">{{ collection.type.constants.singular | capitalize }} tasks</h2>
      {% set rows=[] %}
      {% set reportingDatesTag %}
        {% if collection.reporting_period_start_date and collection.reporting_period_end_date %}
          {{ govukTag({"text": "Completed" , "classes": "govuk-tag--green"}) }}
        {% else %}
          {{ govukTag({"text": "Optional", "classes": "govuk-tag--blue"}) }}
        {% endif %}
      {% endset %}
      {% if collection.type.value == 'monitoring report' %}
        {%
          do rows.append({
            "title": {"text": "Set reporting dates", "classes": "govuk-link--no-visited-state"},
            "hint": {"text": format_date_range(collection.reporting_period_start_date, collection.reporting_period_end_date)} if collection.reporting_period_start_date and collection.reporting_period_end_date else {},
            "status": {"html": reportingDatesTag},
            "href": url_for('collection_lifecycle.set_reporting_dates', grant_id=grant.id, collection_id=collection.id) if collection.status == enum.collection_status.DRAFT else "",
          })
        %}
      {% endif %}


      {% set submissionDatesTag %}
        {% if collection.submission_period_start_date and collection.submission_period_end_date %}
          {{ govukTag({"text": "Completed", "classes": "govuk-tag--green"}) }}
        {% else %}
          {{ govukTag({"text": "To do", "classes": "govuk-tag--grey"}) }}
        {% endif %}
      {% endset %}
      {%
        do rows.append({
          "title": {"text": "Set submission dates", "classes": "govuk-link--no-visited-state"},
          "hint": {"text": format_date_range(collection.submission_period_start_date, collection.submission_period_end_date)} if collection.submission_period_start_date and collection.submission_period_end_date else {},
          "status": {"html": submissionDatesTag},
          "href": url_for('collection_lifecycle.set_submission_dates', grant_id=grant.id, collection_id=collection.id) if collection.status == enum.collection_status.DRAFT else "",
        })
      %}


      {% set reminderDaysTag %}
        {{ govukTag({"text": collection.reminder_email_business_days_before_closing ~ " business day" ~ ("s" if collection.reminder_email_business_days_before_closing != 1 else "") , "classes": "govuk-tag--blue"}) }}
      {% endset %}
      {%
        do rows.append({
          "title": {"text": "Set reminder email timing", "classes": "govuk-link--no-visited-state"},
          "status": {"html": reminderDaysTag},
          "href": url_for('collection_lifecycle.set_reminder_days', grant_id=grant.id, collection_id=collection.id),
        })
      %}

      {% set dates_are_set = collection.submission_period_start_date and collection.submission_period_end_date %}
      {% set prerequisites_met = grant.status == enum.grant_status.LIVE and grant_recipients_count > 0 and dates_are_set %}
      {% set scheduledReportStatus = {"tag": {"text": "To do", "classes": "govuk-tag--grey"} } %}

      {% if collection.status != enum.collection_status.DRAFT %}
        {% set scheduledReportStatus = {"tag": {"text": "Completed", "classes": "govuk-tag--green"} } %}
      {% elif not prerequisites_met %}
        {% set scheduledReportStatus = {"text": "Cannot start yet", "classes": "govuk-task-list__status--cannot-start-yet"} %}
      {% endif %}
      {%
        do rows.append({
          "title": {"text": "Sign off and lock " + collection.type.constants.singular, "classes": "govuk-link--no-visited-state"},
          "hint": {"text": "The grant must be live, submission dates set, and some grant recipients set up"} if not prerequisites_met else {},
          "status": scheduledReportStatus,
          "href": url_for('collection_lifecycle.schedule_collection', grant_id=grant.id, collection_id=collection.id) if prerequisites_met and collection.status == enum.collection_status.DRAFT else "",
        })
      %}

      {% set makeReportLiveStatus = {"tag": {"text": "To do", "classes": "govuk-tag--grey"} } %}

      {% if collection.status == enum.collection_status.OPEN or collection.status == enum.collection_status.CLOSED %}
        {% set makeReportLiveStatus = {"tag": {"text": "Completed", "classes": "govuk-tag--green"} } %}
      {% elif collection.status != enum.collection_status.SCHEDULED %}
        {% set makeReportLiveStatus = {"text": "Cannot start yet", "classes": "govuk-task-list__status--cannot-start-yet"} %}
      {% endif %}
      {%
        do rows.append({
          "title": {"text": "Open the " + collection.type.constants.singular + " for submissions", "classes": "govuk-link--no-visited-state"},
          "status": makeReportLiveStatus,
          "href": url_for('collection_lifecycle.make_collection_live', grant_id=grant.id, collection_id=collection.id) if collection.status == enum.collection_status.SCHEDULED else "",
        })
      %}

      {% if collection.status == enum.collection_status.CLOSED %}
        {% set sendEmailsStatus = {"tag": {"text": "Completed", "classes": "govuk-tag--green"} } %}
      {% elif collection.status == enum.collection_status.OPEN %}
        {% set sendEmailsStatus = {"tag": {"text": "Do once", "classes": "govuk-tag--orange"}} %}
      {% else %}
        {% set sendEmailsStatus = {"text": "Cannot start yet", "classes": "govuk-task-list__status--cannot-start-yet"} %}
      {% endif %}
      {%
        do rows.append({
          "title": {"text": "Send emails to data providers", "classes": "govuk-link--no-visited-state"},
          "status": sendEmailsStatus,
          "href": url_for('collection_lifecycle.send_emails_to_recipients', grant_id=grant.id, collection_id=collection.id, email_type=enum.report_admin_email_type_enum.COLLECTION_OPEN_NOTIFICATION) if collection.status == enum.collection_status.OPEN else "",
        })
      %}
      {%
        do rows.append({
          "title": {"text": "Send deadline reminder emails", "classes": "govuk-link--no-visited-state"},
          "status": sendEmailsStatus,
          "href": url_for('collection_lifecycle.send_emails_to_recipients', grant_id=grant.id, collection_id=collection.id, email_type=enum.report_admin_email_type_enum.DEADLINE_REMINDER) if collection.status == enum.collection_status.OPEN else "",
        })
      %}

      {% if collection.status == enum.collection_status.CLOSED %}
        {% set sendReportClosedEmailsStatus= {"tag": {"text": "Completed", "classes": "govuk-tag--green"} } %}
      {% elif collection.status == enum.collection_status.OPEN and collection.is_overdue %}
        {% set sendReportClosedEmailsStatus = {"tag": {"text": "Do once", "classes": "govuk-tag--orange"}} %}
      {% else %}
        {% set sendReportClosedEmailsStatus = {"text": "Cannot start yet", "classes": "govuk-task-list__status--cannot-start-yet"} %}
      {% endif %}
      {%
        do rows.append({
          "title": {"text": "Send " ~ collection.type.constants.singular ~ " overdue emails", "classes": "govuk-link--no-visited-state"},
          "status": sendReportClosedEmailsStatus,
          "href": url_for('collection_lifecycle.send_emails_to_recipients', grant_id=grant.id, collection_id=collection.id, email_type=enum.report_admin_email_type_enum.COLLECTION_OVERDUE) if collection.status == enum.collection_status.OPEN and collection.is_overdue else "",
        })
      %}

      {% set closeReportStatus = {"tag": {"text": "To do", "classes": "govuk-tag--grey"} } %}

      {% if collection.status == enum.collection_status.CLOSED %}
        {% set closeReportStatus = {"tag": {"text": "Completed", "classes": "govuk-tag--green"} } %}
      {% elif collection.status != enum.collection_status.OPEN or not collection.is_overdue %}
        {% set closeReportStatus = {"text": "Cannot start yet", "classes": "govuk-task-list__status--cannot-start-yet"} %}
      {% endif %}
      {%
        do rows.append({
          "title": {"text": "Close the " ~ collection.type.constants.singular, "classes": "govuk-link--no-visited-state"},
          "status": closeReportStatus,
          "href": url_for('collection_lifecycle.close_collection', grant_id=grant.id, collection_id=collection.id) if collection.status == enum.collection_status.OPEN and collection.is_overdue else "",
        })
      %}

      {% if collection.status == enum.collection_status.CLOSED %}
        {% set sendReportClosedNotificationStatus = {"tag": {"text": "Do once", "classes": "govuk-tag--orange"}} %}
      {% else %}
        {% set sendReportClosedNotificationStatus = {"text": "Cannot start yet", "classes": "govuk-task-list__status--cannot-start-yet"} %}
      {% endif %}
      {%
        do rows.append({
          "title": {"text": "Send " ~ collection.type.constants.singular ~ " closed emails", "classes": "govuk-link--no-visited-state"},
          "status": sendReportClosedNotificationStatus,
          "href": url_for('collection_lifecycle.send_emails_to_recipients', grant_id=grant.id, collection_id=collection.id, email_type=enum.report_admin_email_type_enum.COLLECTION_CLOSED_NOTIFICATION) if collection.status == enum.collection_status.CLOSED else "",
        })
      %}

      {{ govukTaskList({ "idPrefix": "report-tasks", "items": rows, "attributes": {"id": "report-tasks"} }) }}

      <h2 class="govuk-heading-m">Testing tasks</h2>
      <p class="govuk-body">Set up MHCLG form designers to allow testing the Access grant funding recipient journeys.</p>

      {% set test_rows=[] %}
      {%
        do test_rows.append({
          "title": {"text": "Add form designers to test Access grant funding", "classes": "govuk-link--no-visited-state"},
          "status": {"html": govukTag({"text": (test_users_count|default(0)) ~ (" test user" if (test_users_count|default(0)) == 1 else " test users"), "classes": "govuk-tag--blue"})},
          "href": url_for('collection_lifecycle.set_up_test_grant_recipient_users', grant_id=grant.id, collection_id=collection.id),
        })
      %}
      {{ govukTaskList({ "idPrefix": "testing-tasks", "items": test_rows, "attributes": {"id": "testing-tasks"} }) }}
    </div>
  </div>
{% endblock %}
