"""Launch `devin -r <session-id>` as a subprocess."""

import shutil
import subprocess

from loguru import logger


def resume_session(session_id: str, dry_run: bool = False) -> int:
    """Launch `devin -r <session-id>` as a subprocess.

    Returns the exit code of the devin process, or 0 if dry_run.
    """
    if dry_run:
        print(session_id)
        return 0

    devin_bin = shutil.which("devin")
    if devin_bin is None:
        logger.error("devin CLI not found on PATH")
        return 1

    cmd = [devin_bin, "-r", session_id]
    logger.debug("Launching: {}", cmd)

    # Call devin and let it take over the terminal for the TUI.
    return subprocess.call(cmd)
