/**
 * Consent SDK Types
 *
 * DSGVO/TTDSG-konforme Typdefinitionen für das Consent Management System.
 */

// =============================================================================
// Consent Categories
// =============================================================================

/**
 * Standard-Consent-Kategorien nach IAB TCF 2.2
 */
export type ConsentCategory =
  | 'essential'   // Technisch notwendig (TTDSG § 25 Abs. 2)
  | 'functional'  // Personalisierung, Komfortfunktionen
  | 'analytics'   // Anonyme Nutzungsanalyse
  | 'marketing'   // Werbung, Retargeting
  | 'social';     // Social Media Plugins

/**
 * Consent-Status pro Kategorie
 */
export type ConsentCategories = Record<ConsentCategory, boolean>;

/**
 * Consent-Status pro Vendor
 */
export type ConsentVendors = Record<string, boolean>;

// =============================================================================
// Consent State
// =============================================================================

/**
 * Aktueller Consent-Zustand
 */
export interface ConsentState {
  /** Consent pro Kategorie */
  categories: ConsentCategories;

  /** Consent pro Vendor (optional, für granulare Kontrolle) */
  vendors: ConsentVendors;

  /** Zeitstempel der letzten Aenderung */
  timestamp: string;

  /** SDK-Version bei Erstellung */
  version: string;

  /** Eindeutige Consent-ID vom Backend */
  consentId?: string;

  /** Ablaufdatum */
  expiresAt?: string;

  /** IAB TCF String (falls aktiviert) */
  tcfString?: string;
}

/**
 * Minimaler Consent-Input fuer setConsent()
 */
export type ConsentInput = Partial<ConsentCategories> | {
  categories?: Partial<ConsentCategories>;
  vendors?: ConsentVendors;
};

// =============================================================================
// Configuration
// =============================================================================

/**
 * UI-Position des Banners
 */
export type BannerPosition = 'bottom' | 'top' | 'center';

/**
 * Banner-Layout
 */
export type BannerLayout = 'bar' | 'modal' | 'floating';

/**
 * Farbschema
 */
export type BannerTheme = 'light' | 'dark' | 'auto';

/**
 * UI-Konfiguration
 */
export interface ConsentUIConfig {
  /** Position des Banners */
  position?: BannerPosition;

  /** Layout-Typ */
  layout?: BannerLayout;

  /** Farbschema */
  theme?: BannerTheme;

  /** Pfad zu Custom CSS */
  customCss?: string;

  /** z-index fuer Banner */
  zIndex?: number;

  /** Scroll blockieren bei Modal */
  blockScrollOnModal?: boolean;

  /** Custom Container-ID */
  containerId?: string;
}

/**
 * Consent-Verhaltens-Konfiguration
 */
export interface ConsentBehaviorConfig {
  /** Muss Nutzer interagieren? */
  required?: boolean;

  /** "Alle ablehnen" Button sichtbar */
  rejectAllVisible?: boolean;

  /** "Alle akzeptieren" Button sichtbar */
  acceptAllVisible?: boolean;

  /** Einzelne Kategorien waehlbar */
  granularControl?: boolean;

  /** Einzelne Vendors waehlbar */
  vendorControl?: boolean;

  /** Auswahl speichern */
  rememberChoice?: boolean;

  /** Speicherdauer in Tagen */
  rememberDays?: number;

  /** Nur in EU anzeigen (Geo-Targeting) */
  geoTargeting?: boolean;

  /** Erneut nachfragen nach X Tagen */
  recheckAfterDays?: number;
}

/**
 * TCF 2.2 Konfiguration
 */
export interface TCFConfig {
  /** TCF aktivieren */
  enabled?: boolean;

  /** CMP ID */
  cmpId?: number;

  /** CMP Version */
  cmpVersion?: number;
}

/**
 * PWA-spezifische Konfiguration
 */
export interface PWAConfig {
  /** Offline-Unterstuetzung aktivieren */
  offlineSupport?: boolean;

  /** Bei Reconnect synchronisieren */
  syncOnReconnect?: boolean;

  /** Cache-Strategie */
  cacheStrategy?: 'stale-while-revalidate' | 'network-first' | 'cache-first';
}

/**
 * Haupt-Konfiguration fuer ConsentManager
 */
export interface ConsentConfig {
  // Pflichtfelder
  /** API-Endpunkt fuer Consent-Backend */
  apiEndpoint: string;

  /** Site-ID */
  siteId: string;

  // Sprache
  /** Sprache (ISO 639-1) */
  language?: string;

  /** Fallback-Sprache */
  fallbackLanguage?: string;

  // UI
  /** UI-Konfiguration */
  ui?: ConsentUIConfig;

  // Verhalten
  /** Consent-Verhaltens-Konfiguration */
  consent?: ConsentBehaviorConfig;

  // Kategorien
  /** Aktive Kategorien */
  categories?: ConsentCategory[];

  // TCF
  /** TCF 2.2 Konfiguration */
  tcf?: TCFConfig;

