// cli/commands/uninstall.ts — planu uninstall: remove Planu MCP from AI tools (SPEC-236)
import { existsSync } from 'node:fs';
import { homedir } from 'node:os';
import { join } from 'node:path';
import { bold, green, yellow, dim, red } from '../colors.js';
import { confirm } from '../interactive.js';
import { readJsonFile, hasPlanuEntry, removeEntry } from './install.js';
// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
const ALL_PATHS = [
    {
        name: 'Claude Code (user)',
        configPath: join(homedir(), '.claude.json'),
        format: 'standard',
    },
    {
        name: 'Claude Code (project)',
        configPath: join(process.cwd(), '.claude.json'),
        format: 'standard',
    },
    {
        name: 'Cursor (user)',
        configPath: join(homedir(), '.cursor', 'mcp.json'),
        format: 'standard',
    },
    {
        name: 'Cursor (project)',
        configPath: join(process.cwd(), '.cursor', 'mcp.json'),
        format: 'standard',
    },
    {
        name: 'Windsurf (user)',
        configPath: join(homedir(), '.codeium', 'windsurf', 'mcp_config.json'),
        format: 'standard',
    },
    {
        name: 'Windsurf (project)',
        configPath: join(process.cwd(), '.windsurf', 'mcp.json'),
        format: 'standard',
    },
    {
        name: 'Zed (user)',
        configPath: join(homedir(), '.config', 'zed', 'settings.json'),
        format: 'zed',
    },
    {
        name: 'Zed (project)',
        configPath: join(process.cwd(), '.zed', 'settings.json'),
        format: 'zed',
    },
    {
        name: 'Cline',
        configPath: join(homedir(), '.vscode', 'globalStorage', 'saoudrizwan.claude-dev', 'settings', 'cline_mcp_settings.json'),
        format: 'standard',
    },
    {
        name: 'Continue (user)',
        configPath: join(homedir(), '.continue', 'config.json'),
        format: 'standard',
    },
    {
        name: 'Continue (project)',
        configPath: join(process.cwd(), '.continue', 'config.json'),
        format: 'standard',
    },
];
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
function findInstalledLocations() {
    return ALL_PATHS.filter((loc) => {
        if (!existsSync(loc.configPath)) {
            return false;
        }
        try {
            const config = readJsonFile(loc.configPath);
            return hasPlanuEntry(config, loc.format);
        }
        catch {
            return false;
        }
    });
}
// Exported for tests
export { findInstalledLocations };
// ---------------------------------------------------------------------------
// Command
// ---------------------------------------------------------------------------
export const uninstallCommand = {
    name: 'uninstall',
    description: 'Remove Planu MCP from AI tool configs',
    usage: 'planu uninstall [--yes]',
    async run(args, _flags) {
        const forceYes = args.includes('--yes') || args.includes('-y');
        process.stdout.write(`\n${bold('Planu MCP Uninstaller')}\n\n`);
        const locations = findInstalledLocations();
        if (locations.length === 0) {
            process.stdout.write(`${dim('Planu is not installed in any detected AI tool configs.')}\n\n`);
            return;
        }
        process.stdout.write(`Found Planu installed in ${String(locations.length)} location(s):\n\n`);
        for (const loc of locations) {
            process.stdout.write(`  ${yellow('-')} ${loc.name} ${dim(loc.configPath)}\n`);
        }
        process.stdout.write('\n');
        const proceed = forceYes || (await confirm('Remove Planu from all listed locations?', false));
        if (!proceed) {
            process.stdout.write(`${dim('Cancelled.')}\n\n`);
            return;
        }
        let removed = 0;
        let failed = 0;
        for (const loc of locations) {
            try {
                removeEntry(loc.configPath, loc.format);
                process.stdout.write(`  ${green('-')} Removed from ${bold(loc.name)}\n`);
                removed++;
            }
            catch (err) {
                const msg = err instanceof Error ? err.message : String(err);
                process.stdout.write(`  ${red('!')} Failed to update ${loc.name}: ${msg}\n`);
                failed++;
            }
        }
        process.stdout.write(`\n${bold('Done.')} Removed from ${String(removed)} location(s).`);
        if (failed > 0) {
            process.stdout.write(` ${yellow(String(failed) + ' failed.')}`);
        }
        process.stdout.write('\n\n');
    },
};
//# sourceMappingURL=uninstall.js.map