import { CONTROL_PLANE_HOST, CONTROL_PLANE_PORT } from "@pocketcoder/shared";
import { isDaemonRunning } from "./start.js";
/**
 * Handle the `pocketcoder stop` CLI command.
 * Sends POST /stop to the control plane to trigger graceful shutdown.
 */
export async function stopCommand() {
    const running = await isDaemonRunning();
    if (!running) {
        console.log("Daemon is not running");
        return;
    }
    try {
        const url = `http://${CONTROL_PLANE_HOST}:${CONTROL_PLANE_PORT}/stop`;
        const response = await fetch(url, {
            method: "POST",
            signal: AbortSignal.timeout(5000),
        });
        if (response.ok) {
            const body = (await response.json());
            if (body.status === "stopping") {
                console.log("Daemon is stopping...");
            }
            else {
                console.log("Stop request sent");
            }
        }
        else {
            console.error(`Failed to stop daemon: HTTP ${response.status}`);
            process.exitCode = 1;
        }
    }
    catch (err) {
        const message = err instanceof Error ? err.message : String(err);
        console.error(`Failed to stop daemon: ${message}`);
        process.exitCode = 1;
    }
}
//# sourceMappingURL=stop.js.map