import { getDiffStats } from './git-diff-parser.js';
import { analyzeSecuritySurface, analyzeCoverageDelta, analyzeBlastRadius, analyzeReversibility, analyzeSpecCompliance, } from './analyzers.js';
import { execFile } from 'node:child_process';
import { promisify } from 'node:util';
const execFileAsync = promisify(execFile);
async function getCurrentBranch(projectPath) {
    const { stdout } = await execFileAsync('git', ['rev-parse', '--abbrev-ref', 'HEAD'], {
        cwd: projectPath,
    });
    return stdout.trim();
}
async function getDiff(projectPath, branch, baseBranch) {
    const { stdout } = await execFileAsync('git', ['diff', `${baseBranch}...${branch}`], {
        cwd: projectPath,
    });
    return stdout;
}
function buildRecommendation(score) {
    if (score <= 30) {
        return 'auto-merge';
    }
    if (score <= 70) {
        return 'human-review';
    }
    return 'block';
}
function buildHumanSummary(score, recommendation, topReason) {
    const recommendationLabel = recommendation === 'auto-merge'
        ? 'Safe to auto-merge'
        : recommendation === 'human-review'
            ? 'Human review required'
            : 'Blocked — high risk';
    return `Risk score ${score}/100 — ${recommendationLabel}. ${topReason}`;
}
/**
 * Main orchestrator: runs all analyzers, computes weighted risk score,
 * and returns a structured MergeRiskResult.
 */
export async function assessMergeRisk(projectPath, options) {
    const baseBranch = options.baseBranch ?? 'main';
    const branch = options.branch ?? (await getCurrentBranch(projectPath));
    const [diffStats, diff] = await Promise.all([
        getDiffStats(projectPath, branch, baseBranch),
        getDiff(projectPath, branch, baseBranch),
    ]);
    const { filesChanged, linesAdded, linesRemoved, files } = diffStats;
    const securityResult = analyzeSecuritySurface(files, diff);
    const coverageResult = analyzeCoverageDelta(linesAdded, files);
    const blastResult = analyzeBlastRadius(filesChanged, files);
    const reversibilityResult = analyzeReversibility(files, diff);
    const complianceResult = analyzeSpecCompliance(files, options.context);
    const breakdown = [
        {
            category: 'Security Surface',
            score: securityResult.score,
            weight: 0.3,
            reason: securityResult.reason,
        },
        {
            category: 'Coverage Delta',
            score: coverageResult.score,
            weight: 0.25,
            reason: coverageResult.reason,
        },
        {
            category: 'Blast Radius',
            score: blastResult.score,
            weight: 0.2,
            reason: blastResult.reason,
        },
        {
            category: 'Reversibility',
            score: reversibilityResult.score,
            weight: 0.15,
            reason: reversibilityResult.reason,
        },
        {
            category: 'Spec Compliance',
            score: complianceResult.score,
            weight: 0.1,
            reason: complianceResult.reason,
        },
    ];
    const riskScore = Math.round(breakdown.reduce((acc, b) => acc + b.score * b.weight, 0));
    const recommendation = buildRecommendation(riskScore);
    // Top reason = highest weighted contribution
    const topBreakdown = [...breakdown].sort((a, b) => b.score * b.weight - a.score * a.weight)[0];
    const topReason = topBreakdown?.reason ?? 'No significant risk factors';
    const humanSummary = buildHumanSummary(riskScore, recommendation, topReason);
    return {
        riskScore,
        recommendation,
        breakdown,
        filesChanged,
        linesAdded,
        linesRemoved,
        humanSummary,
    };
}
//# sourceMappingURL=index.js.map