import { configCommand } from './config-command.js';
import { ErrorCode, formatCliError } from './errors.js';
import { getPythonRuntimePaths, installPythonRuntime, printPythonRuntimeDoctor, removePythonRuntime, } from './python-runtime.js';
const REGISTRY = [];
function findRegistryEntry(name) {
    return REGISTRY.find((entry) => entry.name === name);
}
function buildServerConfig(entry) {
    const paths = getPythonRuntimePaths(entry.name, entry.runtime);
    return {
        command: paths.executablePath,
        args: entry.serverArgs,
        ...(entry.serverEnv ? { env: entry.serverEnv } : {}),
        ...(entry.agentDescription
            ? { pibo: { description: entry.agentDescription, descriptionSource: 'registry' } }
            : {}),
    };
}
function printRegistryHelp() {
    console.log(`
MCP server registry

Commands:
  pibo mcp registry list                         List built-in MCP server presets
  pibo mcp registry show <name>                  Show one preset
  pibo mcp registry doctor <name>                Check runtime prerequisites
  pibo mcp registry install <name>               Install setup deps and add preset to mcp_servers.json
  pibo mcp registry install <name> --no-setup    Only add preset to mcp_servers.json
  pibo mcp registry remove <name>                Remove config and local runtime
  pibo mcp registry help                         Show this help
`);
}
function printEntry(entry) {
    const paths = getPythonRuntimePaths(entry.name, entry.runtime);
    console.log(`${entry.name}`);
    console.log(`  ${entry.description}`);
    console.log('');
    console.log('Runtime:');
    console.log(`  package: ${entry.runtime.packageName}`);
    console.log(`  python: ${entry.runtime.pythonVersion}`);
    console.log(`  path: ${paths.rootDir}`);
    console.log('');
    console.log('Server config:');
    console.log(JSON.stringify(buildServerConfig(entry), null, 2));
    if (entry.notes.length > 0) {
        console.log('');
        console.log('Notes:');
        for (const note of entry.notes) {
            console.log(`  - ${note}`);
        }
    }
}
async function installEntry(entry, options) {
    if (options.runSetup !== false) {
        await installPythonRuntime(entry.name, entry.runtime);
    }
    await configCommand({
        action: 'add',
        name: entry.name,
        serverJson: JSON.stringify(buildServerConfig(entry)),
        configPath: options.configPath,
    });
}
export async function registryCommand(options) {
    if (options.action === 'help') {
        printRegistryHelp();
        return;
    }
    if (options.action === 'list') {
        if (REGISTRY.length === 0) {
            console.log('No registry entries are currently bundled.');
            return;
        }
        for (const entry of REGISTRY) {
            console.log(`${entry.name}\t${entry.description}`);
        }
        return;
    }
    if (!options.name) {
        throw new Error(formatCliError({
            code: ErrorCode.CLIENT_ERROR,
            type: 'MISSING_ARGUMENT',
            message: `registry ${options.action} requires <name>`,
            suggestion: 'Run pibo mcp registry list to see available presets.',
        }));
    }
    const entry = findRegistryEntry(options.name);
    if (!entry) {
        throw new Error(formatCliError({
            code: ErrorCode.CLIENT_ERROR,
            type: 'MCP_REGISTRY_ENTRY_NOT_FOUND',
            message: `Registry entry "${options.name}" not found`,
            details: REGISTRY.length > 0
                ? `Available entries: ${REGISTRY.map((item) => item.name).join(', ')}`
                : 'No registry entries are currently bundled.',
        }));
    }
    if (options.action === 'show') {
        printEntry(entry);
        return;
    }
    if (options.action === 'doctor') {
        await printPythonRuntimeDoctor(entry.name, entry.runtime);
        return;
    }
    if (options.action === 'install') {
        await installEntry(entry, options);
        return;
    }
    await configCommand({
        action: 'remove',
        name: entry.name,
        configPath: options.configPath,
    });
    await removePythonRuntime(entry.name, entry.runtime);
}
