/**
 * @fileoverview Prompt Builder - Builds LLM prompt with few-shot examples
 * @module morph-spec/llm/prompt-builder
 */

import { getFewShotExamples } from './few-shot-examples.js';

/**
 * @typedef {import('../types/index.js').SanitizedContext} SanitizedContext
 */

/**
 * Build structured prompt for LLM with few-shot examples
 * @param {SanitizedContext} context - Sanitized project context
 * @returns {string} Formatted prompt
 */
export function buildPrompt(context) {
  const examples = getFewShotExamples();

  return `You are a project analysis AI. Analyze the following project context and return a JSON configuration.

## Task
Analyze the project structure, dependencies, and files to determine:
- Project name, description, and type
- Technology stack (frontend, backend, database, hosting)
- Architecture pattern
- Project structure description
- Code conventions

Return ONLY valid JSON matching the schema below. Do not include any explanatory text outside the JSON.

## Few-Shot Examples

${examples.map((ex, i) => `
### Example ${i + 1}

**Input:**
\`\`\`json
${JSON.stringify(ex.input, null, 2)}
\`\`\`

**Output:**
\`\`\`json
${JSON.stringify(ex.output, null, 2)}
\`\`\`
`).join('\n')}

## Project Context to Analyze

\`\`\`json
${JSON.stringify(context, null, 2)}
\`\`\`

## JSON Schema (Output Format)

\`\`\`json
{
  "name": "string (project name)",
  "description": "string (1-2 sentences)",
  "type": "monorepo | blazor-server | nextjs | cli-tool | dotnet-api | other",
  "stack": {
    "frontend": { "tech": "string", "version": "string", "details": "string" } | null,
    "backend": { "tech": "string", "version": "string", "details": "string" },
    "database": { "tech": "string", "version": "string", "details": "string" } | null,
    "hosting": "string | null"
  },
  "architecture": "clean-architecture | monolith | microservices | layered | other",
  "projectStructure": "string (describe folder structure)",
  "conventions": "string (coding conventions detected)",
  "repository": "string | null",
  "hasAzure": boolean,
  "hasDocker": boolean,
  "hasDevOps": boolean,
  "confidence": number (0-100),
  "warnings": ["string"] (uncertainties or issues)
}
\`\`\`

## Rules
1. Return ONLY the JSON object, no additional text
2. Infer missing information from context (package.json, .csproj files, directory structure)
3. Set \`confidence\` based on how clear the project structure is (90-100 = very clear, 70-89 = moderate, <70 = unclear)
4. Add \`warnings\` for any uncertainties or missing critical information
5. If no frontend is detected, set \`stack.frontend\` to null
6. If no database is detected, set \`stack.database\` to null
7. Detect project type based on:
   - \`nextjs\`: package.json has "next" dependency
   - \`blazor-server\`: .csproj files with Blazor.Server references
   - \`dotnet-api\`: .csproj files without Blazor
   - \`cli-tool\`: package.json with bin field or .csproj with OutputType=Exe
   - \`monorepo\`: multiple top-level directories like frontend/, backend/, packages/
   - \`other\`: if none of the above match

## Response
Return the JSON analysis now:`;
}
