// engine/ai-integration/windsurf/windsurfrules-generator.ts — .windsurfrules generator (SPEC-268)
import { readFileSync, writeFileSync, existsSync } from 'node:fs';
import { join } from 'node:path';
import { assertEnglishOnlyArtifactText } from '../../spec-language/english-only.js';
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('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');
    lines.push('5. Validate: `mcp__planu__validate`');
    lines.push('');
    lines.push('### Key Planu MCP Tools');
    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('');
    lines.push('### Branch Convention');
    lines.push('```');
    lines.push('feat/SPEC-XXX-short-description → develop → main');
    lines.push('```');
    lines.push('');
    if (options.activeSpecId) {
        lines.push(`### Active Spec: ${options.activeSpecId}`);
        lines.push('');
        lines.push('Implement this spec next. Follow all 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(SECTION_END);
    const content = lines.join('\n');
    assertEnglishOnlyArtifactText(content, 'agent');
    return content;
}
export function generateWindsurfRules(options) {
    const stackStr = options.stack.length > 0 ? options.stack.join(', ') : 'Not specified';
    const header = [`# Windsurf Rules — ${options.projectName}`, '', `Stack: ${stackStr}`, ''].join('\n');
    const content = header + buildPlanuSection(options);
    assertEnglishOnlyArtifactText(content, 'agent');
    return content;
}
export function writeWindsurfRules(projectPath, options) {
    const filePath = join(projectPath, '.windsurfrules');
    const planuSection = buildPlanuSection(options);
    if (!existsSync(filePath)) {
        writeFileSync(filePath, generateWindsurfRules(options), 'utf8');
        return;
    }
    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=windsurfrules-generator.js.map