import { buildCommandContext } from '../../../app/command-context.js';
import { DOCS_SKILLS } from '../../init/tools/docs/install-docs.js';
import { IDEA_SKILLS } from '../../init/tools/ideas/install-ideas.js';
import { RESEARCH_SKILLS } from '../../init/tools/research/install-research.js';
import { UTILITY_SKILLS } from '../../init/tools/utility/install-utility.js';
import { WORKFLOW_SKILLS } from '../../init/tools/workflows/install-workflows.js';
import { withScopeOption } from '../../shared/scope-option.js';
import { confirmAction, } from '../../shared/shared.prompts.js';
import { readGlobalOptions } from '../../shared/shared.utils.js';
import { Command } from 'commander';
import { createDefaultRemoveSkillDependencies, runRemoveSkill, } from '../skill/remove-skill.js';
const PACK_SKILLS = {
    ideas: IDEA_SKILLS,
    docs: DOCS_SKILLS,
    workflows: WORKFLOW_SKILLS,
    utility: UTILITY_SKILLS,
    research: RESEARCH_SKILLS,
};
function createDependencies() {
    return {
        buildCommandContext,
        confirmAction,
        runRemoveSkill,
        removeSkillDependencies: createDefaultRemoveSkillDependencies(),
    };
}
function isPackName(value) {
    return (value === 'ideas' ||
        value === 'docs' ||
        value === 'workflows' ||
        value === 'utility' ||
        value === 'research');
}
export function createRemoveSkillsCommand(overrides = {}) {
    const dependencies = {
        ...createDependencies(),
        ...overrides,
    };
    return withScopeOption(new Command('skills'))
        .description('Remove installed skills by pack')
        .requiredOption('--pack <pack>', 'Skill pack to remove (ideas|docs|workflows|utility|research)')
        .option('--dry-run', 'Preview removal without applying')
        .action(async (options, command) => {
        const context = dependencies.buildCommandContext(readGlobalOptions(command));
        try {
            const rawPack = (options.pack ?? '').toLowerCase();
            if (!isPackName(rawPack)) {
                throw new Error(`Invalid pack: ${options.pack}. Expected one of: ideas, docs, workflows, utility, research.`);
            }
            const skills = [...PACK_SKILLS[rawPack]];
            if (context.interactive && skills.length > 3) {
                const confirmed = await dependencies.confirmAction(`Remove ${skills.length} skills from pack '${rawPack}'?`, { interactive: context.interactive });
                if (!confirmed) {
                    if (!context.json) {
                        context.logger.info('Removal cancelled.');
                    }
                    process.exitCode = 0;
                    return;
                }
            }
            let removedCount = 0;
            let skippedCount = 0;
            const executionContext = context.json
                ? { ...context, json: false }
                : context;
            for (const skillName of skills) {
                const removed = await dependencies.runRemoveSkill(executionContext, skillName, options.dryRun ?? false, dependencies.removeSkillDependencies);
                if (removed) {
                    removedCount += 1;
                }
                else {
                    skippedCount += 1;
                }
            }
            if (context.json) {
                context.logger.json({
                    status: 'ok',
                    pack: rawPack,
                    removedCount,
                    skippedCount,
                });
            }
            else {
                context.logger.info(`Pack '${rawPack}' processed. Removed: ${removedCount}. Skipped: ${skippedCount}.`);
            }
            process.exitCode = 0;
        }
        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 = 2;
        }
    });
}
