"""Pure transition-labelling helper for the monitoring slice.

Ported from ``mgf-vm``'s ``monitor/events.py``. The engine event
callback does not tell us *who* initiated a power-state transition —
only the new state. For the audit log we infer a best-effort label
from the shape of the transition. This is pure logic over
:class:`~mgf.vm.core.domain.VMStatus`, so it belongs in core even
though the ``EventSubscriber`` that calls it stays adapter-bound.
"""

from __future__ import annotations

from mgf.vm.core.domain import VMStatus


def infer_trigger_source(previous: VMStatus, new: VMStatus) -> str:
    """Pick a short string describing why a transition happened.

    The most useful case is detecting a resumed-from-managed-save
    boot so the events view can show ``"resumed"`` instead of just
    ``"started"``. Falls back to ``"transition"`` for shapes we don't
    have a more specific label for.
    """
    if previous is VMStatus.STOPPED and new is VMStatus.RUNNING:
        return "started"
    if previous is VMStatus.PAUSED and new is VMStatus.RUNNING:
        return "resumed"
    if previous is VMStatus.RUNNING and new is VMStatus.PAUSED:
        return "paused"
    if previous is VMStatus.RUNNING and new is VMStatus.STOPPED:
        return "stopped"
    if new is VMStatus.CRASHED:
        return "crashed"
    return "transition"


__all__ = ["infer_trigger_source"]
