const ARCHIVABLE_DAYS = 30;
/** Detect specs that are done and had no updates for ARCHIVABLE_DAYS days. */
export function detectArchivableSpecs(specs) {
    const cutoff = new Date();
    cutoff.setDate(cutoff.getDate() - ARCHIVABLE_DAYS);
    return specs
        .filter((s) => {
        if (s.status !== 'done') {
            return false;
        }
        const updated = s.updatedAt ? new Date(s.updatedAt) : null;
        return updated === null || updated < cutoff;
    })
        .map((s) => s.id);
}
/** Detect notes whose specId points to a non-existent spec. */
export function detectOrphanNotes(notes, specs) {
    const specIds = new Set(specs.map((s) => s.id));
    return notes
        .filter((n) => n.specId !== undefined && n.specId !== '' && !specIds.has(n.specId))
        .map((n) => n.key);
}
/** Calculate health score 0-100 based on orphan ratio and archivable ratio. */
export function calculateHealthScore(totalNotes, orphanCount, archivableCount, totalSpecs) {
    if (totalNotes === 0 && totalSpecs === 0) {
        return 100;
    }
    const orphanRatio = totalNotes > 0 ? orphanCount / totalNotes : 0;
    const archivableRatio = totalSpecs > 0 ? archivableCount / totalSpecs : 0;
    const penalty = orphanRatio * 40 + archivableRatio * 30;
    return Math.max(0, Math.round(100 - penalty));
}
/** Generate a DocHealthReport from notes and specs data. */
export function generateDocHealthReport(notes, specs) {
    const archivableSpecs = detectArchivableSpecs(specs);
    const orphanNotes = detectOrphanNotes(notes, specs);
    const score = calculateHealthScore(notes.length, orphanNotes.length, archivableSpecs.length, specs.length);
    return {
        archivable: archivableSpecs,
        orphans: orphanNotes,
        outdated: [],
        score,
        summary: buildSummary(score, archivableSpecs.length, orphanNotes.length),
        totalNotes: notes.length,
        totalSpecs: specs.length,
        generatedAt: new Date().toISOString(),
    };
}
function buildSummary(score, archivable, orphans) {
    const parts = [`Health score: ${score}/100.`];
    if (archivable > 0) {
        parts.push(`${archivable} spec(s) are archivable (done >30 days).`);
    }
    if (orphans > 0) {
        parts.push(`${orphans} orphan note(s) reference non-existent specs.`);
    }
    if (archivable === 0 && orphans === 0) {
        parts.push('Everything looks clean!');
    }
    return parts.join(' ');
}
//# sourceMappingURL=health.js.map