"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildQualityDetailed = buildQualityDetailed;
exports.formatQualityDetailedMarkdown = formatQualityDetailedMarkdown;
const remediation_1 = require("../remediation");
const actions_1 = require("./actions");
const index_1 = require("./index");
const scoring_1 = require("../../scoring");
const tools_unavailable_prose_1 = require("../tools/tools-unavailable-prose");
const finding_context_1 = require("../../explore/finding-context");
function buildQualityDetailed(report, graphContext) {
    const actions = (0, remediation_1.rank)((0, actions_1.buildSlopActions)(report.metrics), report.metrics, (m) => (0, scoring_1.evaluateSpec)(scoring_1.QUALITY_SCORING_SPEC, (0, index_1.qualityMetricsToScoreInput)(m)));
    return {
        ...report,
        schemaVersion: '11',
        actions,
        ...(graphContext ? { graphContext } : {}),
    };
}
function formatQualityDetailedMarkdown(detailed, elapsed) {
    const L = [];
    const m = detailed.metrics;
    L.push('# Code Quality Review — Detailed');
    L.push('');
    L.push(`**Date:** ${detailed.analyzedAt.slice(0, 10)}`);
    L.push(`**Repository:** ${detailed.repo}`);
    L.push(`**Branch:** ${detailed.branch} (${detailed.commitSha})`);
    L.push(`**Slop Score:** ${detailed.slopScore}/100`);
    L.push(`**Schema version:** ${detailed.schemaVersion}`);
    L.push('');
    L.push('---');
    L.push('');
    // Ranked actions
    L.push('## Recommended Actions');
    L.push('');
    if (detailed.actions.length === 0) {
        L.push('No remediation actions suggested — slop score has no actionable deductions.');
    }
    else {
        L.push('Actions are ranked by projected score improvement. Each delta is computed by simulating the scorer with the patched metrics.');
        L.push('');
        L.push('| # | Action | Score Δ | Projected | Evidence |');
        L.push('|---|--------|--------:|----------:|----------|');
        detailed.actions.forEach((a, i) => {
            const evSuffix = a.evidence.length
                ? `${a.evidence.length} item${a.evidence.length === 1 ? '' : 's'}`
                : '—';
            L.push(`| ${i + 1} | ${a.title} | +${a.scoreDelta} | ${a.projectedScore}/100 | ${evSuffix} |`);
        });
        L.push('');
        for (const a of detailed.actions) {
            L.push(`### ${a.title} (+${a.scoreDelta})`);
            L.push('');
            L.push(`- **ID:** \`${a.id}\``);
            L.push(`- **Baseline:** ${a.baselineScore}/100`);
            L.push(`- **Projected:** ${a.projectedScore}/100`);
            if (a.rationale)
                L.push(`- **Why:** ${a.rationale}`);
            if (a.evidence.length) {
                L.push('- **Evidence:**');
                for (const e of a.evidence.slice(0, 10)) {
                    const loc = e.line ? `:${e.line}${e.endLine ? `-${e.endLine}` : ''}` : '';
                    L.push(`  - \`${e.file}${loc}\` — ${e.message || e.rule}`);
                }
                if (a.evidence.length > 10) {
                    L.push(`  - … and ${a.evidence.length - 10} more`);
                }
            }
            L.push('');
        }
    }
    L.push('---');
    L.push('');
    // Top duplicate clones
    if (m.duplication?.topClones && m.duplication.topClones.length > 0) {
        L.push('## Top Duplicate Clones');
        L.push('');
        L.push('| Lines | File A | File B |');
        L.push('|------:|--------|--------|');
        for (const c of m.duplication.topClones) {
            L.push(`| ${c.lines} | \`${c.a.file}:${c.a.startLine}-${c.a.endLine}\` | \`${c.b.file}:${c.b.startLine}-${c.b.endLine}\` |`);
        }
        L.push('');
        L.push('---');
        L.push('');
    }
    // Top offenders — enriched with graph context (module + blast radius)
    // when --graph-context ran, so a reader sees how central each
    // offender file is before deciding to touch it.
    const gc = detailed.graphContext;
    let provenancePrinted = false;
    const offenderProvenance = () => {
        if (gc && !provenancePrinted) {
            L.push((0, finding_context_1.graphContextProvenanceLine)(gc));
            L.push('');
            provenancePrinted = true;
        }
    };
    if (m.topConsoleFiles && m.topConsoleFiles.length > 0) {
        L.push('## Files with Most Console Statements');
        L.push('');
        offenderProvenance();
        if (gc) {
            L.push('| File | Count | Graph context |');
            L.push('|------|------:|----------------|');
            for (const f of m.topConsoleFiles) {
                L.push(`| \`${f.file}\` | ${f.count} | ${(0, finding_context_1.formatGraphContextCell)(gc.contexts[(0, finding_context_1.locationKey)(f.file)])} |`);
            }
        }
        else {
            L.push('| File | Count |');
            L.push('|------|------:|');
            for (const f of m.topConsoleFiles)
                L.push(`| \`${f.file}\` | ${f.count} |`);
        }
        L.push('');
    }
    if (m.topTodoFiles && m.topTodoFiles.length > 0) {
        L.push('## Files with Most TODO/FIXME/HACK');
        L.push('');
        offenderProvenance();
        if (gc) {
            L.push('| File | Count | Graph context |');
            L.push('|------|------:|----------------|');
            for (const f of m.topTodoFiles) {
                L.push(`| \`${f.file}\` | ${f.count} | ${(0, finding_context_1.formatGraphContextCell)(gc.contexts[(0, finding_context_1.locationKey)(f.file)])} |`);
            }
        }
        else {
            L.push('| File | Count |');
            L.push('|------|------:|');
            for (const f of m.topTodoFiles)
                L.push(`| \`${f.file}\` | ${f.count} |`);
        }
        L.push('');
    }
    if (m.staleFiles.length > 0) {
        L.push('## Stale Files Committed to Git');
        L.push('');
        for (const f of m.staleFiles)
            L.push(`- \`${f}\``);
        L.push('');
    }
    L.push('---');
    L.push('');
    L.push(`**Tools used:** ${detailed.toolsUsed.join(', ')}`);
    L.push(...(0, tools_unavailable_prose_1.renderToolsUnavailableLines)(detailed.toolsUnavailable));
    L.push(`**Analysis time:** ${elapsed}s`);
    L.push('');
    L.push('*Generated by [VyuhLabs DXKit](https://www.npmjs.com/package/@vyuhlabs/dxkit) — detailed mode*');
    return L.join('\n');
}
//# sourceMappingURL=detailed.js.map