/**
 * Vendor Templates and Categories
 *
 * Pre-defined vendor templates and risk profiles
 */

import {
  VendorFormData,
  VendorRole,
  ServiceCategory,
  DataAccessLevel,
  TransferMechanismType,
  DocumentType,
  ReviewFrequency,
  LocalizedText,
  PersonalDataCategory,
} from '../types'

export interface VendorTemplate {
  id: string
  name: LocalizedText
  description: LocalizedText
  serviceCategory: ServiceCategory
  suggestedRole: VendorRole
  suggestedDataAccess: DataAccessLevel
  suggestedTransferMechanisms: TransferMechanismType[]
  suggestedContractTypes: DocumentType[]
  typicalDataCategories: PersonalDataCategory[]
  typicalCertifications: string[]
  inherentRiskFactors: RiskFactorWeight[]
  commonProviders: string[]
}

export interface RiskFactorWeight {
  factor: string
  weight: number // 0-1
  description: LocalizedText
}

export interface CountryRiskProfile {
  code: string // ISO 3166-1 alpha-2
  name: LocalizedText
  isEU: boolean
  isEEA: boolean
  hasAdequacyDecision: boolean
  adequacyDecisionDate?: string
  riskLevel: 'LOW' | 'MEDIUM' | 'HIGH' | 'VERY_HIGH'
  notes?: LocalizedText
}

// ==========================================
// VENDOR TEMPLATES
// ==========================================

