"""DMS event handlers — subscribe to domain events and trigger async tasks.

This module is imported by ``DMSConfig.ready()`` to wire up all subscriptions.
Handlers are intentionally lightweight: they delegate heavy work to Celery tasks.
"""

from __future__ import annotations

from simorgh.apps.events.bus import subscribe


@subscribe("dms.document.published")
def on_document_published(payload: dict) -> None:
    """Trigger preview generation and notify watchers when a document is published."""
    from simorgh.apps.dms.tasks import dms_generate_preview_async

    document_id = payload.get("document_id")
    if document_id:
        dms_generate_preview_async.delay(str(document_id))


@subscribe("dms.version.added")
def on_version_added(payload: dict) -> None:
    """When a new version is added, generate preview and run OCR if enabled."""
    from simorgh.apps.dms.tasks import dms_generate_preview_async, dms_process_ocr_async

    version_id = payload.get("version_id")
    if version_id:
        dms_generate_preview_async.delay(str(version_id))
        dms_process_ocr_async.delay(str(version_id))


@subscribe("dms.checkout.expired")
def on_checkout_expired(payload: dict) -> None:
    """Log or notify when a document checkout lock expires."""
    # Currently a no-op hook — extend as needed for notifications.
    pass
