/**
 * Report Enrichment — Adds rule registry metadata and framework-specific fixes to findings.
 *
 * Extracted from the orchestrator. Runs after detection + aggregation, before scoring.
 */

import type { Vulnerability } from '../shared/types'
import type { ProjectContext } from '../model/project-context'
import { getRuleMetadata } from '../shared/rules'
import { getFrameworkFix } from '../shared/rules/framework-fixes'

/**
 * Enrich findings with metadata from the rule registry (PRO-82)
 * Sets default impact, evidence, fixSteps, and references from registry
 *
 * PRO-83: When projectContext is provided, uses framework-aware fix suggestions
 * that are tailored to the user's detected tech stack (e.g., Prisma-specific
 * SQL injection fixes instead of generic advice).
 *
 * These can be overridden later by AI-generated content
 */
export function enrichWithMetadata(
  findings: Vulnerability[],
  projectContext?: ProjectContext
): Vulnerability[] {
  return findings.map(f => {
    const metadata = getRuleMetadata(f.category)
    if (!metadata) return f

    // PRO-83: Check for framework-specific fix suggestions
    let fixSteps = metadata.fixSteps
    if (projectContext) {
      const frameworkFix = getFrameworkFix(
        f.category,
        projectContext.frameworks,
        projectContext.dataAccess
      )
      if (frameworkFix) {
        fixSteps = frameworkFix.fixSteps
      }
    }

    return {
      ...f,
      // Set defaults from registry (AI can override later)
      impact: f.impact || metadata.whyItMatters,
      evidence: f.evidence || metadata.evidence,
      fixSteps: f.fixSteps || fixSteps,
      references: f.references || metadata.references,
    }
  })
}
