/**
 * olam memory stop — stop the agent-memory Docker container.
 *
 * Phase B substrate: delegates to `MemoryServiceContainerController.stop()`.
 * Also cleans up any residual Phase A legacy state (`~/.olam/memory.pid` +
 * the log file) via `migrateFromHostProcess({ removeLog: true })`.
 *
 * Plan reference: docs/plans/memory-service-as-docker-peripheral/phase-b-tasks.md B2
 */
import { printSuccess, printInfo, printHeader, printWarning } from '../../output.js';
import { MemoryServiceContainerController } from '../memory-service-container.js';
import { migrateFromHostProcess } from '../../lib/memory-host-process-migration.js';
export async function runMemoryStop() {
    printHeader('olam memory stop');
    // Always sweep up legacy state. Stop is the canonical "remove all running
    // agent-memory" verb; we additionally remove the log file here (vs start.ts
    // which leaves it in place — see B5's removeLog flag).
    const migration = migrateFromHostProcess({ removeLog: true });
    if (migration.cleaned) {
        printInfo('migration', migration.summary);
    }
    const controller = new MemoryServiceContainerController();
    const current = controller.status();
    if (current.state === 'missing') {
        printSuccess('no olam-memory-service container present (nothing to stop)');
        return 0;
    }
    if (current.state === 'stopped') {
        printSuccess('olam-memory-service container already stopped');
        return 0;
    }
    try {
        controller.stop();
    }
    catch (err) {
        printWarning(`docker stop failed: ${err instanceof Error ? err.message : String(err)}`);
        return 1;
    }
    printSuccess('olam-memory-service stopped');
    return 0;
}
export function registerMemoryStop(cmd) {
    cmd
        .command('stop')
        .description('Stop the agent-memory Docker container (Phase B substrate; idempotent).')
        .action(async () => {
        const rc = await runMemoryStop();
        if (rc !== 0)
            process.exitCode = rc;
    });
}
//# sourceMappingURL=stop.js.map