"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createTaskPromoteCommand = createTaskPromoteCommand;
const errors_1 = require("../../../lib/errors");
const output_1 = require("../../../lib/output");
const paths_1 = require("../../../lib/paths");
const global_format_1 = require("../global-format");
const runtime_contract_1 = require("../../../lib/runtime/runtime-contract");
const task_promotion_1 = require("../../../lib/planning/task-promotion");
async function createTaskPromoteCommand() {
    const { Command } = await import("commander");
    const command = new Command("promote");
    command
        .description("Promote a lightweight task workspace into the canonical milestone tasks directory.")
        .requiredOption("--task-definition <path>", "Repository-relative path to the source lightweight task-definition.md file.")
        .option("--dry-run", "Preview source/destination and front matter updates without writing files.")
        .action((_, commandInstance) => {
        const format = (0, output_1.resolveOutputFormat)((0, global_format_1.getGlobalFormat)(commandInstance));
        if (format === null) {
            throw (0, errors_1.createUsageError)("Output format must be one of: text, json.", [`Received: ${String((0, global_format_1.getGlobalFormat)(commandInstance))}`]);
        }
        const repositoryContext = (0, runtime_contract_1.createRepositoryContext)((0, runtime_contract_1.detectRepositoryRoot)(), false);
        const options = commandInstance.opts();
        const result = (0, task_promotion_1.promoteTaskWorkspace)(repositoryContext.root, {
            sourceTaskDefinitionPath: options.taskDefinition,
            dryRun: Boolean(options.dryRun)
        });
        (0, output_1.emitSuccess)({
            command: "task promote",
            format,
            text: renderTaskPromoteText(result, repositoryContext.root),
            data: {
                repositoryRoot: repositoryContext.root,
                detectionSource: repositoryContext.detectionSource,
                dryRun: result.dryRun,
                taskSlug: result.taskSlug,
                paths: {
                    sourceTaskDefinitionPath: result.sourceTaskDefinitionPath,
                    sourceTaskDirectoryPath: (0, paths_1.toRepositoryRelativePath)(repositoryContext.root, result.sourceTaskDirectoryPath),
                    destinationTaskDefinitionPath: result.destinationTaskDefinitionPath,
                    destinationTaskDirectoryPath: (0, paths_1.toRepositoryRelativePath)(repositoryContext.root, result.destinationTaskDirectoryPath),
                    destinationImplementationSummaryPath: result.destinationImplementationSummaryPath
                },
                actions: {
                    taskDirectory: result.taskDirectoryAction,
                    sourceDirectory: result.sourceDirectoryAction
                },
                frontMatterChanges: result.frontMatterChanges
            }
        });
    });
    return command;
}
function renderTaskPromoteText(result, repositoryRoot) {
    const lines = [
        result.dryRun
            ? "Dry-run: no files or directories were written."
            : "Task workspace promoted.",
        `Task slug: ${result.taskSlug}`,
        `Source task definition: ${result.sourceTaskDefinitionPath}`,
        `Source directory: ${(0, paths_1.toRepositoryRelativePath)(repositoryRoot, result.sourceTaskDirectoryPath)} (${result.sourceDirectoryAction})`,
        `Destination task definition: ${result.destinationTaskDefinitionPath}`,
        `Destination directory: ${(0, paths_1.toRepositoryRelativePath)(repositoryRoot, result.destinationTaskDirectoryPath)} (${result.taskDirectoryAction})`,
        `Destination implementation summary: ${result.destinationImplementationSummaryPath}`
    ];
    lines.push("Front matter changes:");
    if (result.frontMatterChanges.length === 0) {
        lines.push("- (none)");
    }
    else {
        for (const change of result.frontMatterChanges) {
            lines.push(`- ${change.filePath}: ${change.field} '${change.before}' -> '${change.after}'`);
        }
    }
    return lines.join("\n");
}
