// tools/generate-rules.ts — Generate project rules for AI agents
// Uses skill-generator engine: generateRules()
import { t, ti } from '../i18n/index.js';
import { knowledgeStore } from '../storage/index.js';
import { generateRules } from '../engine/skill-generator.js';
import { fetchRecommendedSkills } from './suggest-tooling/skills-fetcher.js';
export async function handleGenerateRules(args) {
    const { projectId, platform, includeArchitecture = true, includeConventions = true, includeQuality = true, includeWorkflow = true, customRules = [], } = args;
    const knowledge = await knowledgeStore.getKnowledge(projectId);
    if (!knowledge) {
        return {
            content: [
                {
                    type: 'text',
                    text: ti('errors.projectNotFound', { projectId }),
                },
            ],
            isError: true,
        };
    }
    // Optionally load constitution for richer rules
    const constitution = await knowledgeStore.getConstitution(projectId);
    // Auto-fetch recommended skills from skills.sh based on project stack
    const recommendedSkills = await fetchRecommendedSkills(knowledge);
    const result = await generateRules(platform, knowledge, constitution ?? undefined, {
        includeArchitecture,
        includeConventions,
        includeQuality,
        includeWorkflow,
        customRules,
        recommendedSkills,
    });
    // Format output
    const lines = [
        `## ${t('tools.generate_rules.description')}`,
        '',
        `**Platform:** ${result.platform}`,
        `**Rules count:** ${result.rulesCount}`,
        `**Categories:** ${result.categories.join(', ')}`,
        '',
    ];
    // Main rules file
    lines.push(`### Rules File: \`${result.rulesFile.path}\``);
    lines.push('');
    lines.push(`\`\`\`${result.rulesFile.format}\``);
    lines.push(result.rulesFile.content);
    lines.push('```');
    lines.push('');
    // Additional files
    if (result.additionalFiles.length > 0) {
        lines.push('### Additional Files');
        lines.push('');
        for (const file of result.additionalFiles) {
            lines.push(`#### \`${file.path}\``);
            lines.push('');
            lines.push(`\`\`\`${file.format}\``);
            lines.push(file.content);
            lines.push('```');
            lines.push('');
        }
    }
    // Recommended skills with install commands
    if (result.recommendedSkills.length > 0) {
        lines.push('### Recommended Skills (auto-detected)');
        lines.push('');
        lines.push('Install these skills to enhance your AI agent capabilities:');
        lines.push('');
        for (const skill of result.recommendedSkills) {
            lines.push(`- **${skill.name}**: ${skill.description}`);
            lines.push(`  - Install: \`${skill.installCommand}\``);
        }
        lines.push('');
    }
    // Instructions
    lines.push('### Usage');
    lines.push('');
    lines.push(`Save the rules file to \`${result.rulesFile.path}\` in your project root.`);
    if (result.additionalFiles.length > 0) {
        lines.push(`Also save the ${result.additionalFiles.length} additional file(s) listed above.`);
    }
    if (result.recommendedSkills.length > 0) {
        lines.push('');
        lines.push('**Install the recommended skills above** to get stack-specific best practices.');
    }
    lines.push('');
    lines.push('The AI agent will automatically pick up these rules when working on the project.');
    return {
        content: [{ type: 'text', text: lines.join('\n') }],
    };
}
//# sourceMappingURL=generate-rules.js.map