/**
 * Cookie Banner — Vendor data and EWR classification.
 *
 * Demo vendors per category, mirroring the service registry + cookie_table_generator.py.
 * Used by CookieBannerOverlay for vendor display and EWR filtering.
 */

export interface VendorInfo {
  name: string
  cookies: string
  provider: string
  retention: string
  country: string
}

export interface CategoryVendorData {
  label: string
  description: string
  vendors: VendorInfo[]
}

// EWR = EU + Island, Liechtenstein, Norwegen. CH has adequacy decision.
const EWR_SAFE = ['de', 'at', 'fr', 'nl', 'ie', 'se', 'dk', 'fi', 'be', 'it', 'es',
  'pt', 'pl', 'cz', 'hu', 'ro', 'bg', 'hr', 'sk', 'si', 'lt', 'lv', 'ee', 'cy',
  'mt', 'lu', 'gr', 'is', 'li', 'no', 'ch',  // CH: Angemessenheitsbeschluss
  'eu', 'ewr', 'eigener server']

export function isEWR(country: string): boolean {
  if (!country) return true // No country info = assume first party
  const lower = country.toLowerCase()
  return EWR_SAFE.some(safe => lower.includes(safe))
}

export function isOutsideEWR(country: string): boolean {
  return !isEWR(country)
}

export function countNonEWRVendors(): number {
  let count = 0
  for (const cat of Object.values(CATEGORY_VENDORS)) {
    count += cat.vendors.filter(v => isOutsideEWR(v.country)).length
  }
  return count
}

// Demo vendors per category — mirrors service registry + cookie_table_generator.py
export const CATEGORY_VENDORS: Record<string, CategoryVendorData> = {
  necessary: {
    label: 'Notwendig',
    description: 'Fuer die Grundfunktionen der Website erforderlich.',
    vendors: [
      { name: 'Session', cookies: 'session_id', provider: 'Eigener Server', retention: 'Session', country: 'DE' },
      { name: 'Consent-Cookie', cookies: 'bp_consent', provider: 'Eigener Server', retention: '12 Monate', country: 'DE' },
      { name: 'Cloudflare', cookies: '__cf_bm', provider: 'Cloudflare Inc.', retention: '30 Min.', country: 'USA (DPF)' },
      { name: 'Stripe', cookies: '__stripe_mid', provider: 'Stripe Inc.', retention: 'Session', country: 'USA (DPF)' },
    ],
  },
  statistics: {
    label: 'Statistik',
    description: 'Helfen uns zu verstehen, wie Besucher mit der Website interagieren.',
    vendors: [
      { name: 'Google Analytics', cookies: '_ga, _gid', provider: 'Google LLC', retention: '2 Jahre', country: 'USA (DPF)' },
      { name: 'Hotjar', cookies: '_hj*', provider: 'Hotjar Ltd.', retention: '1 Jahr', country: 'EU (Malta)' },
      { name: 'Google Tag Manager', cookies: '_gcl_au', provider: 'Google LLC', retention: '90 Tage', country: 'USA (DPF)' },
    ],
  },
  marketing: {
    label: 'Marketing',
    description: 'Werden verwendet, um Besuchern relevante Werbung zu zeigen.',
    vendors: [
      { name: 'Facebook Pixel', cookies: '_fbp, _fbc', provider: 'Meta Platforms', retention: '90 Tage', country: 'USA (DPF)' },
      { name: 'Google Ads', cookies: '_gcl_aw, IDE', provider: 'Google LLC', retention: '90 Tage', country: 'USA (DPF)' },
      { name: 'LinkedIn Insight', cookies: 'bcookie, li_sugr', provider: 'LinkedIn Ireland', retention: '6 Monate', country: 'EU (Irland)' },
    ],
  },
  functional: {
    label: 'Funktional',
    description: 'Ermoeglichen erweiterte Funktionen und Personalisierung.',
    vendors: [
      { name: 'Spracheinstellung', cookies: 'bp_lang', provider: 'Eigener Server', retention: '12 Monate', country: 'DE' },
      { name: 'YouTube', cookies: 'YSC, VISITOR_INFO1_LIVE', provider: 'Google LLC', retention: '6 Monate', country: 'USA (DPF)' },
      { name: 'HubSpot Chat', cookies: '__hstc, hubspotutk', provider: 'HubSpot Inc.', retention: '13 Monate', country: 'USA (DPF)' },
    ],
  },
}
