// tools/update-status/batch.ts — SPEC-1017 batch status transitions
import { handleUpdateStatus } from './index.js';
import { resolveProjectIdOrAutoDetect } from '../resolve-project-id.js';
function firstText(result) {
    const first = result.content[0];
    return first?.type === 'text' ? first.text : JSON.stringify(result.structuredContent ?? {});
}
export async function handleUpdateStatusBatch(input) {
    const uniqueSpecIds = [...new Set(input.specIds)].sort();
    if (uniqueSpecIds.length === 0) {
        return {
            content: [{ type: 'text', text: 'update_status_batch requires at least one specId.' }],
            isError: true,
        };
    }
    const resolved = await resolveProjectIdOrAutoDetect({
        projectId: input.projectId,
        projectPath: input.projectPath,
    });
    if (!resolved.ok) {
        return resolved.errorResult;
    }
    if (input.dryRun) {
        return {
            content: [
                {
                    type: 'text',
                    text: `Batch status dry run: ${uniqueSpecIds.length} spec(s) would transition to ${input.status}.\n` +
                        uniqueSpecIds.map((id) => `- ${id}`).join('\n'),
                },
            ],
            structuredContent: {
                dryRun: true,
                status: input.status,
                specIds: uniqueSpecIds,
            },
        };
    }
    const updated = [];
    const skipped = [];
    const failed = [];
    const previousSuppress = process.env.PLANU_SUPPRESS_AUTOCOMMIT;
    process.env.PLANU_SUPPRESS_AUTOCOMMIT = 'true';
    try {
        for (const specId of uniqueSpecIds) {
            const result = await handleUpdateStatus({
                specId,
                status: input.status,
                projectId: resolved.projectId,
                projectPath: resolved.projectPath,
                reviewNotes: input.reviewNotes,
            });
            const message = firstText(result);
            if (result.isError === true) {
                if (/already|idempotent|same status/i.test(message)) {
                    skipped.push({ specId, message });
                }
                else {
                    failed.push({ specId, message });
                }
            }
            else {
                updated.push({ specId, message });
            }
        }
    }
    finally {
        if (previousSuppress === undefined) {
            delete process.env.PLANU_SUPPRESS_AUTOCOMMIT;
        }
        else {
            process.env.PLANU_SUPPRESS_AUTOCOMMIT = previousSuppress;
        }
    }
    try {
        const { runStrictPlanuCleanup } = await import('../../engine/spec-migrator/index.js');
        await runStrictPlanuCleanup(resolved.projectPath);
    }
    catch {
        /* best-effort final pass */
    }
    const text = [
        `Batch status transition complete: ${input.status}`,
        `Updated: ${updated.length}`,
        `Skipped: ${skipped.length}`,
        `Failed: ${failed.length}`,
    ].join('\n');
    return {
        content: [{ type: 'text', text }],
        isError: failed.length > 0,
        structuredContent: {
            updated,
            skipped,
            failed,
            sideEffectsFlushed: ['strict-planu-cleanup'],
        },
    };
}
//# sourceMappingURL=batch.js.map