/**
 * Restart Command
 *
 * Restarts the Ghostflow daemon via launchd.
 */
import { Command, Options } from "@effect/cli";
import { LaunchdService } from "@ghostflow/client";
import { Console, Effect, Option, pipe } from "effect";
import { msg } from "../constants/ansi.js";
import { resolveDaemonPath, validateDaemonPath } from "../resolver.js";
// ============================================================================
// Options
// ============================================================================
const daemonPathOption = Options.text("daemon-path").pipe(Options.withAlias("d"), Options.withDescription("Path to the daemon binary (auto-detected if not specified)"), Options.optional);
// ============================================================================
// Command Implementation
// ============================================================================
const restartHandler = ({ daemonPath }) => pipe(Effect.gen(function* () {
    const launchd = yield* LaunchdService;
    // Resolve daemon path (validate user-provided paths for security)
    const resolvedPath = yield* Option.match(daemonPath, {
        onNone: () => resolveDaemonPath(),
        onSome: (path) => validateDaemonPath(path)
    });
    yield* Console.log("Restarting Ghostflow daemon...");
    // Restart via launchd
    yield* launchd.restart({
        daemonPath: resolvedPath,
        workingDirectory: undefined,
        environment: undefined
    });
    // Verify it started
    yield* Effect.sleep("1 second");
    const status = yield* launchd.status();
    yield* Effect.if(status.isRunning, {
        onTrue: () => Console.log(msg.running(`Ghostflow daemon restarted (PID: ${status.pid})`)),
        onFalse: () => Effect.gen(function* () {
            yield* Console.error(msg.stopped("Daemon failed to restart"));
            yield* Console.error("Check logs at: ~/Library/Logs/ghostflow/daemon.log");
            yield* Effect.fail(new Error("Daemon failed to restart"));
        })
    });
}), Effect.catchAll((error) => Effect.gen(function* () {
    yield* Console.error(msg.error(String(error)));
    return yield* Effect.fail(error);
})));
// ============================================================================
// Command Definition
// ============================================================================
export const restartCommand = Command.make("restart", { daemonPath: daemonPathOption }).pipe(Command.withHandler(restartHandler), Command.withDescription("Restart the Ghostflow daemon"));
//# sourceMappingURL=restart.js.map