// resources/specs.ts — Current project specs list resource
// Returns the list of specs for the current project from the data store.
// Storage format: data/projects/{projectId}/specs.json (single array per project)
import { readFile } from 'node:fs/promises';
import { glob } from 'glob';
import { t } from '../i18n/index.js';
/**
 * Scan the data/projects/ directory for all specs.json files and return them.
 * Each project stores ALL specs in a single data/projects/{projectId}/specs.json as an array.
 */
async function loadAllSpecs() {
    const specs = [];
    try {
        const specFiles = await glob('data/projects/*/specs.json');
        for (const file of specFiles) {
            try {
                const raw = await readFile(file, 'utf-8');
                const parsed = JSON.parse(raw);
                if (Array.isArray(parsed)) {
                    for (const entry of parsed) {
                        if (entry !== null && typeof entry === 'object' && 'id' in entry) {
                            specs.push(entry);
                        }
                    }
                }
            }
            catch {
                // Skip malformed files
            }
        }
    }
    catch {
        // No data directory yet — that is fine
    }
    return specs;
}
/**
 * MCP resource handler for planu://specs
 */
export async function handleSpecsResource() {
    const specs = await loadAllSpecs();
    if (specs.length === 0) {
        return JSON.stringify({
            count: 0,
            message: t('resources.specs.notFound'),
            specs: [],
        }, null, 2);
    }
    const summary = specs.map((s) => ({
        id: s.id,
        title: s.title,
        type: s.type,
        scope: s.scope,
        status: s.status,
        difficulty: s.difficulty,
        risk: s.risk,
        target: s.target,
        createdAt: s.createdAt,
        updatedAt: s.updatedAt,
    }));
    const byStatus = {};
    for (const s of specs) {
        byStatus[s.status] = (byStatus[s.status] ?? 0) + 1;
    }
    return JSON.stringify({
        count: specs.length,
        byStatus,
        specs: summary,
    }, null, 2);
}
export const specsResourceDefinition = {
    uri: 'planu://specs',
    name: 'Project Specs',
    description: t('resources.specs.description'),
    mimeType: 'application/json',
};
//# sourceMappingURL=specs.js.map