import { writeFileSync, mkdirSync, existsSync } from 'node:fs';
import { join } from 'node:path';
import { generateCodexHooksToml, generateCodexHooks } from './hooks-generator.js';
function buildProfilesSection() {
    return [
        '[[profiles]]',
        'name = "planu-safe"',
        'approval_policy = "on-request"',
        'sandbox_mode = "workspace-write"',
        '',
        '[[profiles]]',
        'name = "planu-fast"',
        'approval_policy = "never"',
        'sandbox_mode = "workspace-write"',
        'model = "gpt-4.1-mini"',
    ].join('\n');
}
function buildFeaturesSection() {
    return ['[features]', 'multi_agent = true', 'shell_tool = true'].join('\n');
}
/** Generates .codex/config.toml content for a project. */
export function generateCodexConfigToml(options) {
    const hooks = generateCodexHooks();
    const hooksToml = generateCodexHooksToml(hooks);
    const profiles = buildProfilesSection();
    const features = buildFeaturesSection();
    const mcpComment = options.hasPlanuMcp
        ? '# Planu MCP is configured — SDD workflow available'
        : '# Add Planu MCP to enable SDD workflow: https://github.com/planu-dev/planu';
    const stackComment = options.stack.length > 0 ? `# Stack: ${options.stack.join(', ')}` : '# Stack: not specified';
    const sections = [
        `# .codex/config.toml — Generated by Planu (SPEC-266)`,
        `# Project: ${options.projectPath}`,
        mcpComment,
        stackComment,
        '',
        'model = "gpt-4.1"',
        'approval_policy = "on-request"',
        'sandbox_mode = "workspace-write"',
        'web_search = "cached"',
        'model_reasoning_effort = "high"',
        '',
        hooksToml,
        '',
        profiles,
        '',
        features,
    ];
    return sections.join('\n');
}
/**
 * Creates .codex/ directory if missing and writes config.toml.
 * Idempotent: overwrites existing config.
 */
export function writeCodexConfig(projectPath, options) {
    const codexDir = join(projectPath, '.codex');
    /* v8 ignore start */
    if (!existsSync(codexDir)) {
        mkdirSync(codexDir, { recursive: true });
    }
    /* v8 ignore stop */
    const configPath = join(codexDir, 'config.toml');
    const content = generateCodexConfigToml(options);
    writeFileSync(configPath, content, 'utf8');
}
//# sourceMappingURL=config-generator.js.map