{#
  Overrides invenio_jobs' template to name the community a run worked on.

  A run of the Update Archive-It captures job either names one community in its
  arguments or queues a subtask per community, and the mail invenio_jobs sends
  said only that "Update Archive-It captures" had failed -- not for whom. The
  job cannot pass the community in: invenio_jobs builds the mail from the job's
  title and overwrites the run's message with its own progress line.

  Both variables below stay empty for jobs whose runs carry no community, so
  every other job's mail keeps its wording. ext.py says how this file is found
  ahead of invenio_jobs' own, and an instance can still override both.
-#}
{%- set run_community = run.args.get("community") if run.args is mapping else None %}
{%- set failed_communities = [] %}
{%- for subtask in run.subtasks or [] %}
{%- if subtask.status and subtask.status.name != "SUCCESS" and subtask.args is mapping and subtask.args.get("community") %}
{%- set _ = failed_communities.append(subtask.args.get("community")) %}
{%- endif %}
{%- endfor %}
{%- set failed_communities = failed_communities | unique | sort | list %}
{#-
  SPDX-FileCopyrightText: 2026 CERN.
  SPDX-License-Identifier: MIT
-#}
{#
  Run notification plain text email template

  Context variables:
    - job: Job object
    - run: Run object
    - status_info: Dict with title, user_message, action, color
    - run_url: URL to view run details
    - success_count: (optional) Number of successfully processed items
#}
{{ status_info.user_message }}

{{ status_info.action }}
{% if run_community %}
Community: {{ run_community }}
{% endif %}{% if failed_communities %}
Communities that did not finish: {{ failed_communities | join(", ") }}
{% endif %}
View full details: {{ run_url }}

{% if run.status.name == "PARTIAL_SUCCESS" and run.errored_entries and run.total_entries %}
Summary:
- Successfully processed: {{ success_count }} items
- Failed to process: {{ run.errored_entries }} items

{% endif %}
---
Technical Details:
Status: {{ run.status.name }}
Run ID: {{ run.id }}
Job ID: {{ job.id }}
{% if run.started_at %}Started at: {{ run.started_at }}
{% endif %}{% if run.finished_at %}Finished at: {{ run.finished_at }}
{% endif %}{% if (run.status.name == "FAILED" or run.status.name == "PARTIAL_SUCCESS") and run.failed_subtasks %}Failed subtasks: {{ run.failed_subtasks }}/{{ run.total_subtasks }}
{% endif %}
{% if run.message %}
Error details:
{{ run.message }}
{% endif %}
