"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildTestGapsDetailed = buildTestGapsDetailed;
exports.formatTestGapsDetailedMarkdown = formatTestGapsDetailedMarkdown;
const remediation_1 = require("../remediation");
const actions_1 = require("./actions");
const scoring_1 = require("./scoring");
const tools_unavailable_prose_1 = require("../tools/tools-unavailable-prose");
const finding_context_1 = require("../../explore/finding-context");
function buildTestGapsDetailed(report, graphContext) {
    const counts = (0, actions_1.countsFromReport)(report);
    const actions = (0, remediation_1.rank)((0, actions_1.buildTestGapsActions)(report), counts, scoring_1.scoreTestGapsCounts);
    return {
        ...report,
        schemaVersion: '11',
        coverageScore: (0, scoring_1.scoreTestGapsCounts)(counts).score,
        actions,
        ...(graphContext ? { graphContext } : {}),
    };
}
const TIER_ORDER = { critical: 0, high: 1, medium: 2, low: 3 };
function formatTestGapsDetailedMarkdown(detailed, elapsed) {
    const L = [];
    const s = detailed.summary;
    L.push('# Test Gap Analysis — 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(`**Effective Coverage:** ${s.effectiveCoverage}%`);
    L.push(`**Risk-Weighted Score:** ${detailed.coverageScore}/100`);
    L.push(`**Schema version:** ${detailed.schemaVersion}`);
    L.push('');
    L.push('---');
    L.push('');
    // Summary
    L.push('## Summary');
    L.push('');
    L.push('| Metric | Value |');
    L.push('|--------|------:|');
    L.push(`| Test files | ${s.testFiles} (active: ${s.activeTestFiles}, commented-out: ${s.commentedOutFiles}) |`);
    L.push(`| Source files | ${s.sourceFiles} |`);
    L.push(`| Untested (CRITICAL) | ${s.untestedCritical} |`);
    L.push(`| Untested (HIGH) | ${s.untestedHigh} |`);
    L.push(`| Untested (MEDIUM) | ${s.untestedMedium} |`);
    L.push(`| Untested (LOW) | ${s.untestedLow} |`);
    L.push('');
    L.push('---');
    L.push('');
    // Actions
    L.push('## Recommended Actions');
    L.push('');
    if (detailed.actions.length === 0) {
        L.push('No gaps to close.');
    }
    else {
        L.push('Actions are ranked by projected risk-weighted score improvement.');
        L.push('');
        L.push('| # | Action | Score Δ | Projected |');
        L.push('|---|--------|--------:|----------:|');
        detailed.actions.forEach((a, i) => {
            L.push(`| ${i + 1} | ${a.title} | +${a.scoreDelta} | ${a.projectedScore}/100 |`);
        });
        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('- **Files:**');
                for (const e of a.evidence.slice(0, 20)) {
                    L.push(`  - \`${e.file}\` — ${e.message || e.rule}`);
                }
                if (a.evidence.length > 20)
                    L.push(`  - … and ${a.evidence.length - 20} more`);
            }
            L.push('');
        }
    }
    L.push('---');
    L.push('');
    // Gaps inventory by risk tier
    L.push('## All Gaps by Risk Tier');
    L.push('');
    const gc = detailed.graphContext;
    if (gc) {
        L.push((0, finding_context_1.graphContextProvenanceLine)(gc));
        L.push('');
    }
    const sorted = [...detailed.gaps].sort((a, b) => TIER_ORDER[a.risk] - TIER_ORDER[b.risk] || b.lines - a.lines);
    const grouped = {
        critical: [],
        high: [],
        medium: [],
        low: [],
    };
    for (const g of sorted)
        grouped[g.risk].push(g);
    for (const tier of ['critical', 'high', 'medium', 'low']) {
        const items = grouped[tier];
        if (items.length === 0)
            continue;
        L.push(`### ${tier.toUpperCase()} (${items.length})`);
        L.push('');
        if (gc) {
            L.push('| File | Type | Lines | Graph context |');
            L.push('|------|------|------:|----------------|');
            for (const g of items.slice(0, 50)) {
                const ctx = gc.contexts[(0, finding_context_1.locationKey)(g.path)];
                L.push(`| \`${g.path}\` | ${g.type} | ${g.lines} | ${(0, finding_context_1.formatGraphContextCell)(ctx)} |`);
            }
            if (items.length > 50)
                L.push(`| … and ${items.length - 50} more | | | |`);
        }
        else {
            L.push('| File | Type | Lines |');
            L.push('|------|------|------:|');
            for (const g of items.slice(0, 50)) {
                L.push(`| \`${g.path}\` | ${g.type} | ${g.lines} |`);
            }
            if (items.length > 50)
                L.push(`| … and ${items.length - 50} more | | |`);
        }
        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