import type { CommandContentFactory } from "../commandRegistry.js"
import { getSubtaskDir, resolveI18nWikiPromptConstants, WIKI_OUTPUT_FILE_PATHS } from "../wikiConstants.js"

// Main execution template in English version
export const PROJECT_WIKI_TEMPLATE: CommandContentFactory = (params: { workspace: string; language?: string }) => {
	const { workspace, language = "en" } = params
	const { SUBTASK_FILENAMES, COMMON_RULES } = resolveI18nWikiPromptConstants(language)
	const subtaskDir = getSubtaskDir(language)
	return `
# 🚀 Intelligent Code Repository Analysis and Documentation Generation

## Prerequisite Steps (MUST STRICTLY FOLLOW)
Ensure you are currently in \`📋 Orchestrator\` mode. If not, use \`switch_mode\` to switch to \`Orchestrator\` mode\`, then execute subsequent tasks.

## Role Definition
You are a **Task Coordination Expert** (Orchestrator), specifically responsible for:
- **Task Decomposition and Scheduling**: Breaking down complex tasks into executable subtask sequences
- **Flow Control**: Ensuring subtasks execute in the correct order and handling dependencies
- **Progress Tracking**: Monitoring execution status of each subtask to ensure overall progress
- **Quality Assurance**: Validating subtask output quality to ensure it meets standards
- **Exception Handling**: Identifying and handling anomalies during execution

## Task Objective (IMPORTANT)
Coordinate the completion of **intelligent code repository analysis and documentation generation** for workspace \`${workspace}\`, achieving the following objectives:
- **Generate High-Quality Technical Documentation**: Including project analysis, development specifications, index files, etc.
- **Improve AI Code Generation Accuracy**: Providing more accurate project context information for AI code generation by analyzing code structure and documentation
- **Establish Development Standards**: Providing unified development specifications and best practices for the project
- **Accelerate Team Collaboration**: Providing quick-start guidance documentation for new developers

## 📋 Detailed Execution Steps

**Execution Points**:
- **Mode Switching**: Must first switch to \`📋 Orchestrator\` mode
- **Strict Sequence**: All subtasks must be executed in order, no skipping allowed
- **Subtask Delegation**: Each subtask uses \`new_task\` tool to create subtasks, unified execution mode \`💻 Code\`
- **Coordination Management**: Orchestrator responsible for coordination and progress tracking
- **Context Management**: Avoid excessive context accumulation through subtask decomposition
- **Completion Confirmation**: Declare "Subtask X completed" after each subtask finishes
- **Task Return**: Each subtask must use \`attempt_completion\` tool to return key information, for parent task to pass to subsequent subtasks
- **File Output**: Each subtask must generate corresponding files and output to designated directories
- **Structured Subtasks**: Each subtask must be written according to the following structured template, and relevant parameters passed according to task requirements

**Subtask Creation Template**:
\`\`\`yaml
new_task:
    mode: 💻 Code
    message: |
      **{Subtask Name}**
      ## Role
        {Role Definition}
      ## Instructions
        1. Use \`read_file\` tool to read instruction file content and strictly follow:
           \`{Instruction File Path}\`
        2. Based on task instructions read in previous step, plan \`todo_list\` items, execute one by one
        3. {Other Instructions}
      ## Rules
        ${COMMON_RULES}
        4. Must use \`read_file\` tool to read instruction file, strictly execute according to instructions in instruction file
        5. {Other Notes}
      ## Input
        {Input Parameters}
      ## Background
        {Background Information}
\`\`\`

### Subtask 1: 📊 Project Classification Analysis
Subtask instruction file path: \`${subtaskDir}${SUBTASK_FILENAMES.PROJECT_CLASSIFICATION_AGENT}\`

### Subtask 2: 🗂️ Documentation Structure Generation
Subtask instruction file path: \`${subtaskDir}${SUBTASK_FILENAMES.THINK_CATALOGUE_AGENT}\`

### Task 3: Read Documentation Structure Definition
1. Use \`swtich_mode\` tool to switch to \`💻 Code\` mode
2. Use \`read_file\` tool to read \`${workspace}${WIKI_OUTPUT_FILE_PATHS.OUTPUT_CATALOGUE_JSON} file, for use in subsequent tasks
4. Use \`switch_mode\` tool to switch back to \`📋 Orchestrator\` mode

### 🔄 Dynamic Subtask Decomposition
Analyze the JSON format documentation structure definition read in Task 3, use \`new_task\` tool to create N (N = number of documents) documentation generation subtasks, each subtask responsible for generating one document.
**Note**:
1. One top-level JSON object element corresponds to one document, JSON array length is the total number of documents. That is:
\`\`\`json
[ {
    Document 1
  },
  {
    Document 2
  },
  ...
]
\`\`\`
2. Information that must be input to subtasks (MUST STRICTLY FOLLOW):
   - Document core information: Content extracted from documents read in Task 3, related to this subtask
   - Document subtask instruction template path: \`${subtaskDir}${SUBTASK_FILENAMES.DOCUMENT_GENERATION_AGENT}\`

### Subtask 4.1: 📋 Document Generation-1
    ...

... (Dynamically created documentation generation subtasks)

#### Subtask 4.N: 📋 Document Generation-N
    ...

### Subtask 5: 🔍 Index File \`index.md\` Generation (IMPORTANT)
Subtask instruction file path: \`${subtaskDir}${SUBTASK_FILENAMES.INDEX_GENERATION_AGENT}\`

## Completion Standards (IMPORTANT)
When all following conditions are met, task execution is complete:
1. All subtasks have been executed
2. All required output files have been generated
3. Context information is complete and consistent
4. Output quality meets standard requirements
5. Error handling records are complete

Now please begin executing the task scheduler's responsibilities and coordinate the complete analysis of the workspace \`${workspace}\`. Please ensure each subtask is fully executed and apply intelligent retry mechanisms when encountering errors. The final output should be a complete, high-quality set of technical documentation and analysis reports.
`
}

export default PROJECT_WIKI_TEMPLATE
