// cli/commands/doctor.ts — planu doctor: verify MCP installations health (SPEC-236)
import { existsSync } from 'node:fs';
import { homedir } from 'node:os';
import { join } from 'node:path';
import { bold, cyan, green, yellow, red, dim } from '../colors.js';
import { readJsonFile, hasPlanuEntry } from './install.js';
// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
const CHECKS = [
    {
        name: 'Claude Code',
        paths: [join(homedir(), '.claude.json'), join(process.cwd(), '.claude.json')],
        format: 'standard',
    },
    {
        name: 'Cursor',
        paths: [join(homedir(), '.cursor', 'mcp.json'), join(process.cwd(), '.cursor', 'mcp.json')],
        format: 'standard',
    },
    {
        name: 'Windsurf',
        paths: [
            join(homedir(), '.codeium', 'windsurf', 'mcp_config.json'),
            join(process.cwd(), '.windsurf', 'mcp.json'),
        ],
        format: 'standard',
    },
    {
        name: 'Zed',
        paths: [
            join(homedir(), '.config', 'zed', 'settings.json'),
            join(process.cwd(), '.zed', 'settings.json'),
        ],
        format: 'zed',
    },
    {
        name: 'Cline',
        paths: [
            join(homedir(), '.vscode', 'globalStorage', 'saoudrizwan.claude-dev', 'settings', 'cline_mcp_settings.json'),
        ],
        format: 'standard',
    },
    {
        name: 'Continue',
        paths: [
            join(homedir(), '.continue', 'config.json'),
            join(process.cwd(), '.continue', 'config.json'),
        ],
        format: 'standard',
    },
];
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
function checkTool(check) {
    const reports = [];
    for (const configPath of check.paths) {
        if (!existsSync(configPath)) {
            continue; // skip non-existent paths silently
        }
        let config;
        try {
            config = readJsonFile(configPath);
        }
        catch {
            reports.push({
                name: check.name,
                configPath,
                status: 'invalid-config',
                detail: 'Config file contains invalid JSON',
            });
            continue;
        }
        if (hasPlanuEntry(config, check.format)) {
            reports.push({ name: check.name, configPath, status: 'ok' });
        }
        else {
            reports.push({
                name: check.name,
                configPath,
                status: 'not-installed',
                detail: 'Planu MCP entry not found in config',
            });
        }
    }
    return reports;
}
function statusIcon(status) {
    switch (status) {
        case 'ok':
            return green('+');
        case 'not-installed':
            return dim('-');
        case 'invalid-config':
            return yellow('!');
        case 'not-found':
            return dim('?');
    }
}
function statusLabel(status) {
    switch (status) {
        case 'ok':
            return green('OK');
        case 'not-installed':
            return dim('not installed');
        case 'invalid-config':
            return yellow('invalid config');
        case 'not-found':
            return dim('tool not found');
    }
}
function printReports(allReports) {
    for (const report of allReports) {
        const icon = statusIcon(report.status);
        const label = statusLabel(report.status);
        const pathStr = report.configPath ? ` ${dim(report.configPath)}` : '';
        process.stdout.write(`  [${icon}] ${report.name.padEnd(14)} ${label}${pathStr}\n`);
        if (report.detail) {
            process.stdout.write(`       ${dim(report.detail)}\n`);
        }
    }
}
// Exported for tests
export { checkTool };
// ---------------------------------------------------------------------------
// Command
// ---------------------------------------------------------------------------
export const doctorCommand = {
    name: 'doctor',
    description: 'Check Planu MCP installation health',
    usage: 'planu doctor',
    run(_args, _flags) {
        process.stdout.write(`\n${bold('Planu Doctor')}\n\n`);
        const allReports = [];
        for (const check of CHECKS) {
            const reports = checkTool(check);
            if (reports.length === 0) {
                allReports.push({ name: check.name, configPath: '', status: 'not-found' });
            }
            else {
                allReports.push(...reports);
            }
        }
        printReports(allReports);
        const installed = allReports.filter((r) => r.status === 'ok').length;
        const invalid = allReports.filter((r) => r.status === 'invalid-config').length;
        process.stdout.write('\n');
        if (invalid > 0) {
            process.stdout.write(`${yellow('Warning:')} ${String(invalid)} config file(s) have invalid JSON. Run ${cyan('planu install')} to fix.\n`);
        }
        if (installed === 0) {
            process.stdout.write(`${red('Planu is not installed in any AI tool.')} Run ${cyan('planu install')} to set it up.\n\n`);
            process.exitCode = 1;
        }
        else {
            process.stdout.write(`${green('Planu is installed in')} ${bold(String(installed))} ${green('location(s).')} ${dim('Run `planu install` to add more.')}\n\n`);
        }
        return Promise.resolve();
    },
};
//# sourceMappingURL=doctor.js.map