// engine/ai-integration/gemini/gemini-md-generator.ts — GEMINI.md generator (SPEC-267)
import { writeFileSync, readFileSync, existsSync } from 'node:fs';
import { join } from 'node:path';
const PLANU_SECTION_START = '<!-- planu-sdd:start -->';
const PLANU_SECTION_END = '<!-- planu-sdd:end -->';
function buildSddSection(options) {
    const { projectName, activeSpecId, activeSpecTitle, approvedSpecs, stack, architectureStyle } = options;
    const activeSpecLine = activeSpecId !== undefined && activeSpecTitle !== undefined
        ? `- **Active spec**: ${activeSpecId} — ${activeSpecTitle}`
        : '- **Active spec**: none (create one with `create_spec` before writing code)';
    const queueLines = approvedSpecs.length > 0
        ? approvedSpecs.map((s) => `  - ${s.id}: ${s.title}`).join('\n')
        : '  - (no approved specs in queue)';
    const stackLine = stack.length > 0 ? stack.join(', ') : 'undetected';
    const archLine = architectureStyle !== undefined && architectureStyle.length > 0
        ? `\n- **Architecture**: ${architectureStyle}`
        : '';
    return [
        PLANU_SECTION_START,
        '',
        `## Planu SDD Workflow — ${projectName}`,
        '',
        '### Mandatory Development Flow',
        '',
        '**Spec before code — no exceptions.**',
        '',
        '1. Describe the feature → Planu creates a spec',
        '2. Review + approve the spec',
        '3. Implement on a `feat/SPEC-XXX-*` branch',
        '4. Run `validate` after implementation',
        '',
        '### Current State',
        '',
        activeSpecLine,
        '- **Approved queue**:',
        queueLines,
        `- **Stack**: ${stackLine}${archLine}`,
        '',
        '### Planu MCP Tools',
        '',
        'Use these tools directly from Gemini CLI:',
        '',
        '| Tool | Purpose |',
        '|------|---------|',
        '| `create_spec` | Create a new spec for a feature |',
        '| `list_specs` | List all specs and their status |',
        '| `update_status` | Move spec to approved/done |',
        '| `validate` | Check implementation against spec |',
        '| `challenge_spec` | Stress-test spec for gaps |',
        '| `check_readiness` | Verify spec is detailed enough |',
        '',
        '### Branch Naming',
        '',
        '```',
        'feat/SPEC-XXX-short-description   # feature spec',
        'fix/SPEC-XXX-bug-description      # bug fix spec',
        '```',
        '',
        '### Architecture Rules',
        '',
        '- `types/` → no imports from other layers',
        '- `engine/` → imports only from `types/`',
        '- `storage/` → imports only from `types/`',
        '- `tools/` → imports from `engine/` + `storage/` + `types/`',
        '- Cross-layer violations → ESLint error',
        '',
        PLANU_SECTION_END,
    ].join('\n');
}
export function generateGeminiMd(options) {
    return buildSddSection(options);
}
export function writeGeminiMd(projectPath, options) {
    const filePath = join(projectPath, 'GEMINI.md');
    const newSection = buildSddSection(options);
    if (!existsSync(filePath)) {
        writeFileSync(filePath, newSection + '\n', 'utf8');
        return;
    }
    const existing = readFileSync(filePath, 'utf8');
    const startIdx = existing.indexOf(PLANU_SECTION_START);
    const endIdx = existing.indexOf(PLANU_SECTION_END);
    if (startIdx === -1 || endIdx === -1) {
        // Append section to existing file
        const separator = existing.endsWith('\n') ? '' : '\n';
        writeFileSync(filePath, existing + separator + newSection + '\n', 'utf8');
        return;
    }
    // Replace existing planu section
    const before = existing.slice(0, startIdx);
    const after = existing.slice(endIdx + PLANU_SECTION_END.length);
    writeFileSync(filePath, before + newSection + after, 'utf8');
}
//# sourceMappingURL=gemini-md-generator.js.map