/**
 * 内置 Commands
 *
 * 提供一些常用的预定义 Commands
 */

import type { Command } from '../types.js';

/**
 * /init - 初始化项目配置
 */
export const initCommand: Command = {
  name: 'init',
  description: '分析代码库并创建 AGENTS.md 配置文件',
  template: `Please analyze this codebase and create an AGENTS.md file containing:

1. **Build/lint/test commands** - Document how to build, lint, and test the project
2. **Code style guidelines** - Document the coding conventions used
3. **Project structure** - Overview of the directory structure
4. **Key dependencies** - Important libraries and frameworks used

Additional context: $ARGUMENTS

Start by exploring the project structure and package configuration files.`,
  agent: 'explore',
  subtask: false,
  source: 'builtin',
};

/**
 * /review - 代码审查
 */
export const reviewCommand: Command = {
  name: 'review',
  description: '审查代码变更',
  template: `You are a code reviewer. Your job is to review code changes.

Input: $ARGUMENTS

## Determining What to Review

Based on the input, determine which type of review to perform:

1. **No arguments**: Review uncommitted changes using \`git diff\`
2. **Commit hash**: Review that specific commit using \`git show $ARGUMENTS\`
3. **Branch name**: Compare to specified branch using \`git diff $ARGUMENTS...HEAD\`
4. **PR URL**: Review the pull request (if gh CLI is available)

## What to Look For

- **Bugs**: Logic errors, edge cases, null/undefined handling, security issues
- **Structure**: Does it follow existing patterns? Is it maintainable?
- **Performance**: Only flag if obviously problematic
- **Tests**: Are there adequate tests for the changes?

## Output Format

Provide a structured review with:
1. Summary of changes
2. Issues found (categorized by severity)
3. Suggestions for improvement
4. Positive observations`,
  agent: 'code-reviewer',
  subtask: false,
  source: 'builtin',
};

/**
 * /test - 运行并修复测试
 */
export const testCommand: Command = {
  name: 'test',
  description: '运行测试并修复失败的测试',
  template: `Run the test suite and analyze the results.

Focus on: $ARGUMENTS

## Instructions

1. First, run the test command for this project
2. If tests fail, analyze the failures
3. Identify the root cause of each failure
4. Fix the failing tests or the code they're testing
5. Re-run tests to verify fixes

If no specific focus is provided, run all tests.`,
  agent: 'general',
  subtask: false,
  source: 'builtin',
};

/**
 * /fix - 修复问题
 */
export const fixCommand: Command = {
  name: 'fix',
  description: '修复指定的问题或错误',
  template: `Please fix the following issue:

$ARGUMENTS

## Instructions

1. Understand the problem described
2. Locate the relevant code
3. Analyze the root cause
4. Implement a fix
5. Verify the fix works
6. Check for any side effects`,
  agent: 'general',
  subtask: false,
  source: 'builtin',
};

/**
 * /explain - 解释代码
 */
export const explainCommand: Command = {
  name: 'explain',
  description: '解释代码或概念',
  template: `Please explain the following:

$ARGUMENTS

Provide a clear, structured explanation that includes:
1. Overview - What it does at a high level
2. How it works - Step by step breakdown
3. Key concepts - Important patterns or techniques used
4. Examples - Practical usage examples if applicable`,
  agent: 'general',
  subtask: false,
  source: 'builtin',
};

/**
 * /commit - 生成 commit 消息
 */
export const commitCommand: Command = {
  name: 'commit',
  description: '根据变更生成 Git commit 消息',
  template: `Generate a Git commit message for the current changes.

Additional context: $ARGUMENTS

## Instructions

1. Run \`git diff --staged\` to see staged changes (or \`git diff\` if nothing staged)
2. Analyze the changes
3. Generate a commit message following Conventional Commits format:
   - feat: New feature
   - fix: Bug fix
   - docs: Documentation
   - style: Formatting
   - refactor: Code restructuring
   - test: Adding tests
   - chore: Maintenance

4. Format:
   - First line: type(scope): short description (50 chars max)
   - Blank line
   - Body: Detailed explanation if needed

5. Present the commit message for review`,
  agent: 'general',
  subtask: false,
  source: 'builtin',
};

/**
 * /help - 显示帮助
 */
export const helpCommand: Command = {
  name: 'help',
  description: '显示可用的命令和帮助信息',
  template: `Show help information about available commands.

Topic: $ARGUMENTS

If a specific command is mentioned, provide detailed help for that command.
Otherwise, list all available commands with their descriptions.`,
  agent: 'general',
  subtask: false,
  source: 'builtin',
};

/**
 * 所有内置 Commands
 */
export const builtinCommands: Command[] = [
  initCommand,
  reviewCommand,
  testCommand,
  fixCommand,
  explainCommand,
  commitCommand,
  helpCommand,
];
