// tools/gc-data-projects.ts — SPEC-581
// MCP tool handler: garbage-collect data/projects/ test dirs and stale dirs.
import { join, resolve } from 'node:path';
import { runGC } from '../engine/data-projects-gc/gc-runner.js';
import { getProjects } from '../storage/global-projects-store.js';
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
function resolveDataProjectsDir(projectPath) {
    const base = projectPath ? resolve(projectPath) : process.cwd();
    return join(base, 'data', 'projects');
}
function formatEntries(entries, limit = 10) {
    if (entries.length === 0) {
        return '  _none_';
    }
    const shown = entries.slice(0, limit);
    const more = entries.length - shown.length;
    const lines = shown.map((e) => {
        const age = e.lastModifiedMs !== undefined
            ? ` (last modified ${Math.floor((Date.now() - e.lastModifiedMs) / 86_400_000)}d ago)`
            : '';
        return `  - \`${e.name}\`${age}`;
    });
    if (more > 0) {
        lines.push(`  … and ${more} more`);
    }
    return lines.join('\n');
}
function buildReport(result) {
    const mode = result.dryRun ? '**DRY-RUN** (pass `apply: true` to delete)' : '**APPLIED**';
    const lines = [
        `## gc_data_projects — ${mode}`,
        '',
        `Scanned **${result.scannedTotal}** dirs in \`data/projects/\` | Candidates: **${result.candidateCount}** | Inactive threshold: **${result.inactiveDays}d**`,
        '',
        `### Test-named dirs (${result.testNamed.length})`,
        formatEntries(result.testNamed),
        '',
        `### Inactive dirs (${result.inactive.length})`,
        formatEntries(result.inactive),
        '',
        `### Orphaned dirs (${result.orphaned.length})`,
        formatEntries(result.orphaned),
    ];
    if (!result.dryRun) {
        lines.push('', `### Deleted (${result.deleted.length})`);
        if (result.deleted.length === 0) {
            lines.push('  _none_');
        }
        else {
            lines.push(...result.deleted.map((d) => `  - \`${d}\``));
        }
        if (result.errors.length > 0) {
            lines.push('', `### Errors (${result.errors.length})`);
            lines.push(...result.errors.map((e) => `  - ${e}`));
        }
    }
    return lines.join('\n');
}
// ---------------------------------------------------------------------------
// Handler
// ---------------------------------------------------------------------------
export async function handleGcDataProjects(input) {
    const { projectPath, inactiveDays = 90, apply = false, includeOrphans = false } = input;
    const dataProjectsDir = resolveDataProjectsDir(projectPath);
    // Build registry hash set (for orphan detection)
    let registeredHashes;
    if (includeOrphans) {
        try {
            const projects = await getProjects();
            registeredHashes = new Set(projects.map((p) => p.hash));
        }
        catch {
            // registry unreadable — skip orphan detection gracefully
            registeredHashes = undefined;
        }
    }
    const result = await runGC({
        dataProjectsDir,
        apply,
        inactiveDays,
        registeredHashes,
        includeOrphans: includeOrphans && registeredHashes !== undefined,
    });
    return {
        content: [{ type: 'text', text: buildReport(result) }],
    };
}
//# sourceMappingURL=gc-data-projects.js.map