// tools/github-review-handler.ts — Handler for review_pr tool.
import { createPlatformClient, getRemoteUrl } from '../engine/github/client.js';
import { analyzeDiff, parseDiffStats, generateSummaryFinding, } from '../engine/github/review-analyzer.js';
import { execFile as nodeExecFile } from 'node:child_process';
import { promisify } from 'node:util';
const defaultExec = async (cmd, args) => {
    return promisify(nodeExecFile)(cmd, args, { maxBuffer: 10 * 1024 * 1024 });
};
export async function handleReviewPR(args, exec = defaultExec) {
    const remoteUrl = await getRemoteUrl(exec);
    const client = createPlatformClient(remoteUrl, exec);
    const diff = await client.getPRDiff(args.prNumber);
    const stats = parseDiffStats(diff);
    const reviewType = args.reviewType;
    const findings = analyzeDiff(diff, reviewType);
    const summaryFinding = generateSummaryFinding(stats.filesChanged, stats.additions, stats.deletions);
    const allFindings = [summaryFinding, ...findings];
    const sections = [];
    sections.push(`## Review of PR #${String(args.prNumber)} (${reviewType})\n`);
    sections.push(`**Files changed:** ${String(stats.filesChanged)}`);
    sections.push(`**Lines:** +${String(stats.additions)} -${String(stats.deletions)}\n`);
    if (stats.files.length > 0) {
        sections.push('### Files\n');
        for (const f of stats.files) {
            sections.push(`- ${f}`);
        }
        sections.push('');
    }
    if (findings.length > 0) {
        sections.push('### Findings\n');
        for (const f of findings) {
            const location = f.line ? `${f.file}:${String(f.line)}` : f.file;
            sections.push(`**[${f.severity.toUpperCase()}]** ${location}`);
            sections.push(`  ${f.message}`);
            if (f.suggestion) {
                sections.push(`  Suggestion: ${f.suggestion}`);
            }
            sections.push('');
        }
    }
    else if (reviewType !== 'summary') {
        sections.push(`\nNo ${reviewType} issues found in this PR.`);
    }
    return {
        content: [
            {
                type: 'text',
                text: sections.join('\n'),
            },
        ],
        _meta: {
            prNumber: args.prNumber,
            reviewType,
            filesChanged: stats.filesChanged,
            additions: stats.additions,
            deletions: stats.deletions,
            findingsCount: allFindings.length,
        },
    };
}
//# sourceMappingURL=github-review-handler.js.map