use crate::analysis::findings::LlmSeverity;
use crate::analysis::response_contract::{
CATEGORY, CODE_SNIPPET, COMPILE_FAILURE, ISSUES, LINE, MESSAGE, SEVERITY, SUGGESTION, SUMMARY,
};
use crate::languages::spec::LanguageSupport;
pub fn build_analysis_prompt(language: &LanguageSupport) -> String {
let conventions = conventions_block(language);
let display_name = language.display_name;
let severities = LlmSeverity::alternation();
let issues = ISSUES;
let summary = SUMMARY;
let line = LINE;
let severity = SEVERITY;
let category = CATEGORY;
let message = MESSAGE;
let suggestion = SUGGESTION;
let code_snippet = CODE_SNIPPET;
let compile_failure = COMPILE_FAILURE;
format!(
"You are an expert {display_name} code reviewer.\n\
Analyze the following code and identify issues in these categories:\n\
\n\
1. **Bugs & Logic Errors**: Incorrect logic, unhandled edge cases,\n\
potential crashes, undefined variables, type errors\n\
2. **Security Issues**: Injection, path traversal, unsafe deserialization,\n\
hardcoded secrets, weak cryptography\n\
3. **Best Practices**: Poor naming, code smells, anti-patterns\n\
4. **Performance**: Inefficient algorithms, unnecessary work,\n\
blocking I/O, memory leaks\n\
\n\
{conventions}\
For each issue found, provide:\n\
- Line number (approximate if exact line is unclear)\n\
- Severity: critical (security vulnerabilities, crashes), high (bugs,\n\
serious issues), medium (best practices, moderate issues), low (minor\n\
improvements), info (suggestions)\n\
- Category: bug, security, best-practice, performance, style, maintainability\n\
- Clear message explaining the issue\n\
- Specific, actionable suggestion for fixing it\n\
- The problematic code snippet\n\
- Whether the finding explicitly claims the code cannot compile\n\
\n\
**Important instructions:**\n\
- Only report genuine issues, not false positives\n\
- Be specific about line numbers - estimate if needed\n\
- Provide actionable suggestions, not vague advice\n\
- Focus on correctness, security, and maintainability\n\
- The input is a line-numbered excerpt. Report the finding's `line`\n\
as the number shown in the gutter, never an offset into the\n\
excerpt. The excerpt itself states which lines are in scope.\n\
- Do not report subjective style issues, and do not report anything a\n\
formatter or linter would catch: those run separately and deterministically\n\
\n\
Return your analysis as valid JSON matching this exact schema:\n\
{{\n\
\"{issues}\": [\n\
{{\n\
\"{line}\": <line_number>,\n\
\"{severity}\": \"<{severities}>\",\n\
\"{category}\": \"<bug|security|best-practice|performance|style|maintainability>\",\n\
\"{message}\": \"<clear description of the issue>\",\n\
\"{suggestion}\": \"<specific recommendation for fixing>\",\n\
\"{code_snippet}\": \"<the problematic code>\",\n\
\"{compile_failure}\": <true|false>\n\
}}\n\
],\n\
\"{summary}\": \"<overall assessment of code quality>\"\n\
}}\n\
\n\
If no issues are found, return:\n\
{{\n\
\"{issues}\": [],\n\
\"{summary}\": \"No significant issues found. Code quality looks good.\"\n\
}}\n"
)
}
fn conventions_block(language: &LanguageSupport) -> String {
if language.conventions.is_empty() {
return String::new();
}
let mut out = format!("**{}-specific concerns:**\n", language.display_name);
for concern in language.conventions {
out.push_str("- ");
out.push_str(concern);
out.push('\n');
}
out
}