import fs from 'fs';
import path from 'path';
/**
 * Initialize .codebuddy directory with templates and configurations
 * Similar to Claude Code's project initialization
 */
export function initCodeBuddyProject(workingDirectory = process.cwd(), options = {}) {
    const result = {
        success: true,
        created: [],
        skipped: [],
        errors: []
    };
    const codebuddyDir = path.join(workingDirectory, '.codebuddy');
    // Create .codebuddy directory
    if (!fs.existsSync(codebuddyDir)) {
        fs.mkdirSync(codebuddyDir, { recursive: true });
        result.created.push('.codebuddy/');
    }
    // Create CODEBUDDY.md (custom instructions)
    const codebuddyMdPath = path.join(codebuddyDir, 'CODEBUDDY.md');
    if (!fs.existsSync(codebuddyMdPath) || options.force) {
        const codebuddyMdContent = `# Custom Instructions for Code Buddy

## About This Project
<!-- Describe your project here -->
This project is...

## Code Style Guidelines
- Use TypeScript for all new files
- Follow the existing code style
- Add comments for complex logic
- Use meaningful variable names

## Architecture
<!-- Describe your project architecture -->
- src/ - Source code
- tests/ - Test files

## Testing
- Write tests for new features
- Maintain test coverage above 80%
- Use Jest/Vitest for testing

## Git Conventions
- Use conventional commits (feat:, fix:, docs:, etc.)
- Keep commits small and focused
- Write descriptive commit messages

## Important Notes
<!-- Add any project-specific notes here -->
- ...

## Forbidden Actions
<!-- List things Code Buddy should never do -->
- Never commit .env files
- Never expose API keys
- Never delete production data
`;
        fs.writeFileSync(codebuddyMdPath, codebuddyMdContent);
        result.created.push('.codebuddy/CODEBUDDY.md');
    }
    else {
        result.skipped.push('.codebuddy/CODEBUDDY.md (already exists)');
    }
    // Create hooks.json
    if (options.includeHooks !== false) {
        const hooksPath = path.join(codebuddyDir, 'hooks.json');
        if (!fs.existsSync(hooksPath) || options.force) {
            const hooksContent = {
                enabled: true,
                globalTimeout: 30000,
                hooks: [
                    {
                        type: 'pre-commit',
                        command: 'npm run lint && npm test',
                        enabled: false,
                        timeout: 60000,
                        continueOnError: false,
                        description: 'Run linter and tests before commit'
                    },
                    {
                        type: 'post-edit',
                        command: 'npm run typecheck',
                        enabled: false,
                        timeout: 30000,
                        continueOnError: true,
                        description: 'Run type checking after file edit'
                    },
                    {
                        type: 'on-file-change',
                        command: 'prettier --write {file}',
                        enabled: false,
                        timeout: 10000,
                        continueOnError: true,
                        description: 'Format file with Prettier on change'
                    }
                ]
            };
            fs.writeFileSync(hooksPath, JSON.stringify(hooksContent, null, 2));
            result.created.push('.codebuddy/hooks.json');
        }
        else {
            result.skipped.push('.codebuddy/hooks.json (already exists)');
        }
    }
    // Create mcp.json
    if (options.includeMcp !== false) {
        const mcpPath = path.join(codebuddyDir, 'mcp.json');
        if (!fs.existsSync(mcpPath) || options.force) {
            const mcpContent = {
                $schema: 'https://json-schema.org/draft/2020-12/schema',
                description: 'MCP server configuration. This file can be committed to share MCP servers with your team.',
                mcpServers: {
                    // Example configurations (disabled by default)
                    'filesystem': {
                        name: 'filesystem',
                        type: 'stdio',
                        command: 'npx',
                        args: ['-y', '@anthropic-ai/mcp-server-filesystem', '.'],
                        enabled: false,
                        description: 'File system access MCP server'
                    },
                    'github': {
                        name: 'github',
                        type: 'stdio',
                        command: 'npx',
                        args: ['-y', '@anthropic-ai/mcp-github'],
                        env: {
                            GITHUB_TOKEN: '${GITHUB_TOKEN}'
                        },
                        enabled: false,
                        description: 'GitHub integration MCP server'
                    }
                }
            };
            fs.writeFileSync(mcpPath, JSON.stringify(mcpContent, null, 2));
            result.created.push('.codebuddy/mcp.json');
        }
        else {
            result.skipped.push('.codebuddy/mcp.json (already exists)');
        }
    }
    // Create security.json
    if (options.includeSecurity !== false) {
        const securityPath = path.join(codebuddyDir, 'security.json');
        if (!fs.existsSync(securityPath) || options.force) {
            const securityContent = {
                mode: 'suggest',
                allowedDirectories: [],
                blockedCommands: [],
                blockedPaths: []
            };
            fs.writeFileSync(securityPath, JSON.stringify(securityContent, null, 2));
            result.created.push('.codebuddy/security.json');
        }
        else {
            result.skipped.push('.codebuddy/security.json (already exists)');
        }
    }
    // Create commands directory with example
    if (options.includeCommands !== false) {
        const commandsDir = path.join(codebuddyDir, 'commands');
        if (!fs.existsSync(commandsDir)) {
            fs.mkdirSync(commandsDir, { recursive: true });
            result.created.push('.codebuddy/commands/');
        }
        // Create example command
        const exampleCommandPath = path.join(commandsDir, 'example.md');
        if (!fs.existsSync(exampleCommandPath) || options.force) {
            const exampleCommandContent = `---
description: Example custom command template
---

# Example Command

This is an example slash command. Usage: /example [argument]

Replace this content with your own prompt template.

You can use placeholders:
- $1, $2, etc. for positional arguments
- $@ for all arguments combined

Example: Analyze the file $1 and suggest improvements.
`;
            fs.writeFileSync(exampleCommandPath, exampleCommandContent);
            result.created.push('.codebuddy/commands/example.md');
        }
        // Create deploy command template
        const deployCommandPath = path.join(commandsDir, 'deploy.md');
        if (!fs.existsSync(deployCommandPath) || options.force) {
            const deployCommandContent = `---
description: Deploy the application to production
---

# Deploy Command

Perform a deployment to production:

1. Run all tests to ensure nothing is broken
2. Build the project for production
3. Check for any uncommitted changes
4. Create a git tag for the release
5. Push to the deployment branch

Environment: $1 (default: production)

Safety checks:
- Ensure all tests pass
- Ensure no uncommitted changes
- Confirm before proceeding
`;
            fs.writeFileSync(deployCommandPath, deployCommandContent);
            result.created.push('.codebuddy/commands/deploy.md');
        }
    }
    // Create settings.json
    const settingsPath = path.join(codebuddyDir, 'settings.json');
    if (!fs.existsSync(settingsPath) || options.force) {
        const settingsContent = {
            model: 'grok-3-fast',
            maxToolRounds: 400,
            theme: 'default'
        };
        fs.writeFileSync(settingsPath, JSON.stringify(settingsContent, null, 2));
        result.created.push('.codebuddy/settings.json');
    }
    else {
        result.skipped.push('.codebuddy/settings.json (already exists)');
    }
    // Update .gitignore if it exists
    if (options.includeGitignore !== false) {
        const gitignorePath = path.join(workingDirectory, '.gitignore');
        const codebuddyIgnoreEntries = `
# Code Buddy
.codebuddy/sessions/
.codebuddy/history/
.codebuddy/user-settings.json
`;
        if (fs.existsSync(gitignorePath)) {
            const currentContent = fs.readFileSync(gitignorePath, 'utf-8');
            if (!currentContent.includes('# Code Buddy') && !currentContent.includes('# Grok CLI')) {
                fs.appendFileSync(gitignorePath, codebuddyIgnoreEntries);
                result.created.push('.gitignore (updated with Code Buddy entries)');
            }
            else {
                result.skipped.push('.gitignore (already has Code Buddy entries)');
            }
        }
        else {
            fs.writeFileSync(gitignorePath, codebuddyIgnoreEntries.trim());
            result.created.push('.gitignore');
        }
    }
    // Create README for .codebuddy directory
    const readmePath = path.join(codebuddyDir, 'README.md');
    if (!fs.existsSync(readmePath) || options.force) {
        const readmeContent = `# .codebuddy Directory

This directory contains configuration and customization files for [Code Buddy](https://github.com/phuetz/code-buddy).

## Files

- **CODEBUDDY.md** - Custom instructions that Code Buddy follows when working in this project
- **settings.json** - Project-specific settings
- **hooks.json** - Automated hooks (pre-commit, post-edit, etc.)
- **mcp.json** - MCP server configurations (committable, shared with team)
- **security.json** - Security mode configuration
- **commands/** - Custom slash commands

## Custom Commands

Create \`.md\` files in the \`commands/\` directory to add custom slash commands.

Example \`commands/my-command.md\`:
\`\`\`markdown
---
description: My custom command
---

# My Command

Your prompt template here. Use $1, $2 for arguments.
\`\`\`

Then use it with: \`/my-command arg1 arg2\`

## Hooks

Configure automated actions in \`hooks.json\`:
- \`pre-commit\` - Run before git commit
- \`post-edit\` - Run after file edit
- \`on-file-change\` - Run when files change

## MCP Servers

Configure MCP servers in \`mcp.json\` to extend Code Buddy's capabilities.
This file can be committed to share servers with your team.

## Security

Configure security modes in \`security.json\`:
- \`suggest\` - All changes require approval (safest)
- \`auto-edit\` - File edits auto-apply, bash requires approval
- \`full-auto\` - Fully autonomous but sandboxed

## More Information

See the [Code Buddy documentation](https://github.com/phuetz/code-buddy) for more details.
`;
        fs.writeFileSync(readmePath, readmeContent);
        result.created.push('.codebuddy/README.md');
    }
    return result;
}
/**
 * Format init result for display
 */
export function formatInitResult(result) {
    let output = '🚀 Code Buddy Project Initialization\n' + '═'.repeat(50) + '\n\n';
    if (result.created.length > 0) {
        output += '✅ Created:\n';
        for (const item of result.created) {
            output += `   • ${item}\n`;
        }
        output += '\n';
    }
    if (result.skipped.length > 0) {
        output += '⏭️  Skipped:\n';
        for (const item of result.skipped) {
            output += `   • ${item}\n`;
        }
        output += '\n';
    }
    if (result.errors.length > 0) {
        output += '❌ Errors:\n';
        for (const item of result.errors) {
            output += `   • ${item}\n`;
        }
        output += '\n';
    }
    output += '─'.repeat(50) + '\n';
    output += '💡 Edit .codebuddy/CODEBUDDY.md to customize Code Buddy for this project\n';
    output += '📚 See .codebuddy/README.md for documentation';
    return output;
}
//# sourceMappingURL=init-project.js.map