/**
 * Shared types for the security benchmark test suite
 */

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

/**
 * A test fixture containing a file to scan and expected results
 */
export interface TestFixture {
  /** Descriptive name for the test */
  name: string
  /** The file to scan */
  file: ScanFile
  /** Whether findings are expected (true positive test) or not (false negative test) */
  expectFindings: boolean
  /** Categories expected to be found (for true positive tests) */
  expectedCategories?: VulnerabilityCategory[]
  /** Description of what this test validates */
  description?: string
  /**
   * For false negative tests: Allow specific low-severity findings
   * Some edge cases may generate info findings (e.g., test files with eval())
   * By default, false negative tests fail on medium+ severity
   */
  allowedInfoFindings?: {
    category: VulnerabilityCategory
    maxCount: number
    reason: string
  }[]
}

/**
 * Result from running a single test
 */
export interface TestResult {
  name: string
  file: ScanFile
  layer1Findings: Vulnerability[]
  layer2Findings: Vulnerability[]
  expectedCategories: VulnerabilityCategory[]
  unexpectedCategories: VulnerabilityCategory[]
  passed: boolean
  failureReason?: string
  /** Findings that were unexpected but acceptable (info severity) */
  acceptableInfoFindings?: Vulnerability[]
  /** Findings that caused the test to fail (medium+) */
  problematicFindings?: Vulnerability[]
}

/**
 * A group of related test fixtures
 */
export interface TestGroup {
  /** Name of the test group (e.g., "Hardcoded Secrets") */
  name: string
  /** Tier classification */
  tier: 'A' | 'B' | 'C'
  /** Layer this test group primarily targets */
  layer: 1 | 2
  /** Description of the test group */
  description: string
  /** True positive test fixtures */
  truePositives: TestFixture[]
  /** False negative test fixtures (should NOT flag) */
  falseNegatives: TestFixture[]
}

/**
 * Detector performance metrics
 */
export interface DetectorMetrics {
  /** Detector/category name */
  name: string
  /** True positives detected */
  truePositives: number
  /** False positives (unexpected findings on safe code) */
  falsePositives: number
  /** False negatives (missed detections) */
  falseNegatives: number
  /** Precision: TP / (TP + FP) */
  precision: number
  /** Recall: TP / (TP + FN) */
  recall: number
}

/**
 * Severity distribution metrics
 */
export interface SeverityMetrics {
  critical: number
  high: number
  medium: number
  low: number
  info: number
}

/**
 * Detailed benchmark metrics
 */
export interface BenchmarkMetrics {
  /** Overall detection statistics */
  detection: {
    totalVulnerabilitiesDetected: number
    truePositiveDetections: number
    falsePositiveDetections: number
    missedVulnerabilities: number
  }
  /** Performance by detector/category */
  byDetector: DetectorMetrics[]
  /** Severity distribution of all findings */
  severityDistribution: SeverityMetrics
  /** Severity distribution of false positives */
  falsePositiveSeverity: SeverityMetrics
  /** Coverage statistics */
  coverage: {
    totalCategories: number
    categoriesTested: number
    coveragePercent: number
    untestedCategories: string[]
  }
  /** Tier statistics */
  byTier: {
    tierA: { tested: number; passed: number }
    tierB: { tested: number; passed: number }
    tierC: { tested: number; passed: number }
  }
}

/**
 * Summary of benchmark results
 */
export interface BenchmarkSummary {
  truePositivePassed: number
  truePositiveFailed: number
  falseNegativePassed: number
  falseNegativeFailed: number
  totalTests: number
  passedTests: number
  passRate: number
  results: TestResult[]
  /** Detailed performance metrics */
  metrics: BenchmarkMetrics
}
