import path from 'path';
import { findSquadDir } from '../../../core/squad-loader.js';
import { readState, writeState, setSquadStatus } from '../../../core/state-manager.js';
export async function stopCommand(name) {
    const cwd = process.cwd();
    const squadsDir = path.join(cwd, 'squads');
    const squadDir = findSquadDir(squadsDir, name);
    if (!squadDir) {
        console.error(`Squad "${name}" not found.`);
        process.exit(1);
    }
    const state = readState(squadDir);
    if (!state) {
        console.log(`Squad "${name}" is not running (no state.json).`);
        return;
    }
    if (state.status === 'idle' || state.status === 'completed') {
        console.log(`Squad "${name}" is already ${state.status}.`);
        return;
    }
    const updated = setSquadStatus(state, 'idle');
    writeState(squadDir, updated);
    console.log(`Squad "${name}" stopped.`);
}
