import { buildCommandContext, } from '../../../../app/command-context.js';
import { withScopeOption } from '../../../shared/scope-option.js';
import { confirmAction, } from '../../../shared/shared.prompts.js';
import { readGlobalOptions } from '../../../shared/shared.utils.js';
import { canonicalPathsForPack, setInstalledCanonicalPaths, } from '../../../tools/shared/install-sync-context.js';
import { resolveAssetsRoot } from '../../../../fs/assets.js';
import { resolveProjectRoot, resolveScopeRoot } from '../../../../fs/paths.js';
import { Command } from 'commander';
import { installIdeas as defaultInstallIdeas, } from './install-ideas.js';
const DEFAULT_DEPENDENCIES = {
    buildCommandContext,
    resolveProjectRoot,
    resolveScopeRoot,
    resolveAssetsRoot,
    installIdeas: defaultInstallIdeas,
    confirmAction,
};
function resolveInstallScope(context) {
    return context.scope === 'user' ? 'user' : 'project';
}
function getCountSummary(result) {
    return {
        skills: `copied=${result.copiedSkills.length}, updated=${result.updatedSkills.length}, skipped=${result.skippedSkills.length}`,
        infra: `copied=${result.copiedInfraFiles.length}, updated=${result.updatedInfraFiles.length}, skipped=${result.skippedInfraFiles.length}`,
        templates: `copied=${result.copiedTemplates.length}, updated=${result.updatedTemplates.length}, skipped=${result.skippedTemplates.length}`,
    };
}
function reportSuccess(context, scope, targetRoot, assetsRoot, result) {
    if (context.json) {
        context.logger.json({
            status: 'ok',
            scope,
            targetRoot,
            assetsRoot,
            result,
        });
        return;
    }
    const counts = getCountSummary(result);
    context.logger.info('Installed ideas tool pack.');
    context.logger.info(`Scope: ${scope}`);
    context.logger.info(`Target root: ${targetRoot}`);
    context.logger.info(`Skills: ${counts.skills}`);
    context.logger.info(`Infra files: ${counts.infra}`);
    context.logger.info(`Runtime templates: ${counts.templates}`);
    context.logger.info(`Run: oat sync --scope ${scope}`);
}
async function runInitToolsIdeas(context, options, dependencies) {
    try {
        const scope = resolveInstallScope(context);
        const targetRoot = scope === 'project'
            ? await dependencies.resolveProjectRoot(context.cwd)
            : dependencies.resolveScopeRoot('user', context.cwd, context.home);
        if (options.force && context.interactive) {
            const confirmed = await dependencies.confirmAction(`Force overwrite existing ideas assets in ${scope} scope?`, { interactive: context.interactive });
            if (!confirmed) {
                if (!context.json) {
                    context.logger.info('Cancelled: no files were overwritten.');
                }
                process.exitCode = 0;
                return false;
            }
        }
        const assetsRoot = await dependencies.resolveAssetsRoot();
        const result = await dependencies.installIdeas({
            assetsRoot,
            targetRoot,
            force: options.force,
        });
        reportSuccess(context, scope, targetRoot, assetsRoot, result);
        process.exitCode = 0;
        return true;
    }
    catch (error) {
        const message = error instanceof Error ? error.message : String(error);
        if (context.json) {
            context.logger.json({ status: 'error', message });
        }
        else {
            context.logger.error(message);
        }
        process.exitCode = 1;
        return false;
    }
}
export function createInitToolsIdeasCommand(overrides = {}) {
    const dependencies = {
        ...DEFAULT_DEPENDENCIES,
        ...overrides,
    };
    return withScopeOption(new Command('ideas'))
        .description('Install OAT ideas skills, templates, and idea workflow files')
        .option('--force', 'Overwrite existing files where applicable')
        .action(async (options, command) => {
        const context = dependencies.buildCommandContext(readGlobalOptions(command));
        const didInstall = await runInitToolsIdeas(context, options, dependencies);
        if (didInstall) {
            setInstalledCanonicalPaths(command, canonicalPathsForPack('ideas'));
        }
    });
}
