/**
 * CLAUDE.md Generator
 *
 * Generates CLAUDE.md - the PRIMARY instruction file for Claude Code.
 *
 * CRITICAL:
 * - Claude Code ONLY reads CLAUDE.md (NOT AGENTS.md)
 * - This is the native/baseline implementation (skills, agents, hooks, slash commands)
 * - Claude Code is NOT an adapter - it's the gold standard that others try to approximate
 *
 * This generator creates a quick reference that lists what's actually installed,
 * so when we add new agents/skills, CLAUDE.md stays in sync automatically.
 */
import { DocGenerator } from './doc-generator.js';
import * as fs from '../utils/fs-native.js';
import { getDirname } from '../utils/esm-helpers.js';
import { findTemplateFile } from './template-utils.js';
const __dirname = getDirname(import.meta.url);
/**
 * Generate CLAUDE.md with dynamic agent/skill sections
 */
export class ClaudeMdGenerator {
    constructor(skillsDir, agentsDir, commandsDir) {
        this.docGen = new DocGenerator(skillsDir, agentsDir, commandsDir);
    }
    /**
     * Generate complete CLAUDE.md content
     */
    async generate(options) {
        // Read template - use provided path or find it via package root
        let templatePath = options.templatePath;
        if (!templatePath || !await fs.pathExists(templatePath)) {
            const foundPath = findTemplateFile('CLAUDE.md.template', __dirname);
            if (foundPath) {
                templatePath = foundPath;
            }
            else {
                throw new Error('CLAUDE.md template not found');
            }
        }
        let content = await fs.readFile(templatePath, 'utf-8');
        // Replace placeholders
        content = content.replace(/\{PROJECT_NAME\}/g, options.projectName);
        // Replace dynamic sections
        content = await this.replaceDynamicSections(content);
        return content;
    }
    /**
     * Replace dynamic sections in template
     */
    async replaceDynamicSections(content) {
        // Extract actual skills and agents
        const skills = await this.docGen.extractSkills();
        const agents = await this.docGen.extractAgents();
        const commands = await this.docGen.extractCommands();
        // Replace agents section (lines 226-243 in template)
        content = this.replaceAgentsSection(content, agents);
        // Replace skills section (lines 248-278 in template)
        content = this.replaceSkillsSection(content, skills);
        // Replace commands section if needed
        content = this.replaceCommandsSection(content, commands);
        return content;
    }
    /**
     * Generate dynamic agents section
     */
    replaceAgentsSection(content, agents) {
        const agentsTable = this.generateAgentsTable(agents);
        // Find the agents section and replace it
        // Look for "## Agents (Activate Automatically)" section
        const agentsSectionRegex = /## Agents \(Activate Automatically\)[\s\S]*?(?=\n## |$)/;
        const agentsSection = `## Agents (Activate Automatically)

**Strategic Agents** (pre-installed & ready to use):

${agentsTable}

**All ${agents.length} agents are pre-installed** - Claude uses the right one based on your request!

**See**: \`.claude/agents/\` for complete list

---
`;
        if (agentsSectionRegex.test(content)) {
            return content.replace(agentsSectionRegex, agentsSection);
        }
        return content;
    }
    /**
     * Generate agents table
     */
    generateAgentsTable(agents) {
        let table = '| Agent | Purpose | Activates When |\n';
        table += '|-------|---------|----------------|\n';
        for (const agent of agents) {
            const activation = this.getAgentActivation(agent.name);
            table += `| \`${agent.name}\` | ${agent.description} | ${activation} |\n`;
        }
        return table;
    }
    /**
     * Get agent activation guidance
     */
    getAgentActivation(agentName) {
        const activations = {
            'pm': 'Planning features',
            'architect': 'Technical design',
            'security': 'Security review',
            'qa-engineer': 'Testing',
            'devops': 'Deployment needed',
            'sre': 'Troubleshooting',
            'docs-writer': 'Writing docs',
            'performance': 'Optimization needed',
            'nodejs-backend': 'Node.js backend',
            'python-backend': 'Python backend',
            'nextjs': 'Next.js development',
            'dotnet-backend': '.NET development'
        };
        return activations[agentName] || 'Specialized tasks';
    }
    /**
     * Generate dynamic skills section
     */
    replaceSkillsSection(content, skills) {
        const skillsTable = this.generateSkillsTable(skills);
        // Find the skills section and replace it
        const skillsSectionRegex = /## Skills \(Activate Automatically\)[\s\S]*?(?=\n## |$)/;
        const skillsSection = `## Skills (Activate Automatically)

**Framework Skills** (always available):

${this.generateFrameworkSkillsTable(skills)}

**Technology Skills** (all pre-installed):

${this.generateTechSkillsTable(skills)}

**Integration Skills** (all pre-installed):

${this.generateIntegrationSkillsTable(skills)}

**See**: \`.claude/skills/\` for complete list of ${skills.length}+ skills!

---
`;
        if (skillsSectionRegex.test(content)) {
            return content.replace(skillsSectionRegex, skillsSection);
        }
        return content;
    }
    /**
     * Generate framework skills table
     */
    generateFrameworkSkillsTable(skills) {
        const frameworkSkills = skills.filter(s => ['specweave-detector', 'increment', 'context-loader', 'skill-router',
            'spec-driven-debugging', 'brainstorm'].includes(s.name));
        let table = '| Skill | Purpose | Activates When |\n';
        table += '|-------|---------|----------------|\n';
        for (const skill of frameworkSkills) {
            const activation = this.getSkillActivation(skill.name);
            table += `| \`${skill.name}\` | ${this.truncate(skill.description, 60)} | ${activation} |\n`;
        }
        return table;
    }
    /**
     * Generate tech skills table
     */
    generateTechSkillsTable(skills) {
        const techSkills = skills.filter(s => ['nodejs-backend', 'python-backend', 'nextjs', 'dotnet-backend',
            'e2e-playwright'].includes(s.name));
        let table = '| Skill | Purpose | Activates When |\n';
        table += '|-------|---------|----------------|\n';
        for (const skill of techSkills) {
            const activation = this.getSkillActivation(skill.name);
            table += `| \`${skill.name}\` | ${this.truncate(skill.description, 60)} | ${activation} |\n`;
        }
        return table;
    }
    /**
     * Generate integration skills table
     */
    generateIntegrationSkillsTable(skills) {
        const integrationSkills = skills.filter(s => ['jira-sync', 'github-sync', 'ado-sync', 'hetzner-provisioner',
            'cost-optimizer', 'figma'].includes(s.name));
        let table = '| Skill | Purpose | Activates When |\n';
        table += '|-------|---------|----------------|\n';
        for (const skill of integrationSkills) {
            const activation = this.getSkillActivation(skill.name);
            table += `| \`${skill.name}\` | ${this.truncate(skill.description, 60)} | ${activation} |\n`;
        }
        return table;
    }
    /**
     * Get skill activation guidance
     */
    getSkillActivation(skillName) {
        const activations = {
            'specweave-detector': 'User asks about SpecWeave',
            'increment': 'sw:increment or feature planning',
            'context-loader': 'Working on increments',
            'skill-router': 'Ambiguous requests',
            'spec-driven-debugging': 'Bug or test failure',
            'brainstorm': 'Brainstorm or refine idea',
            'nodejs-backend': 'Node.js backend',
            'python-backend': 'Python backend',
            'nextjs': 'Next.js',
            'dotnet-backend': '.NET',
            'jira-sync': 'JIRA integration',
            'github-sync': 'GitHub integration',
            'ado-sync': 'Azure DevOps sync',
            'hetzner-provisioner': 'Hetzner deployment',
            'cost-optimizer': 'Cost optimization',
            'e2e-playwright': 'E2E testing'
        };
        return activations[skillName] || 'Specialized tasks';
    }
    /**
     * Generate skills table
     */
    generateSkillsTable(skills) {
        let table = '| Skill | Description |\n';
        table += '|-------|-------------|\n';
        for (const skill of skills.slice(0, 20)) { // Top 20 skills
            table += `| \`${skill.name}\` | ${this.truncate(skill.description, 80)} |\n`;
        }
        if (skills.length > 20) {
            table += `| _...and ${skills.length - 20} more_ | See \`.claude/skills/\` |\n`;
        }
        return table;
    }
    /**
     * Replace commands section
     */
    replaceCommandsSection(content, commands) {
        // Commands are already listed in the template, but we could make them dynamic too
        // For now, we'll skip this since commands are explicitly documented
        return content;
    }
    /**
     * Truncate text to max length
     */
    truncate(text, maxLength) {
        if (text.length <= maxLength)
            return text;
        return text.substring(0, maxLength - 3) + '...';
    }
}
//# sourceMappingURL=claude-md-generator.js.map