"""Undo command implementation."""

from __future__ import annotations

import sys

from ..data.session import require_session_started
from ..data.undo.checkpoints import undo_last_checkpoint
from ..git_paths import display_path
from ..i18n import _
from ..utils.git_repository import require_git_repository


def command_undo(*, force: bool = False) -> None:
    """Undo the most recent undoable session operation."""
    require_git_repository()
    require_session_started()

    operation = undo_last_checkpoint(force=force)
    print(
        _("✓ Undid: {operation}").format(operation=display_path(operation)),
        file=sys.stderr,
    )