export const VENDOR_TEMPLATES: VendorTemplate[] = [
  // Cloud & Infrastructure
  {
    id: 'tpl-vendor-cloud-iaas',
    name: { de: 'Cloud IaaS-Anbieter', en: 'Cloud IaaS Provider' },
    description: {
      de: 'Infrastructure-as-a-Service Anbieter (AWS, Azure, GCP)',
      en: 'Infrastructure-as-a-Service provider (AWS, Azure, GCP)',
    },
    serviceCategory: 'CLOUD_INFRASTRUCTURE',
    suggestedRole: 'PROCESSOR',
    suggestedDataAccess: 'CONTENT',
    suggestedTransferMechanisms: ['SCC_PROCESSOR', 'BCR'],
    suggestedContractTypes: ['AVV', 'MSA', 'SLA', 'TOM_ANNEX'],
    typicalDataCategories: ['NAME', 'CONTACT', 'USAGE_DATA', 'IP_ADDRESS'],
    typicalCertifications: ['ISO 27001', 'SOC 2', 'C5'],
    inherentRiskFactors: [
      { factor: 'data_volume', weight: 0.9, description: { de: 'Hohes Datenvolumen', en: 'High data volume' } },
      { factor: 'criticality', weight: 0.9, description: { de: 'Geschäftskritisch', en: 'Business critical' } },
      { factor: 'sub_processors', weight: 0.7, description: { de: 'Viele Unterauftragnehmer', en: 'Many sub-processors' } },
    ],
    commonProviders: ['AWS', 'Microsoft Azure', 'Google Cloud Platform', 'Hetzner', 'OVH'],
  },
  {
    id: 'tpl-vendor-hosting',
    name: { de: 'Webhosting-Anbieter', en: 'Web Hosting Provider' },
    description: {
      de: 'Hosting von Websites und Webanwendungen',
      en: 'Hosting of websites and web applications',
    },
    serviceCategory: 'HOSTING',
    suggestedRole: 'PROCESSOR',
    suggestedDataAccess: 'ADMINISTRATIVE',
    suggestedTransferMechanisms: ['ADEQUACY_DECISION', 'SCC_PROCESSOR'],
    suggestedContractTypes: ['AVV', 'MSA', 'SLA'],
    typicalDataCategories: ['IP_ADDRESS', 'USAGE_DATA', 'LOGIN_DATA'],
    typicalCertifications: ['ISO 27001'],
    inherentRiskFactors: [
      { factor: 'data_volume', weight: 0.6, description: { de: 'Mittleres Datenvolumen', en: 'Medium data volume' } },
      { factor: 'criticality', weight: 0.7, description: { de: 'Wichtig für Betrieb', en: 'Important for operations' } },
    ],
    commonProviders: ['Hetzner', 'All-Inkl', 'IONOS', 'Strato', 'DigitalOcean'],
  },
  {
    id: 'tpl-vendor-cdn',
    name: { de: 'CDN-Anbieter', en: 'CDN Provider' },
    description: {
      de: 'Content Delivery Network für schnelle Inhaltsauslieferung',
      en: 'Content Delivery Network for fast content delivery',
    },
    serviceCategory: 'CDN',
    suggestedRole: 'PROCESSOR',
    suggestedDataAccess: 'POTENTIAL',
    suggestedTransferMechanisms: ['SCC_PROCESSOR'],
    suggestedContractTypes: ['AVV', 'MSA'],
    typicalDataCategories: ['IP_ADDRESS', 'USAGE_DATA'],
    typicalCertifications: ['ISO 27001', 'SOC 2'],
    inherentRiskFactors: [
      { factor: 'data_transit', weight: 0.5, description: { de: 'Daten im Transit', en: 'Data in transit' } },
      { factor: 'global_presence', weight: 0.6, description: { de: 'Globale Präsenz', en: 'Global presence' } },
    ],
    commonProviders: ['Cloudflare', 'Fastly', 'Akamai', 'AWS CloudFront'],
  },

  // Business Software
  {
    id: 'tpl-vendor-crm',
    name: { de: 'CRM-System', en: 'CRM System' },
    description: {
      de: 'Customer Relationship Management System',
      en: 'Customer Relationship Management System',
    },
    serviceCategory: 'CRM',
    suggestedRole: 'PROCESSOR',
    suggestedDataAccess: 'CONTENT',
    suggestedTransferMechanisms: ['SCC_PROCESSOR', 'BCR'],
    suggestedContractTypes: ['AVV', 'MSA', 'SLA', 'TOM_ANNEX'],
    typicalDataCategories: ['NAME', 'CONTACT', 'ADDRESS', 'COMMUNICATION_DATA', 'CONTRACT_DATA'],
    typicalCertifications: ['ISO 27001', 'SOC 2'],
    inherentRiskFactors: [
      { factor: 'customer_data', weight: 0.8, description: { de: 'Kundendaten', en: 'Customer data' } },
      { factor: 'data_volume', weight: 0.7, description: { de: 'Hohes Datenvolumen', en: 'High data volume' } },
    ],
    commonProviders: ['Salesforce', 'HubSpot', 'Pipedrive', 'Microsoft Dynamics', 'Zoho CRM'],
  },
  {
    id: 'tpl-vendor-erp',
    name: { de: 'ERP-System', en: 'ERP System' },
    description: {
      de: 'Enterprise Resource Planning System',
      en: 'Enterprise Resource Planning System',
    },
    serviceCategory: 'ERP',
    suggestedRole: 'PROCESSOR',
    suggestedDataAccess: 'CONTENT',
    suggestedTransferMechanisms: ['ADEQUACY_DECISION', 'SCC_PROCESSOR'],
    suggestedContractTypes: ['AVV', 'MSA', 'SLA', 'TOM_ANNEX'],
    typicalDataCategories: [
      'NAME', 'CONTACT', 'ADDRESS', 'BANK_ACCOUNT', 'CONTRACT_DATA',
      'EMPLOYMENT_DATA', 'SALARY_DATA',
    ],
    typicalCertifications: ['ISO 27001', 'SOC 2'],
    inherentRiskFactors: [
      { factor: 'data_volume', weight: 0.9, description: { de: 'Sehr hohes Datenvolumen', en: 'Very high data volume' } },
      { factor: 'criticality', weight: 0.95, description: { de: 'Geschäftskritisch', en: 'Business critical' } },
      { factor: 'sensitive_data', weight: 0.8, description: { de: 'Sensible Daten', en: 'Sensitive data' } },
    ],
    commonProviders: ['SAP', 'Oracle', 'Microsoft Dynamics', 'Sage', 'Odoo'],
  },
  {
    id: 'tpl-vendor-hr',
    name: { de: 'HR-Software', en: 'HR Software' },
    description: {
      de: 'Personalverwaltung und HR-Management',
      en: 'Personnel administration and HR management',
    },
    serviceCategory: 'HR_SOFTWARE',
    suggestedRole: 'PROCESSOR',
    suggestedDataAccess: 'CONTENT',
    suggestedTransferMechanisms: ['ADEQUACY_DECISION', 'SCC_PROCESSOR'],
    suggestedContractTypes: ['AVV', 'MSA', 'TOM_ANNEX'],
    typicalDataCategories: [
      'NAME', 'CONTACT', 'ADDRESS', 'DOB', 'SOCIAL_SECURITY', 'TAX_ID',
      'BANK_ACCOUNT', 'EMPLOYMENT_DATA', 'SALARY_DATA', 'HEALTH_DATA',
    ],
    typicalCertifications: ['ISO 27001'],
    inherentRiskFactors: [
      { factor: 'employee_data', weight: 0.9, description: { de: 'Mitarbeiterdaten', en: 'Employee data' } },
      { factor: 'sensitive_data', weight: 0.85, description: { de: 'Sensible Daten', en: 'Sensitive data' } },
      { factor: 'special_categories', weight: 0.7, description: { de: 'Besondere Kategorien möglich', en: 'Special categories possible' } },
    ],
    commonProviders: ['Personio', 'Workday', 'SAP SuccessFactors', 'HRworks', 'Factorial'],
  },
  {
    id: 'tpl-vendor-accounting',
    name: { de: 'Buchhaltungssoftware', en: 'Accounting Software' },
    description: {
      de: 'Finanzbuchhaltung und Rechnungswesen',
      en: 'Financial accounting and bookkeeping',
    },
    serviceCategory: 'ACCOUNTING',
    suggestedRole: 'PROCESSOR',
    suggestedDataAccess: 'CONTENT',
    suggestedTransferMechanisms: ['ADEQUACY_DECISION'],
    suggestedContractTypes: ['AVV', 'MSA'],
    typicalDataCategories: ['NAME', 'ADDRESS', 'BANK_ACCOUNT', 'PAYMENT_DATA', 'TAX_ID'],
    typicalCertifications: ['ISO 27001', 'IDW PS 951'],
    inherentRiskFactors: [
      { factor: 'financial_data', weight: 0.85, description: { de: 'Finanzdaten', en: 'Financial data' } },
      { factor: 'legal_retention', weight: 0.7, description: { de: 'Aufbewahrungspflichten', en: 'Retention requirements' } },
    ],
    commonProviders: ['DATEV', 'Lexware', 'SevDesk', 'Xero', 'Sage'],
  },

  // Communication & Collaboration
  {
    id: 'tpl-vendor-email',
    name: { de: 'E-Mail-Dienst', en: 'Email Service' },
    description: {
      de: 'E-Mail-Hosting und -Kommunikation',
      en: 'Email hosting and communication',
    },
    serviceCategory: 'EMAIL',
    suggestedRole: 'PROCESSOR',
    suggestedDataAccess: 'CONTENT',
    suggestedTransferMechanisms: ['SCC_PROCESSOR', 'BCR'],
    suggestedContractTypes: ['AVV', 'MSA', 'SLA'],
    typicalDataCategories: ['NAME', 'CONTACT', 'COMMUNICATION_DATA'],
    typicalCertifications: ['ISO 27001', 'SOC 2'],
    inherentRiskFactors: [
      { factor: 'communication_data', weight: 0.8, description: { de: 'Kommunikationsdaten', en: 'Communication data' } },
      { factor: 'criticality', weight: 0.8, description: { de: 'Geschäftskritisch', en: 'Business critical' } },
    ],
    commonProviders: ['Microsoft 365', 'Google Workspace', 'Zoho Mail', 'ProtonMail'],
  },
  {
    id: 'tpl-vendor-communication',
    name: { de: 'Kollaborations-Tool', en: 'Collaboration Tool' },
    description: {
      de: 'Team-Kommunikation und Zusammenarbeit',
      en: 'Team communication and collaboration',
    },
    serviceCategory: 'COMMUNICATION',
    suggestedRole: 'PROCESSOR',
    suggestedDataAccess: 'CONTENT',
    suggestedTransferMechanisms: ['SCC_PROCESSOR', 'BCR'],
    suggestedContractTypes: ['AVV', 'MSA'],
    typicalDataCategories: ['NAME', 'CONTACT', 'COMMUNICATION_DATA', 'PHOTO_VIDEO'],
    typicalCertifications: ['ISO 27001', 'SOC 2'],
    inherentRiskFactors: [
      { factor: 'communication_data', weight: 0.7, description: { de: 'Kommunikationsdaten', en: 'Communication data' } },
      { factor: 'file_sharing', weight: 0.6, description: { de: 'Dateifreigabe', en: 'File sharing' } },
    ],
    commonProviders: ['Slack', 'Microsoft Teams', 'Zoom', 'Google Meet', 'Webex'],
  },

  // Marketing & Analytics
  {
    id: 'tpl-vendor-analytics',
    name: { de: 'Analytics-Tool', en: 'Analytics Tool' },
    description: {
      de: 'Web-Analyse und Nutzerverhalten',
      en: 'Web analytics and user behavior',
    },
    serviceCategory: 'ANALYTICS',
    suggestedRole: 'PROCESSOR',
    suggestedDataAccess: 'CONTENT',
    suggestedTransferMechanisms: ['SCC_PROCESSOR'],
    suggestedContractTypes: ['AVV', 'MSA'],
    typicalDataCategories: ['IP_ADDRESS', 'DEVICE_ID', 'USAGE_DATA', 'LOCATION_DATA'],
    typicalCertifications: ['ISO 27001'],
    inherentRiskFactors: [
      { factor: 'tracking', weight: 0.7, description: { de: 'Tracking', en: 'Tracking' } },
      { factor: 'profiling', weight: 0.6, description: { de: 'Profiling möglich', en: 'Profiling possible' } },
    ],
    commonProviders: ['Google Analytics', 'Matomo', 'Plausible', 'Mixpanel', 'Amplitude'],
  },
  {
    id: 'tpl-vendor-marketing-automation',
    name: { de: 'Marketing-Automatisierung', en: 'Marketing Automation' },
    description: {
      de: 'E-Mail-Marketing und Automatisierung',
      en: 'Email marketing and automation',
    },
    serviceCategory: 'MARKETING',
    suggestedRole: 'PROCESSOR',
    suggestedDataAccess: 'CONTENT',
    suggestedTransferMechanisms: ['SCC_PROCESSOR'],
    suggestedContractTypes: ['AVV', 'MSA'],
    typicalDataCategories: ['NAME', 'CONTACT', 'USAGE_DATA'],
    typicalCertifications: ['ISO 27001'],
    inherentRiskFactors: [
      { factor: 'marketing_data', weight: 0.6, description: { de: 'Marketing-Daten', en: 'Marketing data' } },
      { factor: 'consent_management', weight: 0.7, description: { de: 'Einwilligungsmanagement', en: 'Consent management' } },
    ],
    commonProviders: ['Mailchimp', 'HubSpot', 'Sendinblue', 'CleverReach', 'ActiveCampaign'],
  },

  // Support & Service
  {
    id: 'tpl-vendor-support',
    name: { de: 'Support-/Ticketsystem', en: 'Support/Ticket System' },
    description: {
      de: 'Kundenservice und Ticket-Management',
      en: 'Customer service and ticket management',
    },
    serviceCategory: 'SUPPORT',
    suggestedRole: 'PROCESSOR',
    suggestedDataAccess: 'CONTENT',
    suggestedTransferMechanisms: ['SCC_PROCESSOR'],
    suggestedContractTypes: ['AVV', 'MSA'],
    typicalDataCategories: ['NAME', 'CONTACT', 'COMMUNICATION_DATA', 'CONTRACT_DATA'],
    typicalCertifications: ['ISO 27001', 'SOC 2'],
    inherentRiskFactors: [
      { factor: 'customer_data', weight: 0.7, description: { de: 'Kundendaten', en: 'Customer data' } },
      { factor: 'communication', weight: 0.6, description: { de: 'Kommunikationsinhalte', en: 'Communication content' } },
    ],
    commonProviders: ['Zendesk', 'Freshdesk', 'Intercom', 'HelpScout', 'Jira Service Management'],
  },

  // Payment & Finance
  {
    id: 'tpl-vendor-payment',
    name: { de: 'Zahlungsdienstleister', en: 'Payment Service Provider' },
    description: {
      de: 'Zahlungsabwicklung und Payment Gateway',
      en: 'Payment processing and payment gateway',
    },
    serviceCategory: 'PAYMENT',
    suggestedRole: 'PROCESSOR',
    suggestedDataAccess: 'CONTENT',
    suggestedTransferMechanisms: ['SCC_PROCESSOR', 'BCR'],
    suggestedContractTypes: ['AVV', 'MSA', 'SLA'],
    typicalDataCategories: ['NAME', 'ADDRESS', 'BANK_ACCOUNT', 'PAYMENT_DATA'],
    typicalCertifications: ['PCI DSS', 'ISO 27001'],
    inherentRiskFactors: [
      { factor: 'financial_data', weight: 0.9, description: { de: 'Finanzdaten', en: 'Financial data' } },
      { factor: 'pci_scope', weight: 0.8, description: { de: 'PCI-Scope', en: 'PCI scope' } },
    ],
    commonProviders: ['Stripe', 'PayPal', 'Adyen', 'Mollie', 'Klarna'],
  },

  // Security
  {
    id: 'tpl-vendor-security',
    name: { de: 'Sicherheitsdienstleister', en: 'Security Service Provider' },
    description: {
      de: 'IT-Sicherheit, Penetrationstests, SIEM',
      en: 'IT security, penetration testing, SIEM',
    },
    serviceCategory: 'SECURITY',
    suggestedRole: 'PROCESSOR',
    suggestedDataAccess: 'ADMINISTRATIVE',
    suggestedTransferMechanisms: ['ADEQUACY_DECISION', 'SCC_PROCESSOR'],
    suggestedContractTypes: ['AVV', 'MSA', 'NDA'],
    typicalDataCategories: ['IP_ADDRESS', 'USAGE_DATA', 'LOGIN_DATA'],
    typicalCertifications: ['ISO 27001', 'SOC 2'],
    inherentRiskFactors: [
      { factor: 'system_access', weight: 0.8, description: { de: 'Systemzugriff', en: 'System access' } },
      { factor: 'security_data', weight: 0.7, description: { de: 'Sicherheitsdaten', en: 'Security data' } },
    ],
    commonProviders: ['CrowdStrike', 'Splunk', 'Palo Alto Networks', 'Tenable'],
  },

  // Backup & Storage
  {
    id: 'tpl-vendor-backup',
    name: { de: 'Backup-Anbieter', en: 'Backup Provider' },
    description: {
      de: 'Datensicherung und Disaster Recovery',
      en: 'Data backup and disaster recovery',
    },
    serviceCategory: 'BACKUP',
    suggestedRole: 'PROCESSOR',
    suggestedDataAccess: 'CONTENT',
    suggestedTransferMechanisms: ['ADEQUACY_DECISION', 'SCC_PROCESSOR'],
    suggestedContractTypes: ['AVV', 'MSA', 'SLA'],
    typicalDataCategories: ['NAME', 'CONTACT', 'CONTRACT_DATA'],
    typicalCertifications: ['ISO 27001', 'SOC 2'],
    inherentRiskFactors: [
      { factor: 'full_backup', weight: 0.9, description: { de: 'Vollständige Kopie', en: 'Full copy' } },
      { factor: 'retention', weight: 0.7, description: { de: 'Lange Aufbewahrung', en: 'Long retention' } },
    ],
    commonProviders: ['Veeam', 'Acronis', 'Commvault', 'AWS Backup'],
  },

  // Consulting
  {
    id: 'tpl-vendor-consulting',
    name: { de: 'Beratungsunternehmen', en: 'Consulting Company' },
    description: {
      de: 'IT-Beratung, Projektunterstützung',
      en: 'IT consulting, project support',
    },
    serviceCategory: 'CONSULTING',
    suggestedRole: 'PROCESSOR',
    suggestedDataAccess: 'POTENTIAL',
    suggestedTransferMechanisms: ['ADEQUACY_DECISION'],
    suggestedContractTypes: ['AVV', 'MSA', 'NDA'],
    typicalDataCategories: ['NAME', 'CONTACT', 'CONTRACT_DATA'],
    typicalCertifications: ['ISO 27001'],
    inherentRiskFactors: [
      { factor: 'project_access', weight: 0.5, description: { de: 'Projektzugriff', en: 'Project access' } },
      { factor: 'temporary', weight: 0.4, description: { de: 'Temporär', en: 'Temporary' } },
    ],
    commonProviders: ['Accenture', 'McKinsey', 'Deloitte', 'PwC', 'KPMG'],
  },
]

