"""Celery tasks for the M&EL indicators submodule."""
from __future__ import annotations

import logging

from celery import shared_task

logger = logging.getLogger(__name__)


@shared_task(name="mel.indicators.scan_silent_responsibles")
def scan_silent_responsibles_task() -> int:
    """G7 — fire MEL_INDICATOR_SILENT notifications for overdue indicators.

    Wraps :func:`apps.mel.indicators.services.scan_silent_responsibles` so the
    beat schedule can hold a task name rather than a function reference. The
    underlying service is idempotent on (indicator, period_label).
    """
    from apps.mel.indicators.services import scan_silent_responsibles

    return scan_silent_responsibles()


@shared_task(name="mel.indicators.recompute_programme_health")
def recompute_programme_health_task() -> int:
    """G4 — nightly recomputation of programme health scores.

    Walks every ACTIVE log frame and persists a current-period
    :class:`apps.mel.indicators.models.ProgrammeHealthScore`. Idempotent on
    ``(logframe, period_label)``.
    """
    from apps.mel.indicators.services import recompute_health_for_all_active

    return recompute_health_for_all_active()


@shared_task(name="mel.indicators.escalate_alert")
def escalate_alert_task(escalation_id: int) -> str:
    """G13 — countdown-driven escalation tier transition.

    Wraps :func:`apps.mel.indicators.services.escalate_alert` so the
    countdown can be enqueued via ``apply_async(countdown=…)``.
    """
    from apps.mel.indicators.services import escalate_alert

    return escalate_alert(escalation_id)
