/**
 * Type definitions for AI validation module
 */

import type { Vulnerability, VulnerabilitySeverity, VulnerabilityCategory, ValidationStatus } from '../shared/types'

// ============================================================================
// Cost Monitoring Types
// ============================================================================

export interface ValidationStats {
  /** Total findings processed (input) */
  totalFindings: number
  /** Findings that went through AI validation */
  validatedFindings: number
  /** Findings confirmed as true positives */
  confirmedFindings: number
  /** Findings dismissed as false positives */
  dismissedFindings: number
  /** Findings with severity adjusted down */
  downgradedFindings: number
  /** Findings auto-dismissed before AI (test files, etc.) */
  autoDismissedFindings: number
  /** Estimated input tokens used */
  estimatedInputTokens: number
  /** Estimated output tokens used */
  estimatedOutputTokens: number
  /** Estimated cost in USD (based on Haiku pricing) */
  estimatedCost: number
  /** Number of API calls made */
  apiCalls: number
  /** Cache creation tokens (first write to cache) */
  cacheCreationTokens: number
  /** Cache read tokens (subsequent reads from cache) */
  cacheReadTokens: number
  /** Cache hit rate (0-1) */
  cacheHitRate: number
}

export interface AIValidationResult {
  vulnerabilities: Vulnerability[]
  stats: ValidationStats
}

// ============================================================================
// Validation Types
// ============================================================================

export interface ValidationResult {
  index: number
  keep: boolean
  // Optimized format: single notes field (replaces reason + validationNotes)
  notes?: string  // Only for keep=true, concise explanation
  adjustedSeverity?: VulnerabilitySeverity | null
  // Legacy fields for backward compatibility during parsing
  reason?: string
  validationNotes?: string
  // Actionable output fields (PRO-82) - only for kept findings
  impact?: string  // Context-aware explanation of why this matters
  fixSuggestion?: string  // Specific fix recommendation for this code context
}

export interface AIFinding {
  lineNumber: number
  severity: VulnerabilitySeverity
  category: VulnerabilityCategory
  title: string
  description: string
  suggestedFix: string
}

// ============================================================================
// Context Types
// ============================================================================

export interface Layer3Context {
  /** Middleware configuration from project scan */
  middlewareConfig?: {
    hasAuthMiddleware: boolean
    authType?: string
    protectedPaths: string[]
  }
  /** Auth helper context */
  authHelpers?: {
    hasThrowingHelpers: boolean
    summary: string
  }
  /** Additional context string */
  additionalContext?: string
}

// ============================================================================
// Auto-Dismiss Types
// ============================================================================

export interface AutoDismissRule {
  name: string
  check: (finding: Vulnerability, fileContent?: string) => boolean
  reason: string
}

// ============================================================================
// Stats Accumulator (for thread-safe parallel processing)
// ============================================================================

export interface StatsAccumulator {
  apiCalls: number
  estimatedInputTokens: number
  estimatedOutputTokens: number
  cacheReadTokens: number
  estimatedCost: number
  validatedFindings: number
  confirmedFindings: number
  dismissedFindings: number
  downgradedFindings: number
}

export function createStatsAccumulator(): StatsAccumulator {
  return {
    apiCalls: 0,
    estimatedInputTokens: 0,
    estimatedOutputTokens: 0,
    cacheReadTokens: 0,
    estimatedCost: 0,
    validatedFindings: 0,
    confirmedFindings: 0,
    dismissedFindings: 0,
    downgradedFindings: 0,
  }
}

export function createInitialStats(totalFindings: number): ValidationStats {
  return {
    totalFindings,
    validatedFindings: 0,
    confirmedFindings: 0,
    dismissedFindings: 0,
    downgradedFindings: 0,
    autoDismissedFindings: 0,
    estimatedInputTokens: 0,
    estimatedOutputTokens: 0,
    estimatedCost: 0,
    apiCalls: 0,
    cacheCreationTokens: 0,
    cacheReadTokens: 0,
    cacheHitRate: 0,
  }
}
