// tools/feedback-handler.ts — Feedback Hub tool handlers (SPEC-188)
import { join } from 'node:path';
import { submitFeedback, getFeedback, resolveFeedback, findSimilar, } from '../storage/feedback-store.js';
import { triageFeedback, formatTriageReport } from '../engine/feedback/triage.js';
// ---------------------------------------------------------------------------
// Default data directory
// ---------------------------------------------------------------------------
function getDataDir(projectPath) {
    const base = projectPath ?? process.cwd();
    return join(base, 'data');
}
// ---------------------------------------------------------------------------
// Type emoji mapping
// ---------------------------------------------------------------------------
const TYPE_EMOJI = {
    bug: 'Bug',
    feature: 'Feature',
    suggestion: 'Suggestion',
    dx: 'DX',
};
// ---------------------------------------------------------------------------
// submit_feedback handler
// ---------------------------------------------------------------------------
export async function handleSubmitFeedback(input) {
    const feedbackType = input.type;
    const dataDir = getDataDir(input.context?.projectPath);
    const entry = await submitFeedback(dataDir, {
        type: feedbackType,
        title: input.title,
        description: input.description,
        context: input.context,
        status: 'pending',
        votes: 1,
    });
    // Check for similar feedback to warn about duplicates
    const similar = findSimilar(dataDir, input.title, 0.4).filter((e) => e.id !== entry.id);
    const topSimilar = similar.sort((a, b) => (b.votes ?? 0) - (a.votes ?? 0))[0];
    const lines = [
        `Feedback submitted: **${entry.id}**`,
        '',
        '| Field | Value |',
        '|-------|-------|',
        `| Type | ${TYPE_EMOJI[feedbackType]} |`,
        `| Title | ${entry.title} |`,
        `| Status | Pending |`,
    ];
    if (topSimilar !== undefined) {
        lines.push('');
        lines.push(`Similar feedback found: ${topSimilar.id} (${topSimilar.votes ?? 1} votes) — consider voting instead of duplicating.`);
    }
    return { content: [{ type: 'text', text: lines.join('\n') }] };
}
// ---------------------------------------------------------------------------
// triage_feedback handler
// ---------------------------------------------------------------------------
export function handleTriageFeedback(input) {
    const dataDir = getDataDir();
    const filterType = input.filter;
    const entries = getFeedback(dataDir, filterType !== undefined ? { type: filterType } : undefined);
    const groups = triageFeedback(entries);
    const report = formatTriageReport(groups);
    const summary = getSummaryLine(entries.length, groups.length);
    const text = [summary, '', report].join('\n');
    return { content: [{ type: 'text', text }] };
}
function getSummaryLine(total, groups) {
    return `${total} feedback item(s) → ${groups} triage group(s)`;
}
// ---------------------------------------------------------------------------
// resolve_feedback handler
// ---------------------------------------------------------------------------
export async function handleResolveFeedback(input) {
    const dataDir = getDataDir();
    const resolved = await resolveFeedback(dataDir, input.feedbackId, input.specId, input.prUrl, input.notes);
    if (resolved === null) {
        return {
            content: [
                {
                    type: 'text',
                    text: `Error: Feedback ${input.feedbackId} not found. Check the ID and try again.`,
                },
            ],
            isError: true,
        };
    }
    const lines = [
        `Feedback resolved: **${resolved.id}**`,
        '',
        '| Field | Value |',
        '|-------|-------|',
        `| Title | ${resolved.title} |`,
        `| Status | Resolved |`,
        `| Resolved At | ${resolved.resolvedAt ?? 'now'} |`,
    ];
    if (resolved.specId) {
        lines.push(`| Linked Spec | ${resolved.specId} |`);
    }
    if (resolved.prUrl) {
        lines.push(`| PR URL | ${resolved.prUrl} |`);
    }
    if (resolved.resolvedNotes) {
        lines.push('', `**Notes:** ${resolved.resolvedNotes}`);
    }
    return { content: [{ type: 'text', text: lines.join('\n') }] };
}
//# sourceMappingURL=feedback-handler.js.map