import { writeFileSync, mkdirSync } from 'node:fs';
import { join } from 'node:path';
import { assertEnglishOnlyArtifactText } from '../../spec-language/english-only.js';
const PLANU_SECTION_START = '<!-- planu:start -->';
const PLANU_SECTION_END = '<!-- planu:end -->';
function buildWorkflowSection() {
    return `## Planu SDD Workflow (MANDATORY)

Never write production code without an approved Planu spec.

1. Describe what you want to build
2. Planu creates a spec via MCP: \`mcp__planu__create_spec\`
3. Review and approve the spec
4. Implement following the spec criteria
5. Validate: \`mcp__planu__validate\`

### Key Planu MCP Tools
- \`mcp__planu__create_spec\` — Create a new spec from a description
- \`mcp__planu__list_specs\` — List all specs and their status
- \`mcp__planu__update_status\` — Move a spec through the workflow
- \`mcp__planu__validate\` — Validate implementation against spec criteria
- \`mcp__planu__challenge_spec\` — Stress-test a spec before implementation
- \`mcp__planu__check_readiness\` — Verify spec has enough detail to implement`;
}
function buildSpecSection(options) {
    const lines = ['## Active Spec'];
    if (options.activeSpecId && options.activeSpecTitle) {
        lines.push('');
        lines.push(`**${options.activeSpecId}**: ${options.activeSpecTitle}`);
        lines.push('');
        lines.push('Implement this spec next. Follow all criteria exactly.');
    }
    else {
        lines.push('');
        lines.push('No active spec. Use `mcp__planu__list_specs` to find approved specs.');
    }
    if (options.approvedSpecs.length > 0) {
        lines.push('');
        lines.push('## Approved Queue');
        lines.push('');
        for (const spec of options.approvedSpecs) {
            lines.push(`- **${spec.id}**: ${spec.title}`);
        }
        lines.push('');
        lines.push('Implement specs in order. Do not skip ahead.');
    }
    return lines.join('\n');
}
function buildBranchSection() {
    return `## Branch Convention

\`\`\`
feat/SPEC-XXX-short-description → develop → main
\`\`\`

- Always branch from \`develop\`
- Branch name must include the spec ID: \`feat/SPEC-042-user-auth\`
- Never commit directly to \`develop\` or \`main\`
- Merge via PR after implementation is validated`;
}
function buildArchitectureSection(options) {
    const lines = ['## Architecture'];
    if (options.architectureStyle) {
        lines.push('');
        lines.push(`**Style**: ${options.architectureStyle}`);
    }
    if (options.stack.length > 0) {
        lines.push('');
        lines.push(`**Stack**: ${options.stack.join(', ')}`);
    }
    lines.push('');
    lines.push('Follow the existing project structure. Read the codebase before writing new files.');
    lines.push('Prefer extending existing modules over creating new ones when the domain matches.');
    return lines.join('\n');
}
/** Generates the full AGENTS.md content for a project using Planu SDD. */
export function generateAgentsMd(options) {
    const workflowSection = buildWorkflowSection();
    const specSection = buildSpecSection(options);
    const branchSection = buildBranchSection();
    const architectureSection = buildArchitectureSection(options);
    const planuBlock = [
        PLANU_SECTION_START,
        `# ${options.projectName} — Agent Instructions`,
        '',
        workflowSection,
        '',
        specSection,
        '',
        branchSection,
        '',
        architectureSection,
        PLANU_SECTION_END,
    ].join('\n');
    assertEnglishOnlyArtifactText(planuBlock, 'agent');
    return planuBlock;
}
/**
 * Writes AGENTS.md to projectPath.
 * Idempotent: replaces the Planu section if it already exists,
 * or creates the file from scratch.
 */
export function writeAgentsMd(projectPath, options) {
    const filePath = join(projectPath, 'AGENTS.md');
    const content = generateAgentsMd(options);
    /* v8 ignore start */
    try {
        mkdirSync(projectPath, { recursive: true });
    }
    catch {
        // directory already exists
    }
    /* v8 ignore stop */
    writeFileSync(filePath, content, 'utf8');
}
//# sourceMappingURL=agents-md-generator.js.map