use crate::llm::{CommitContext, ReviewType};

/// 默认的 commit prompt 模板
const DEFAULT_COMMIT_PROMPT: &str = r#"You are an expert software engineer reviewing a git diff to generate a concise, informative commit message.

## Git Diff:
```
{diff}
```

## Context:
- Files changed: {files_changed}
- Insertions: {insertions}
- Deletions: {deletions}
{branch_info}

## Instructions:
1. Analyze the changes carefully
2. Generate a commit message following conventional commits format
3. First line: type(scope): brief summary (max 72 chars)
4. Blank line
5. Body: explain what and why (not how), if necessary
6. Keep it concise but informative

Common types: feat, fix, docs, style, refactor, test, chore

Output only the commit message, no explanations."#;

/// 默认的 review prompt 模板
const DEFAULT_REVIEW_PROMPT: &str = r#"You are an expert code reviewer. Review the following code changes carefully.

## Code to Review:
```
{diff}
```

## Review Criteria:
1. **Correctness**: Are there any bugs or logical errors?
2. **Security**: Are there any security vulnerabilities?
3. **Performance**: Are there any performance issues?
4. **Maintainability**: Is the code readable and maintainable?
5. **Best Practices**: Does it follow best practices?"#;

/// 默认的 JSON 输出格式说明（用于自定义 review prompt 时追加）
const DEFAULT_JSON_FORMAT: &str = r#"## Output Format:
Provide your review in JSON format
Do not include any explanations outside the JSON structure. Format as follows:
{{
  "summary": "Brief overall assessment",
  "issues": [
    {{
      "severity": "critical" | "warning" | "info",
      "description": "Issue description",
      "file": "filename (if applicable)",
      "line": line_number (if applicable)
    }}
  ],
  "suggestions": [
    "Improvement suggestion 1"
  ]
}}

If no issues found, return empty issues array but provide constructive suggestions."#;

/// 构建 commit message 生成的 prompt
pub fn build_commit_prompt(
    diff: &str,
    context: &CommitContext,
    custom_template: Option<&str>,
) -> String {
    let template = match custom_template {
        Some(t) if !t.contains("{diff}") => {
            // 自定义模板缺少 {diff}，追加默认的 diff 部分
            format!(
                "{}\n\n## Git Diff:\n```\n{{diff}}\n```\n\n## Context:\n- Files: {{files_changed}}\n- Changes: +{{insertions}} -{{deletions}}",
                t
            )
        }
        Some(t) => t.to_string(),
        None => DEFAULT_COMMIT_PROMPT.to_string(),
    };

    let branch_info = context
        .branch_name
        .as_ref()
        .map(|b| format!("- Branch: {}", b))
        .unwrap_or_default();

    let mut prompt = template
        .replace("{diff}", diff)
        .replace("{files_changed}", &context.files_changed.join(", "))
        .replace("{insertions}", &context.insertions.to_string())
        .replace("{deletions}", &context.deletions.to_string())
        .replace(
            "{branch_name}",
            context.branch_name.as_deref().unwrap_or(""),
        )
        .replace("{branch_info}", &branch_info);

    // 在 prompt 尾部追加用户反馈
    if !context.user_feedback.is_empty() {
        prompt.push_str("\n\n## Additional User Requirements:\n");
        for (i, fb) in context.user_feedback.iter().enumerate() {
            prompt.push_str(&format!("{}. {}\n", i + 1, fb));
        }
    }

    prompt
}

/// 构建代码审查的 prompt
pub fn build_review_prompt(
    diff: &str,
    _review_type: &ReviewType,
    custom_template: Option<&str>,
) -> String {
    let mut template = custom_template
        .map(|t| t.to_string())
        .unwrap_or_else(|| DEFAULT_REVIEW_PROMPT.to_string());

    // 检测并追加缺失的 {diff}
    if !template.contains("{diff}") {
        template.push_str("\n\n## Code to Review:\n```\n{diff}\n```");
    }

    // 始终追加 JSON 格式说明
    template.push_str("\n\n");
    template.push_str(DEFAULT_JSON_FORMAT);

    template.replace("{diff}", diff)
}
