// tools/init-project/per-client-files-writer.ts — SPEC-648: Per-client file generation
import { writeFile, mkdir, access } from 'node:fs/promises';
import { join } from 'node:path';
import { detectAndCacheClient } from '../../engine/client-detection.js';
import { assertEnglishOnlyArtifactText } from '../../engine/spec-language/english-only.js';
const CURSOR_MDC_CONTENT = `---
description: Planu SDD workflow for Cursor
globs: ["**/*"]
alwaysApply: true
---

# Planu SDD — Cursor Rules

**ALWAYS follow Spec Driven Development. Never write code without an approved spec.**

## Mandatory Workflow

\`\`\`
facilitate("what to build") → create_spec → challenge_spec → check_readiness
→ update_status(review) → approve → update_status(approved) → implement
→ validate → update_status(done)
\`\`\`

## Session Start

1. Call \`planu_status\` to see active specs
2. Resume any \`implementing\` spec before starting new work

## Quick Reference

| Goal | Tool |
|------|------|
| New feature | \`facilitate("description")\` |
| Project status | \`planu_status\` |
| Mark done | \`update_status\` → \`done\` |
| Verify | \`validate\` |
`;
const CODEX_SKILL_CONTENT = `# planu-sdd — Planu Spec Driven Development Skill

Auto-generated by \`init_project\`.

## Overview

Planu enforces Spec Driven Development — spec first, then implement, then validate.

## When to Use

- Starting a new feature: use \`facilitate()\` or \`create_spec()\`
- Resuming work: use \`planu_status\` to see what's in progress
- Finishing work: use \`validate()\` then \`update_status(done)\`

## Mandatory Workflow

1. \`facilitate("what to build")\` — brainstorm → spec
2. \`challenge_spec(specId)\` — stress-test the spec
3. \`check_readiness(specId)\` — verify criteria are complete
4. \`update_status(specId, "approved")\` — lock the spec
5. Implement the code
6. \`validate(specId)\` — verify code matches spec
7. \`update_status(specId, "done")\` — mark complete

## Rules

- NEVER write production code before spec is \`approved\`
- NEVER skip \`validate\` after implementation
`;
const CODEX_NATIVE_SKILL_CONTENT = `# planu-native — Planu Lightweight Native Surface

Auto-generated by \`init_project\`.

## Overview

Use Planu's lightweight CLI surface for common SDD checks without loading the full MCP tool graph.

## Native Commands

- \`planu status\` — compact project snapshot
- \`planu spec create "<title>"\` — create a spec
- \`planu spec list\` — list specs
- \`planu spec validate SPEC-001\` — validate one spec
- \`planu audit debt\` — read-only technical debt audit
- \`planu release check\` — local release readiness check

## Escalate to Full MCP

Use full MCP mode for advanced lifecycle automation, generators, governance tools, MCP resources, prompts, or multi-step orchestration.
`;
async function writeIfMissing(filePath, content) {
    try {
        await access(filePath);
        return false;
    }
    catch {
        await mkdir(join(filePath, '..'), { recursive: true });
        assertEnglishOnlyArtifactText(content, filePath.includes('/skills/') ? 'skill' : 'agent');
        await writeFile(filePath, content, 'utf-8');
        return true;
    }
}
/** SPEC-648: Generate .cursor/rules/planu.mdc for Cursor clients. */
async function writeCursorMdc(projectPath) {
    return writeIfMissing(join(projectPath, '.cursor', 'rules', 'planu.mdc'), CURSOR_MDC_CONTENT);
}
/** SPEC-648: Generate .agents/skills/planu-sdd.md for Codex clients. */
async function writeCodexSkill(projectPath) {
    return writeIfMissing(join(projectPath, '.agents', 'skills', 'planu-sdd.md'), CODEX_SKILL_CONTENT);
}
/** SPEC-1069: Generate .agents/skills/planu-native.md for Codex lightweight mode. */
async function writeCodexNativeSkill(projectPath) {
    return writeIfMissing(join(projectPath, '.agents', 'skills', 'planu-native.md'), CODEX_NATIVE_SKILL_CONTENT);
}
/**
 * SPEC-648: Detect LLM client and generate the appropriate config files.
 * AGENTS.md is always assumed to be generated by generateAgentFilesIfMissing.
 * This function adds client-specific extras (Cursor MDC, Codex skill).
 * Idempotent — never overwrites existing files.
 */
export async function generatePerClientFiles(projectPath) {
    const detectedClient = await detectAndCacheClient(projectPath);
    const filesWritten = [];
    if (detectedClient === 'cursor') {
        const written = await writeCursorMdc(projectPath);
        if (written) {
            filesWritten.push('.cursor/rules/planu.mdc');
        }
    }
    if (detectedClient === 'codex') {
        const [sddWritten, nativeWritten] = await Promise.all([
            writeCodexSkill(projectPath),
            writeCodexNativeSkill(projectPath),
        ]);
        if (sddWritten) {
            filesWritten.push('.agents/skills/planu-sdd.md');
        }
        if (nativeWritten) {
            filesWritten.push('.agents/skills/planu-native.md');
        }
    }
    return { detectedClient, filesWritten };
}
//# sourceMappingURL=per-client-files-writer.js.map