// tools/comments-handler.ts — Spec Comments tool handlers (SPEC-282)
import { join } from 'node:path';
import { addComment, listComments, resolveComment, replyComment, } from '../storage/comments-store.js';
// ---------------------------------------------------------------------------
// Data directory helper
// ---------------------------------------------------------------------------
function getDataDir(projectPath) {
    return join(projectPath, 'data');
}
// ---------------------------------------------------------------------------
// add_comment handler
// ---------------------------------------------------------------------------
export async function handleAddComment(input) {
    const dataDir = getDataDir(input.projectPath);
    const comment = await addComment(dataDir, input.projectPath, input.specId, {
        author: input.author,
        text: input.text,
        acIndex: input.acIndex,
        blocking: input.blocking,
    });
    const lines = [
        `Comment added: **${comment.id}**`,
        '',
        '| Field | Value |',
        '|-------|-------|',
        `| Spec | ${comment.specId} |`,
        `| Author | ${comment.author} |`,
        `| Blocking | ${comment.blocking ? 'Yes' : 'No'} |`,
    ];
    if (comment.acIndex !== undefined) {
        lines.push(`| AC Index | ${comment.acIndex} |`);
    }
    lines.push('', `> ${comment.text}`);
    return { content: [{ type: 'text', text: lines.join('\n') }] };
}
// ---------------------------------------------------------------------------
// list_comments handler
// ---------------------------------------------------------------------------
export function handleListComments(input) {
    const dataDir = getDataDir(input.projectPath);
    const filter = input.resolved !== undefined ? { resolved: input.resolved } : undefined;
    const comments = listComments(dataDir, input.projectPath, input.specId, filter);
    if (comments.length === 0) {
        const filterLabel = input.resolved === true ? ' resolved' : input.resolved === false ? ' open' : '';
        return {
            content: [
                {
                    type: 'text',
                    text: `No${filterLabel} comments found for **${input.specId}**.`,
                },
            ],
        };
    }
    const lines = [`**${comments.length} comment(s)** for ${input.specId}:`, ''];
    for (const c of comments) {
        const status = c.resolved ? 'Resolved' : c.blocking ? 'Open (blocking)' : 'Open';
        const anchor = c.acIndex !== undefined ? ` [AC-${c.acIndex}]` : '';
        const reply = c.parentId !== undefined ? ` (reply to ${c.parentId})` : '';
        lines.push(`**${c.id}**${anchor}${reply} — ${c.author} @ ${c.createdAt.slice(0, 10)}`);
        lines.push(`Status: ${status}`);
        lines.push(`> ${c.text}`);
        lines.push('');
    }
    return { content: [{ type: 'text', text: lines.join('\n') }] };
}
// ---------------------------------------------------------------------------
// resolve_comment handler
// ---------------------------------------------------------------------------
export async function handleResolveComment(input) {
    const dataDir = getDataDir(input.projectPath);
    const resolved = await resolveComment(dataDir, input.projectPath, input.specId, input.commentId);
    if (resolved === null) {
        return {
            content: [
                {
                    type: 'text',
                    text: `Error: Comment ${input.commentId} not found in spec ${input.specId}. Check the ID and try again.`,
                },
            ],
            isError: true,
        };
    }
    const lines = [
        `Comment resolved: **${resolved.id}**`,
        '',
        '| Field | Value |',
        '|-------|-------|',
        `| Spec | ${resolved.specId} |`,
        `| Author | ${resolved.author} |`,
        `| Resolved At | ${resolved.resolvedAt ?? 'now'} |`,
        '',
        `> ${resolved.text}`,
    ];
    return { content: [{ type: 'text', text: lines.join('\n') }] };
}
// ---------------------------------------------------------------------------
// reply_comment handler
// ---------------------------------------------------------------------------
export async function handleReplyComment(input) {
    const dataDir = getDataDir(input.projectPath);
    const reply = await replyComment(dataDir, input.projectPath, input.specId, {
        parentId: input.parentId,
        author: input.author,
        text: input.text,
    });
    if (reply === null) {
        return {
            content: [
                {
                    type: 'text',
                    text: `Error: Parent comment ${input.parentId} not found in spec ${input.specId}. Check the ID and try again.`,
                },
            ],
            isError: true,
        };
    }
    const lines = [
        `Reply added: **${reply.id}** (thread: ${reply.parentId ?? ''})`,
        '',
        '| Field | Value |',
        '|-------|-------|',
        `| Spec | ${reply.specId} |`,
        `| Author | ${reply.author} |`,
        `| Parent | ${reply.parentId ?? ''} |`,
        '',
        `> ${reply.text}`,
    ];
    return { content: [{ type: 'text', text: lines.join('\n') }] };
}
//# sourceMappingURL=comments-handler.js.map