import { checkRemoteAutoSession, isAutoActive, isAutoPaused, stopAutoRemote } from "../auto.js";
import { assertSafeDirectory } from "../validate-directory.js";
import { resolveProjectRoot } from "../worktree.js";
import { showNextAction } from "../../shared/tui.js";
import { handleStatus } from "./handlers/core.js";
export function projectRoot() {
    const cwd = process.cwd();
    const root = resolveProjectRoot(cwd);
    if (root !== cwd) {
        assertSafeDirectory(cwd);
    }
    else {
        assertSafeDirectory(root);
    }
    return root;
}
export async function guardRemoteSession(ctx, pi) {
    if (isAutoActive() || isAutoPaused())
        return true;
    const remote = checkRemoteAutoSession(projectRoot());
    if (!remote.running || !remote.pid)
        return true;
    const unitLabel = remote.unitType && remote.unitId
        ? `${remote.unitType} (${remote.unitId})`
        : "unknown unit";
    // In RPC/web bridge mode, interactive TUI prompts (showNextAction) block
    // forever because there is no terminal to answer them. Notify and bail.
    if (process.env.NERF_WEB_BRIDGE_TUI === "1") {
        ctx.ui.notify(`Another auto-mode session (PID ${remote.pid}) is running on this project (${unitLabel}). ` +
            `Stop it first with /nerf stop, or use /nerf steer to redirect it.`, "warning");
        return false;
    }
    const choice = await showNextAction(ctx, {
        title: `Auto-mode is running in another terminal (PID ${remote.pid})`,
        summary: [
            `Currently executing: ${unitLabel}`,
            ...(remote.startedAt ? [`Started: ${remote.startedAt}`] : []),
        ],
        actions: [
            {
                id: "status",
                label: "View status",
                description: "Show the current NERF progress dashboard.",
                recommended: true,
            },
            {
                id: "steer",
                label: "Steer the session",
                description: "Use /nerf steer <instruction> to redirect the running session.",
            },
            {
                id: "stop",
                label: "Stop remote session",
                description: `Send SIGTERM to PID ${remote.pid} to stop it gracefully.`,
            },
            {
                id: "force",
                label: "Force start (steal lock)",
                description: "Start a new session, terminating the existing one.",
            },
        ],
        notYetMessage: "Run /nerf when ready.",
    });
    if (choice === "status") {
        await handleStatus(ctx);
        return false;
    }
    if (choice === "steer") {
        ctx.ui.notify("Use /nerf steer <instruction> to redirect the running auto-mode session.\n" +
            "Example: /nerf steer Use Postgres instead of SQLite", "info");
        return false;
    }
    if (choice === "stop") {
        const result = stopAutoRemote(projectRoot());
        if (result.found) {
            ctx.ui.notify(`Sent stop signal to auto-mode session (PID ${result.pid}). It will shut down gracefully.`, "info");
        }
        else if (result.error) {
            ctx.ui.notify(`Failed to stop remote auto-mode: ${result.error}`, "error");
        }
        else {
            ctx.ui.notify("Remote session is no longer running.", "info");
        }
        return false;
    }
    return choice === "force";
}
