// Apply project manifest to ~/.claude/knowledge/projects/
import { writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { ensureDir, knowledgeFile } from '../utils/paths.js';
export function writeProjectManifest(projects) {
    const projectsDir = knowledgeFile('projects');
    ensureDir(projectsDir);
    const writtenPaths = [];
    const today = new Date().toISOString().slice(0, 10);
    // Write index README
    const indexContent = `# Projects Manifest

> Auto-generated by ai-bootstrap. ${projects.length} projects tracked.

## Project list

${projects.map((p) => `- [${p.name}](./${slugify(p.name)}.md) — ${p.type}`).join('\n')}

## Stats

- Total projects: ${projects.length}
- By type:
${Object.entries(groupBy(projects, 'type'))
        .map(([type, items]) => `  - ${type}: ${items.length}`)
        .join('\n')}

---

**Generated**: ${today}
**Source**: ai-bootstrap wizard
`;
    const indexPath = join(projectsDir, 'README.md');
    writeFileSync(indexPath, indexContent, 'utf-8');
    writtenPaths.push(indexPath);
    // Write per-project file
    for (const p of projects) {
        const slug = slugify(p.name);
        const filePath = join(projectsDir, `${slug}.md`);
        const content = `---
name: ${p.name}
slug: ${slug}
type: ${p.type}
status: active
last_active: ${p.lastModified.toISOString().slice(0, 10)}
repo: ${p.path}
---

# ${p.name}

## Type
\`${p.type}\`

## Repo location
\`${p.path}\`

## Detected features
- CLAUDE.md present: ${p.hasClaudeMd ? '✓' : '✗'}
- docs/ folder present: ${p.hasDocsFolder ? '✓' : '✗'}
- Last modified: ${p.lastModified.toISOString().slice(0, 10)}

## Current state
(Auto-populate as you work on this project)

- **Stage**: ${p.hasClaudeMd ? 'active' : 'unknown'}
- **Users**: (TBD)
- **Revenue**: (TBD)

## Stack
(Read CLAUDE.md or package.json to populate)

## Cross-references
- Skills used: (TBD)
- Agents used: (TBD)
- MCPs used: (TBD)

## Open loops
(Auto-populated from session handoffs)

## Last session
(Auto-populated by journal-keeper)

---

**Generated**: ${today}
**Last updated**: ${today}
**Source**: ai-bootstrap wizard auto-scan
`;
        writeFileSync(filePath, content, 'utf-8');
        writtenPaths.push(filePath);
    }
    return writtenPaths;
}
function slugify(name) {
    return name
        .toLowerCase()
        .replace(/[^a-z0-9]+/g, '-')
        .replace(/^-+|-+$/g, '');
}
function groupBy(items, key) {
    return items.reduce((acc, item) => {
        const k = String(item[key]);
        if (!acc[k])
            acc[k] = [];
        acc[k].push(item);
        return acc;
    }, {});
}
//# sourceMappingURL=projects-writer.js.map