import { Args, Flags } from '@oclif/core';
import inquirer from 'inquirer';
import { colors, format } from '../../lib/colors.js';
import { getWorkspaceInfo, removeAgentsFromWorkspace, formatAgentList } from '../../lib/agents/commands.js';
import { PMOCommand, pmoBaseFlags } from '../../lib/pmo/index.js';
import { shouldOutputJson, outputPromptAsJson, outputErrorAsJson, createMetadata, buildPromptConfig, } from '../../lib/prompt-json.js';
export default class Remove extends PMOCommand {
    static description = 'Remove a specific agent from the workspace';
    static examples = [
        '<%= config.bin %> <%= command.id %> camry',
        '<%= config.bin %> <%= command.id %>',
    ];
    static args = {
        name: Args.string({
            description: 'Agent name to remove',
            required: false,
        }),
    };
    static flags = {
        ...pmoBaseFlags,
        force: Flags.boolean({
            char: 'f',
            description: 'Skip confirmation prompt (for non-interactive use)',
            default: false,
        }),
    };
    getPMOOptions() {
        return { promptIfMultiple: false };
    }
    async execute() {
        const { args, flags } = await this.parse(Remove);
        // Check if JSON output mode is active
        const jsonMode = shouldOutputJson(flags);
        // Helper to handle errors in JSON mode
        const handleError = (code, message) => {
            if (jsonMode) {
                outputErrorAsJson(code, message, createMetadata('staff remove', flags));
                this.exit(1);
            }
            this.error(message);
        };
        // Get workspace information
        const workspaceInfo = getWorkspaceInfo();
        // Filter to staff (persistent) agents only
        const staffAgents = workspaceInfo.agents.filter(a => a.type === 'persistent');
        if (staffAgents.length === 0) {
            if (jsonMode) {
                outputErrorAsJson('NO_AGENTS', 'No staff agents to remove.', createMetadata('staff remove', flags));
                return;
            }
            this.log(colors.warning('No staff agents to remove.'));
            return;
        }
        let agentName = args.name;
        // Interactive mode if no agent specified
        if (!agentName) {
            // Build choices once, use for both JSON and interactive modes
            const agentChoices = [
                ...staffAgents.map((agent) => ({ name: agent.name, value: agent.name })),
                { name: 'Cancel', value: 'cancel' },
            ];
            const selectMessage = 'Select agent to remove:';
            // In JSON mode, output agent selection prompt
            if (jsonMode) {
                outputPromptAsJson(buildPromptConfig('list', 'name', selectMessage, agentChoices), createMetadata('staff remove', flags));
                return;
            }
            const { selected } = await this.prompt([
                {
                    type: 'list',
                    name: 'selected',
                    message: selectMessage,
                    choices: [
                        ...agentChoices.slice(0, -1),
                        new inquirer.Separator(),
                        { name: '❌ ' + agentChoices[agentChoices.length - 1].name, value: agentChoices[agentChoices.length - 1].value }
                    ]
                }
            ], null);
            if (selected === 'cancel') {
                this.log(colors.textMuted('Operation cancelled.'));
                return;
            }
            agentName = selected;
        }
        // Validate agent exists and is a staff agent
        const agent = staffAgents.find((a) => a.name === agentName);
        if (!agent) {
            return handleError('AGENT_NOT_FOUND', `Staff agent "${agentName}" not found. Available: ${formatAgentList(staffAgents)}`);
        }
        const agentsToRemove = [agentName];
        // Skip confirmation if --force flag is passed
        if (!flags.force) {
            // Build choices once, use for both JSON and interactive modes
            const confirmChoices = [
                { name: 'No, cancel', value: 'false' },
                { name: 'Yes, remove agent', value: 'true' },
            ];
            const confirmMessage = `Are you sure you want to remove agent "${agentName}"? This will delete its worktree.`;
            // Confirm removal
            // In JSON mode, output confirmation prompt
            if (jsonMode) {
                outputPromptAsJson(buildPromptConfig('list', 'confirmed', confirmMessage, confirmChoices), createMetadata('staff remove', flags));
                return;
            }
            const { confirm } = await this.prompt([
                {
                    type: 'list',
                    name: 'confirm',
                    message: confirmMessage,
                    choices: [
                        { name: '❌ ' + confirmChoices[0].name, value: false },
                        { name: '⚠️  ' + confirmChoices[1].name, value: true }
                    ],
                    default: 0 // Default to "No, cancel"
                }
            ], null);
            if (!confirm) {
                this.log(colors.textMuted('Removal cancelled.'));
                return;
            }
        }
        // Remove agents
        this.log(colors.primary(`Removing agent "${agentName}"...`));
        const { removed, failed } = await removeAgentsFromWorkspace(workspaceInfo, agentsToRemove);
        // Show results for each agent
        for (const agentName of agentsToRemove) {
            if (removed.includes(agentName)) {
                this.log(format.success(`Agent ${agentName} removed`));
            }
            else if (failed.includes(agentName)) {
                this.log(format.error(`Failed to remove agent ${agentName}`));
            }
        }
        // Summary
        if (removed.length > 0) {
            this.log(format.success(`\nSuccessfully removed ${removed.length} agent(s): ${removed.join(', ')}`));
        }
        if (failed.length > 0) {
            this.log(format.error(`\nFailed to remove ${failed.length} agent(s): ${failed.join(', ')}`));
        }
    }
}
//# sourceMappingURL=remove.js.map