// engine/ai-integration/github-copilot/instructions-generator.ts — .github/copilot-instructions.md generator (SPEC-272)
import { mkdirSync, readFileSync, writeFileSync, existsSync } from 'node:fs';
import { join } from 'node:path';
const SECTION_START = '<!-- planu-sdd:start -->';
const SECTION_END = '<!-- planu-sdd:end -->';
function buildPlanuSection(options) {
    const lines = [SECTION_START, ''];
    lines.push('## Planu SDD Workflow (MANDATORY)');
    lines.push('');
    lines.push('Never write production code without an approved Planu spec.');
    lines.push('');
    lines.push('### Full SDD Flow');
    lines.push('');
    lines.push('1. Describe what you want to build');
    lines.push('2. Planu creates a spec via MCP: `mcp__planu__create_spec`');
    lines.push('3. Review and approve the spec');
    lines.push('4. Implement following the spec criteria exactly');
    lines.push('5. Validate: `mcp__planu__validate`');
    lines.push('6. Mark done: `mcp__planu__update_status`');
    lines.push('');
    lines.push('### Key Planu MCP Commands');
    lines.push('');
    lines.push('- `mcp__planu__create_spec` — Create a new spec from a description');
    lines.push('- `mcp__planu__list_specs` — List all specs and their status');
    lines.push('- `mcp__planu__update_status` — Move a spec through the workflow');
    lines.push('- `mcp__planu__validate` — Validate implementation against spec criteria');
    lines.push('- `mcp__planu__challenge_spec` — Stress-test a spec before implementation');
    lines.push('- `mcp__planu__planu_status` — Show active spec, git state, and pending queue');
    lines.push('');
    lines.push('### Branch Convention');
    lines.push('');
    lines.push('```');
    lines.push('feat/SPEC-XXX-short-description → develop → main');
    lines.push('```');
    lines.push('');
    if (options.activeSpecId) {
        const titlePart = options.activeSpecTitle ? ` — ${options.activeSpecTitle}` : '';
        lines.push(`### Active Spec: ${options.activeSpecId}${titlePart}`);
        lines.push('');
        lines.push('Implement this spec next. Follow all acceptance criteria exactly.');
        lines.push('');
    }
    if (options.approvedSpecs.length > 0) {
        lines.push('### Approved Queue');
        lines.push('');
        for (const spec of options.approvedSpecs) {
            lines.push(`- **${spec.id}**: ${spec.title}`);
        }
        lines.push('');
    }
    if (options.architectureStyle) {
        lines.push(`### Architecture: ${options.architectureStyle}`);
        lines.push('');
    }
    lines.push('### Architectural Rules');
    lines.push('');
    lines.push('- `types/` has no imports from any other layer');
    lines.push('- `engine/` imports only from `types/`');
    lines.push('- `storage/` imports only from `types/`');
    lines.push('- `tools/` imports from `engine/`, `storage/`, and `types/`');
    lines.push('- Cross-layer violations are ESLint errors — do not bypass');
    lines.push('');
    lines.push(SECTION_END);
    return lines.join('\n');
}
export function generateCopilotInstructions(options) {
    const stackStr = options.stack.length > 0 ? options.stack.join(', ') : 'Not specified';
    const header = [
        `# GitHub Copilot Instructions — ${options.projectName}`,
        '',
        `Stack: ${stackStr}`,
        '',
        'This file provides workspace-level instructions for GitHub Copilot.',
        'Instructions in the `<!-- planu-sdd:start -->` block are auto-managed by Planu.',
        '',
    ].join('\n');
    return header + buildPlanuSection(options);
}
export function writeCopilotInstructions(projectPath, options) {
    const githubDir = join(projectPath, '.github');
    mkdirSync(githubDir, { recursive: true });
    const filePath = join(githubDir, 'copilot-instructions.md');
    const planuSection = buildPlanuSection(options);
    /* v8 ignore start */
    if (!existsSync(filePath)) {
        writeFileSync(filePath, generateCopilotInstructions(options), 'utf8');
        return;
    }
    /* v8 ignore stop */
    const existing = readFileSync(filePath, 'utf8');
    const startIdx = existing.indexOf(SECTION_START);
    const endIdx = existing.indexOf(SECTION_END);
    if (startIdx !== -1 && endIdx !== -1) {
        const before = existing.slice(0, startIdx);
        const after = existing.slice(endIdx + SECTION_END.length);
        writeFileSync(filePath, before + planuSection + after, 'utf8');
    }
    else {
        writeFileSync(filePath, existing + '\n' + planuSection, 'utf8');
    }
}
//# sourceMappingURL=instructions-generator.js.map