/**
 * Pipeline Configuration — Scan options and mode resolution.
 *
 * Handles both scan mode (full/incremental) and depth (local/verified/deep).
 */

import type {
  ScanMode,
  ScanModeConfig,
  ScanDepth,
  CancellationToken,
  ProgressCallback,
} from '../shared/types'
import { SCAN_MODE_DEFAULTS } from '../shared/types'

export interface ScanOptions {
  /** Enable AI-powered validation and analysis */
  enableAI?: boolean
  /** Maximum files to scan */
  maxFiles?: number
  /** Branch being scanned */
  branch?: string
  /** Scan mode configuration (full vs incremental) */
  scanMode?: ScanMode | ScanModeConfig
  /** Scan depth (cheap/validated/deep) - controls AI usage */
  scanDepth?: ScanDepth
  /** Suppress console.log output (for interactive CLI mode) */
  quiet?: boolean
  /** Cancellation token for aborting scans gracefully */
  cancellationToken?: CancellationToken
  /** Project path for loading suppression config (defaults to cwd) */
  projectPath?: string
  /** Include suppressed findings in output (for --show-suppressed) */
  showSuppressed?: boolean
  /** Include filter audit trail in output (for debugging/explaining dismissals) */
  includeFilterAudit?: boolean
}

/**
 * Resolve scan mode configuration from options
 *
 * Handles both scan mode (full/incremental) and depth (local/verified/deep).
 * Depth controls AI usage:
 * - local: skip AI validation + skip Layer 3
 * - verified: run AI validation, skip Layer 3
 * - deep: run AI validation + Layer 3
 */
export function resolveScanModeConfig(options: ScanOptions): ScanModeConfig {
  const scanModeOption = options.scanMode

  // Determine base mode
  const mode: ScanMode = !scanModeOption ? 'full'
    : typeof scanModeOption === 'string' ? scanModeOption
    : scanModeOption.mode

  const defaults = SCAN_MODE_DEFAULTS[mode]

  // Build config from defaults + explicit overrides
  let config: ScanModeConfig = {
    ...defaults,
    mode,
    ...(typeof scanModeOption === 'object' ? scanModeOption : {}),
  }

  // Apply depth mode (default: 'local')
  const depth = options.scanDepth ?? 'local'
  config.scanDepth = depth

  // Map depth to skipAIValidation/skipLayer3 unless explicitly overridden
  const hasExplicitSkipAI = typeof scanModeOption === 'object' && scanModeOption.skipAIValidation !== undefined
  const hasExplicitSkipL3 = typeof scanModeOption === 'object' && scanModeOption.skipLayer3 !== undefined

  if (!hasExplicitSkipAI) {
    config.skipAIValidation = depth === 'local'
  }
  if (!hasExplicitSkipL3) {
    config.skipLayer3 = depth !== 'deep'
  }

  return config
}
