// tools/version-spec.ts — Handler for the version_spec tool
import { projectDataDir } from '../storage/index.js';
import { readSpecContent } from '../engine/spec-reader.js';
import { createVersion, getLatestVersionTag, suggestNextTag, listVersions, } from '../engine/spec-versioning/index.js';
// SPEC-719: cross-process lock is applied at a higher layer (MCP tool wrapper) for
// version_spec because VersionSpecInput does not carry projectPath. The lock guard
// in update-status/index.ts protects the most common race path.
export async function handleVersionSpec(args) {
    const { projectId, specId, message, author, specPath } = args;
    const dataDir = projectDataDir(projectId);
    const latestTag = await getLatestVersionTag(dataDir, specId);
    const tag = args.tag ?? suggestNextTag(latestTag);
    // Read spec content from disk if specPath provided, otherwise use placeholder
    const content = (await readSpecContent(specPath)) ?? `# ${specId} — version snapshot\n(content not provided)`;
    try {
        const version = await createVersion(dataDir, {
            projectId,
            specId,
            tag,
            content,
            message,
            author,
        });
        const allVersions = await listVersions(dataDir, specId);
        const versionCount = allVersions.length;
        const nextTag = suggestNextTag(version.tag);
        return {
            content: [
                {
                    type: 'text',
                    text: [
                        `Version snapshot created successfully.`,
                        ``,
                        `Spec: ${specId}`,
                        `Tag: ${version.tag}`,
                        `Timestamp: ${version.timestamp}`,
                        message !== undefined ? `Message: ${message}` : null,
                        author !== undefined ? `Author: ${author}` : null,
                        `Total versions: ${versionCount}`,
                        ``,
                        `Next suggested tag: ${nextTag}`,
                    ]
                        .filter((line) => line !== null)
                        .join('\n'),
                },
            ],
        };
    }
    catch (error) {
        const msg = error instanceof Error ? error.message : String(error);
        return {
            content: [{ type: 'text', text: `Error: ${msg}` }],
            isError: true,
        };
    }
}
//# sourceMappingURL=version-spec.js.map