import { readFile } from 'node:fs/promises';
import { join } from 'node:path';
import { buildCommandContext } from '../../../app/command-context.js';
import { getFrontmatterBlock, getFrontmatterField, parseFrontmatterField, } from '../../shared/frontmatter.js';
import { withScopeOption } from '../../shared/scope-option.js';
import { readGlobalOptions } from '../../shared/shared.utils.js';
import { scanTools } from '../../tools/shared/scan-tools.js';
import { resolveAssetsRoot } from '../../../fs/assets.js';
import { fileExists } from '../../../fs/io.js';
import { resolveProjectRoot, resolveScopeRoot } from '../../../fs/paths.js';
import { Command } from 'commander';
import { runInfoTool, } from './info-tool.js';
async function getToolDetail(tool, scopeRoot) {
    if (tool.type === 'skill') {
        const skillDir = join(scopeRoot, '.agents', 'skills', tool.name);
        const skillMdPath = join(skillDir, 'SKILL.md');
        const exists = await fileExists(skillMdPath);
        if (!exists) {
            return {
                description: null,
                argumentHint: null,
                allowedTools: null,
                userInvocable: false,
            };
        }
        const content = await readFile(skillMdPath, 'utf8');
        const block = getFrontmatterBlock(content);
        if (!block) {
            return {
                description: null,
                argumentHint: null,
                allowedTools: null,
                userInvocable: false,
            };
        }
        return {
            description: getFrontmatterField(block, 'description'),
            argumentHint: getFrontmatterField(block, 'argument-hint'),
            allowedTools: getFrontmatterField(block, 'allowed-tools'),
            userInvocable: getFrontmatterField(block, 'user-invocable') === 'true',
        };
    }
    // Agent
    const agentPath = join(scopeRoot, '.agents', 'agents', `${tool.name}.md`);
    const description = await parseFrontmatterField(agentPath, 'description');
    return {
        description: description || null,
        argumentHint: null,
        allowedTools: null,
        userInvocable: false,
    };
}
const defaultDependencies = {
    scanTools,
    resolveScopeRoot: async (scope, cwd, home) => {
        if (scope === 'project')
            return resolveProjectRoot(cwd);
        return resolveScopeRoot(scope, cwd, home);
    },
    resolveAssetsRoot,
    getToolDetail,
};
export function createToolsInfoCommand(dependencies = defaultDependencies) {
    return withScopeOption(new Command('info'))
        .description('Show details for an installed tool')
        .argument('<name>', 'Tool name')
        .action(async (name, _opts, command) => {
        const globalOptions = readGlobalOptions(command);
        const context = buildCommandContext(globalOptions);
        const result = await runInfoTool(context, name, dependencies);
        if (!result.found) {
            process.exitCode = 1;
        }
    });
}
