import React from 'react'
import {
  CheckCircle,
  XCircle,
  FileCheck,
  Shield,
} from 'lucide-react'

export type ConsentType = 'marketing' | 'analytics' | 'newsletter' | 'terms' | 'privacy' | 'cookies'
export type ConsentStatus = 'granted' | 'withdrawn' | 'pending'
export type HistoryAction = 'granted' | 'withdrawn' | 'version_update' | 'renewed'

export interface ConsentHistoryEntry {
  id: string
  action: HistoryAction
  timestamp: Date
  version: string
  documentTitle?: string
  ipAddress: string
  userAgent: string
  source: string
  notes?: string
}

export interface ConsentRecord {
  id: string
  identifier: string
  email: string
  firstName?: string
  lastName?: string
  consentType: ConsentType
  status: ConsentStatus
  currentVersion: string
  grantedAt: Date | null
  withdrawnAt: Date | null
  source: string | null
  ipAddress: string
  userAgent: string
  history: ConsentHistoryEntry[]
}

export const typeLabels: Record<ConsentType, string> = {
  marketing: 'Marketing',
  analytics: 'Analyse',
  newsletter: 'Newsletter',
  terms: 'AGB',
  privacy: 'Datenschutz',
  cookies: 'Cookies',
}

export const typeColors: Record<ConsentType, string> = {
  marketing: 'bg-purple-100 text-purple-700',
  analytics: 'bg-blue-100 text-blue-700',
  newsletter: 'bg-green-100 text-green-700',
  terms: 'bg-yellow-100 text-yellow-700',
  privacy: 'bg-orange-100 text-orange-700',
  cookies: 'bg-pink-100 text-pink-700',
}

export const statusColors: Record<ConsentStatus, string> = {
  granted: 'bg-green-100 text-green-700',
  withdrawn: 'bg-red-100 text-red-700',
  pending: 'bg-yellow-100 text-yellow-700',
}

export const statusLabels: Record<ConsentStatus, string> = {
  granted: 'Erteilt',
  withdrawn: 'Widerrufen',
  pending: 'Ausstehend',
}

export const actionLabels: Record<HistoryAction, string> = {
  granted: 'Einwilligung erteilt',
  withdrawn: 'Einwilligung widerrufen',
  version_update: 'Neue Version akzeptiert',
  renewed: 'Einwilligung erneuert',
}

export const actionIcons: Record<HistoryAction, React.ReactNode> = {
  granted: React.createElement(CheckCircle, { className: 'w-5 h-5 text-green-500' }),
  withdrawn: React.createElement(XCircle, { className: 'w-5 h-5 text-red-500' }),
  version_update: React.createElement(FileCheck, { className: 'w-5 h-5 text-blue-500' }),
  renewed: React.createElement(Shield, { className: 'w-5 h-5 text-purple-500' }),
}

export function formatDateTime(date: Date): string {
  return date.toLocaleString('de-DE', {
    day: '2-digit',
    month: '2-digit',
    year: 'numeric',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit',
  })
}

export function formatDate(date: Date | null): string {
  if (!date) return '-'
  return date.toLocaleDateString('de-DE')
}

export const PAGE_SIZE = 50

// Banner (Device-based) Consent
export interface BannerConsentRecord {
  id: string
  site_id: string
  device_fingerprint: string
  categories: string[]
  vendors: string[]
  vendor_consents: Record<string, boolean>
  ip_hash: string | null
  user_agent: string | null
  linked_email: string | null
  consent_string: string | null
  // Vendor-agnostische Felder (Migration 107)
  consent_method: string | null
  banner_version: number | null
  banner_config_hash: string | null
  geo_country: string | null
  geo_region: string | null
  consent_scope: string | null
  page_url: string | null
  referrer: string | null
  device_type: string | null
  browser: string | null
  os: string | null
  screen_resolution: string | null
  session_id: string | null
  // Script/Cookie-Tracking (Migration 108)
  scripts_blocked: { src: string; category: string }[]
  scripts_released: { src: string; category: string }[]
  cookies_set: { name: string; domain: string; expiry_days: number; category: string }[]
  expires_at: string | null
  created_at: string | null
  updated_at: string | null
}

export interface BannerConsentStats {
  total_consents: number
  category_acceptance: Record<string, { count: number; rate: number }>
}

export interface BannerSite {
  site_id: string
  site_name: string
  site_url: string
  tcf_enabled?: boolean
}
