"""SP4 showcasing Celery tasks.

Schedule via Celery beat as needed; the queue handlers stay thin and delegate
the heavy lifting to ``services``.
"""
from __future__ import annotations

import logging

from celery import shared_task

logger = logging.getLogger(__name__)


@shared_task(name="smehub.showcasing.expire_deal_rooms")
def expire_deal_rooms_task() -> int:
    """FRSME-ISD016 — flip OPEN deal rooms past their expiry to EXPIRED."""
    from apps.smehub.showcasing.services import expire_deal_rooms

    count = expire_deal_rooms()
    if count:
        logger.info("smehub.showcasing.expire_deal_rooms expired %s rooms", count)
    return count


@shared_task(name="smehub.showcasing.warn_expiring_deal_rooms")
def warn_expiring_deal_rooms_task(days_before: int = 3) -> int:
    """FRSME-ISD016 alt-flow A3 follow-up — email both parties when an OPEN
    deal room is approaching its expiry. Audit flagged the absence of an
    early warning. Schedule via Celery beat to run daily.
    """
    from apps.smehub.showcasing.services import warn_expiring_deal_rooms

    count = warn_expiring_deal_rooms(days_before=days_before)
    if count:
        logger.info(
            "smehub.showcasing.warn_expiring_deal_rooms warned on %s rooms (T-%sd)",
            count, days_before,
        )
    return count
