// ==========================================
// HELPER FUNCTIONS
// ==========================================

import type {
  RiskLevel,
  PersonalDataCategory,
  FindingSeverity,
  VendorStatus,
  ProcessingActivityStatus,
  ContractStatus,
} from './enums'

/**
 * Calculate risk level from score
 */
export function getRiskLevelFromScore(score: number): RiskLevel {
  if (score <= 4) return 'LOW'
  if (score <= 9) return 'MEDIUM'
  if (score <= 16) return 'HIGH'
  return 'CRITICAL'
}

/**
 * Calculate risk score from likelihood and impact
 */
export function calculateRiskScore(likelihood: number, impact: number): number {
  return likelihood * impact
}

/**
 * Check if data category is special (Art. 9 DSGVO)
 */
export function isSpecialCategory(category: PersonalDataCategory): boolean {
  const specialCategories: PersonalDataCategory[] = [
    'HEALTH_DATA',
    'GENETIC_DATA',
    'BIOMETRIC_DATA',
    'RACIAL_ETHNIC',
    'POLITICAL_OPINIONS',
    'RELIGIOUS_BELIEFS',
    'TRADE_UNION',
    'SEX_LIFE',
    'CRIMINAL_DATA',
  ]
  return specialCategories.includes(category)
}

/**
 * Check if country has adequacy decision
 */
export function hasAdequacyDecision(countryCode: string): boolean {
  const adequateCountries = [
    'AD', 'AR', 'CA', 'FO', 'GG', 'IL', 'IM', 'JP', 'JE', 'NZ', 'KR', 'CH', 'GB', 'UY',
    // EU/EEA countries
    'AT', 'BE', 'BG', 'HR', 'CY', 'CZ', 'DK', 'EE', 'FI', 'FR', 'DE', 'GR', 'HU',
    'IE', 'IT', 'LV', 'LT', 'LU', 'MT', 'NL', 'PL', 'PT', 'RO', 'SK', 'SI', 'ES', 'SE',
    'IS', 'LI', 'NO',
  ]
  return adequateCountries.includes(countryCode.toUpperCase())
}

/**
 * Generate VVT ID
 */
export function generateVVTId(existingIds: string[]): string {
  const year = new Date().getFullYear()
  const prefix = `VVT-${year}-`

  const existingNumbers = existingIds
    .filter(id => id.startsWith(prefix))
    .map(id => parseInt(id.replace(prefix, ''), 10))
    .filter(n => !isNaN(n))

  const nextNumber = existingNumbers.length > 0 ? Math.max(...existingNumbers) + 1 : 1
  return `${prefix}${nextNumber.toString().padStart(3, '0')}`
}

/**
 * Format date for display
 */
export function formatDate(date: Date | string | undefined): string {
  if (!date) return '-'
  const d = typeof date === 'string' ? new Date(date) : date
  return d.toLocaleDateString('de-DE', {
    day: '2-digit',
    month: '2-digit',
    year: 'numeric',
  })
}

/**
 * Get severity color class
 */
export function getSeverityColor(severity: FindingSeverity): string {
  switch (severity) {
    case 'LOW': return 'text-blue-600 bg-blue-100'
    case 'MEDIUM': return 'text-yellow-600 bg-yellow-100'
    case 'HIGH': return 'text-orange-600 bg-orange-100'
    case 'CRITICAL': return 'text-red-600 bg-red-100'
  }
}

/**
 * Get status color class
 */
export function getStatusColor(status: VendorStatus | ProcessingActivityStatus | ContractStatus): string {
  switch (status) {
    case 'ACTIVE':
    case 'APPROVED':
    case 'SIGNED':
      return 'text-green-600 bg-green-100'
    case 'DRAFT':
    case 'PENDING_REVIEW':
      return 'text-yellow-600 bg-yellow-100'
    case 'REVIEW':
    case 'INACTIVE':
      return 'text-blue-600 bg-blue-100'
    case 'ARCHIVED':
    case 'EXPIRED':
    case 'TERMINATED':
      return 'text-gray-600 bg-gray-100'
    default:
      return 'text-gray-600 bg-gray-100'
  }
}
