/**
 * Controls Library - Helper Functions
 *
 * Query, filter, and analysis utilities for the controls library.
 */

import { Control, ControlDomain, ReviewFrequency, LocalizedText } from '../types'
import { CONTROLS_LIBRARY } from './controls-all'

/**
 * Get all controls
 */
export function getAllControls(): Control[] {
  return CONTROLS_LIBRARY
}

/**
 * Get controls by domain
 */
export function getControlsByDomain(domain: ControlDomain): Control[] {
  return CONTROLS_LIBRARY.filter((c) => c.domain === domain)
}

/**
 * Get control by ID
 */
export function getControlById(id: string): Control | undefined {
  return CONTROLS_LIBRARY.find((c) => c.id === id)
}

/**
 * Get required controls
 */
export function getRequiredControls(): Control[] {
  return CONTROLS_LIBRARY.filter((c) => c.isRequired)
}

/**
 * Get controls by frequency
 */
export function getControlsByFrequency(frequency: ReviewFrequency): Control[] {
  return CONTROLS_LIBRARY.filter((c) => c.defaultFrequency === frequency)
}

/**
 * Get controls applicable to vendors
 */
export function getVendorControls(): Control[] {
  return CONTROLS_LIBRARY.filter((c) =>
    ['TRANSFER', 'AUDIT', 'DELETION', 'INCIDENT', 'SUBPROCESSOR', 'TOM', 'CONTRACT', 'DATA_SUBJECT', 'SECURITY', 'GOVERNANCE'].includes(c.domain)
  )
}

/**
 * Get controls applicable to processing activities
 */
export function getProcessingActivityControls(): Control[] {
  return CONTROLS_LIBRARY.filter((c) =>
    ['TOM', 'DATA_SUBJECT', 'GOVERNANCE', 'SECURITY'].includes(c.domain)
  )
}

/**
 * Group controls by domain
 */
export function getControlsGroupedByDomain(): Map<ControlDomain, Control[]> {
  const grouped = new Map<ControlDomain, Control[]>()

  for (const control of CONTROLS_LIBRARY) {
    const existing = grouped.get(control.domain) || []
    grouped.set(control.domain, [...existing, control])
  }

  return grouped
}

/**
 * Get domain metadata
 */
export function getControlDomainMeta(domain: ControlDomain): LocalizedText {
  const meta: Record<ControlDomain, LocalizedText> = {
    TRANSFER: { de: 'Drittlandtransfer', en: 'Third Country Transfer' },
    AUDIT: { de: 'Audit & Prüfung', en: 'Audit & Review' },
    DELETION: { de: 'Löschung', en: 'Deletion' },
    INCIDENT: { de: 'Incident Response', en: 'Incident Response' },
    SUBPROCESSOR: { de: 'Unterauftragnehmer', en: 'Sub-Processors' },
    TOM: { de: 'Technische/Org. Maßnahmen', en: 'Technical/Org. Measures' },
    CONTRACT: { de: 'Vertragliche Grundlagen', en: 'Contractual Basics' },
    DATA_SUBJECT: { de: 'Betroffenenrechte', en: 'Data Subject Rights' },
    SECURITY: { de: 'Sicherheit', en: 'Security' },
    GOVERNANCE: { de: 'Governance', en: 'Governance' },
  }

  return meta[domain]
}

/**
 * Calculate control coverage
 */
export function calculateControlCoverage(
  controlIds: string[],
  domain?: ControlDomain
): { covered: number; total: number; percentage: number } {
  const targetControls = domain
    ? getControlsByDomain(domain)
    : getRequiredControls()

  const covered = targetControls.filter((c) => controlIds.includes(c.id)).length

  return {
    covered,
    total: targetControls.length,
    percentage: targetControls.length > 0 ? Math.round((covered / targetControls.length) * 100) : 0,
  }
}
