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 { installWorkflows as defaultInstallWorkflows, } from './install-workflows.js';
const DEFAULT_DEPENDENCIES = {
    buildCommandContext,
    resolveProjectRoot,
    resolveScopeRoot,
    resolveAssetsRoot,
    installWorkflows: defaultInstallWorkflows,
    confirmAction,
};
function reportSuccess(context, scope, targetRoot, assetsRoot, result) {
    if (context.json) {
        context.logger.json({
            status: 'ok',
            scope,
            targetRoot,
            assetsRoot,
            result,
        });
        return;
    }
    context.logger.info('Installed workflows tool pack.');
    context.logger.info(`Scope: ${scope}`);
    context.logger.info(`Target root: ${targetRoot}`);
    context.logger.info(`Skills: copied=${result.copiedSkills.length}, updated=${result.updatedSkills.length}, skipped=${result.skippedSkills.length}`);
    context.logger.info(`Agents: copied=${result.copiedAgents.length}, updated=${result.updatedAgents.length}, skipped=${result.skippedAgents.length}`);
    context.logger.info(`Templates: copied=${result.copiedTemplates.length}, updated=${result.updatedTemplates.length}, skipped=${result.skippedTemplates.length}`);
    context.logger.info(`Scripts: copied=${result.copiedScripts.length}, updated=${result.updatedScripts.length}, skipped=${result.skippedScripts.length}`);
    context.logger.info(`Projects root initialized: ${result.projectsRootInitialized ? 'yes' : 'no'}`);
    context.logger.info(`Run: oat sync --scope ${scope}`);
}
async function runInitToolsWorkflows(context, options, dependencies) {
    try {
        const scope = context.scope === 'user' ? 'user' : 'project';
        const targetRoot = scope === 'user'
            ? dependencies.resolveScopeRoot('user', context.cwd, context.home)
            : await dependencies.resolveProjectRoot(context.cwd);
        if (options.force && context.interactive) {
            const confirmed = await dependencies.confirmAction(`Force overwrite existing workflows 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.installWorkflows({
            assetsRoot,
            targetRoot,
            scope,
            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 createInitToolsWorkflowsCommand(overrides = {}) {
    const dependencies = {
        ...DEFAULT_DEPENDENCIES,
        ...overrides,
    };
    return withScopeOption(new Command('workflows'))
        .description('Install OAT workflows skills, agents, templates, and scripts')
        .option('--force', 'Overwrite existing files where applicable')
        .action(async (options, command) => {
        const context = dependencies.buildCommandContext(readGlobalOptions(command));
        const didInstall = await runInitToolsWorkflows(context, options, dependencies);
        if (didInstall) {
            setInstalledCanonicalPaths(command, canonicalPathsForPack('workflows'));
        }
    });
}