// ==========================================
// HELPER FUNCTIONS (template-specific)
// ==========================================

/**
 * Get vendor template by ID
 */
export function getVendorTemplateById(id: string): VendorTemplate | undefined {
  return VENDOR_TEMPLATES.find((t) => t.id === id)
}

/**
 * Get vendor templates by category
 */
export function getVendorTemplatesByCategory(category: ServiceCategory): VendorTemplate[] {
  return VENDOR_TEMPLATES.filter((t) => t.serviceCategory === category)
}

/**
 * Calculate inherent risk score for vendor template
 */
export function calculateTemplateRiskScore(template: VendorTemplate): number {
  const baseScore = template.inherentRiskFactors.reduce(
    (sum, factor) => sum + factor.weight * 100,
    0
  )
  return Math.min(100, baseScore / template.inherentRiskFactors.length)
}

/**
 * Create form data from vendor template
 */
export function createVendorFormDataFromTemplate(
  template: VendorTemplate
): Partial<VendorFormData> {
  return {
    serviceCategory: template.serviceCategory,
    role: template.suggestedRole,
    dataAccessLevel: template.suggestedDataAccess,
    transferMechanisms: template.suggestedTransferMechanisms,
    contractTypes: template.suggestedContractTypes,
    certifications: template.typicalCertifications.map((type) => ({
      type,
      issuedDate: undefined,
      expirationDate: undefined,
    })),
    reviewFrequency: 'ANNUAL',
  }
}

// Re-export country profiles and functions for backward compatibility
export {
  COUNTRY_RISK_PROFILES,
  getCountryRiskProfile,
  requiresTransferMechanism,
  getSuggestedTransferMechanisms,
  getEUEEACountries,
  getAdequateCountries,
  getHighRiskCountries,
} from './vendor-country-profiles'