  // PWA
  /** PWA-Konfiguration */
  pwa?: PWAConfig;

  // Callbacks
  /** Callback bei Consent-Aenderung */
  onConsentChange?: (consent: ConsentState) => void;

  /** Callback wenn Banner angezeigt wird */
  onBannerShow?: () => void;

  /** Callback wenn Banner geschlossen wird */
  onBannerHide?: () => void;

  /** Callback bei Fehler */
  onError?: (error: Error) => void;

  // Debug
  /** Debug-Modus aktivieren */
  debug?: boolean;
}

// =============================================================================
// Vendor Configuration
// =============================================================================

/**
 * Cookie-Information
 */
export interface CookieInfo {
  /** Cookie-Name */
  name: string;

  /** Cookie-Domain */
  domain: string;

  /** Ablaufzeit (z.B. "2 Jahre", "Session") */
  expiration: string;

  /** Speichertyp */
  type: 'http' | 'localStorage' | 'sessionStorage' | 'indexedDB';

  /** Beschreibung */
  description: string;
}

/**
 * Vendor-Definition
 */
export interface ConsentVendor {
  /** Eindeutige Vendor-ID */
  id: string;

  /** Anzeigename */
  name: string;

  /** Kategorie */
  category: ConsentCategory;

  /** IAB TCF Purposes (falls relevant) */
  purposes?: number[];

  /** Legitimate Interests */
  legitimateInterests?: number[];

  /** Cookie-Liste */
  cookies: CookieInfo[];

  /** Link zur Datenschutzerklaerung */
  privacyPolicyUrl: string;

  /** Datenaufbewahrung */
  dataRetention?: string;

  /** Datentransfer (z.B. "USA (EU-US DPF)", "EU") */
  dataTransfer?: string;
}

// =============================================================================
// API Types
// =============================================================================

/**
 * API-Antwort fuer Consent-Erstellung
 */
export interface ConsentAPIResponse {
  consentId: string;
  timestamp: string;
  expiresAt: string;
  version: string;
}

/**
 * API-Antwort fuer Site-Konfiguration
 */
export interface SiteConfigResponse {
  siteId: string;
  siteName: string;
  categories: CategoryConfig[];
  ui: ConsentUIConfig;
  legal: LegalConfig;
  tcf?: TCFConfig;
}

/**
 * Kategorie-Konfiguration vom Server
 */
export interface CategoryConfig {
  id: ConsentCategory;
  name: Record<string, string>;
  description: Record<string, string>;
  required: boolean;
  vendors: ConsentVendor[];
}

/**
 * Rechtliche Konfiguration
 */
export interface LegalConfig {
  privacyPolicyUrl: string;
  imprintUrl: string;
  dpo?: {
    name: string;
    email: string;
  };
}

// =============================================================================
// Events
// =============================================================================

/**
 * Event-Typen
 */
export type ConsentEventType =
  | 'init'
  | 'change'
  | 'accept_all'
  | 'reject_all'
  | 'save_selection'
  | 'banner_show'
  | 'banner_hide'
  | 'settings_open'
  | 'settings_close'
  | 'vendor_enable'
  | 'vendor_disable'
  | 'error';

/**
 * Event-Listener Callback
 */
export type ConsentEventCallback<T = unknown> = (data: T) => void;

/**
 * Event-Daten fuer verschiedene Events
 */
export type ConsentEventData = {
  init: ConsentState | null;
  change: ConsentState;
  accept_all: ConsentState;
  reject_all: ConsentState;
  save_selection: ConsentState;
  banner_show: undefined;
  banner_hide: undefined;
  settings_open: undefined;
  settings_close: undefined;
  vendor_enable: string;
  vendor_disable: string;
  error: Error;
};

// =============================================================================
// Storage
// =============================================================================

/**
 * Storage-Adapter Interface
 */
export interface ConsentStorageAdapter {
  /** Consent laden */
  get(): ConsentState | null;

  /** Consent speichern */
  set(consent: ConsentState): void;

  /** Consent loeschen */
  clear(): void;

  /** Pruefen ob Consent existiert */
  exists(): boolean;
}

// =============================================================================
// Translations
// =============================================================================

/**
 * Uebersetzungsstruktur
 */
export interface ConsentTranslations {
  title: string;
  description: string;
  acceptAll: string;
  rejectAll: string;
  settings: string;
  saveSelection: string;
  close: string;
  categories: {
    [K in ConsentCategory]: {
      name: string;
      description: string;
    };
  };
  footer: {
    privacyPolicy: string;
    imprint: string;
    cookieDetails: string;
  };
  accessibility: {
    closeButton: string;
    categoryToggle: string;
    requiredCategory: string;
  };
}

/**
 * Alle unterstuetzten Sprachen
 */
export type SupportedLanguage =
  | 'de' | 'en' | 'fr' | 'es' | 'it' | 'nl' | 'pl' | 'pt'
  | 'cs' | 'da' | 'el' | 'fi' | 'hu' | 'ro' | 'sk' | 'sl' | 'sv';
